Why do you want to reinvent the wheel? PHP is already an XML parser, even a DOMDocument library for html ...