+6  A: 

You could use SimpleXML, which is included in a default PHP install. This extensions offers easy object-oriented access to XML-structures.

There's also DOM XML. A "downside" to this extension is that it is a bit harder to use and that it is not included by default.

Aron Rotteveel
DOMXML is a php4 module. php5 comes with a DOM library.
troelskn
+1  A: 

I prefer SimpleXMLElement as it's pretty easy to use to lop through elements.

Edit: It says no version info avaliable but it's avaliable in PHP5, at least 5.2.5 but probably earlier.

It's really personal choice though, there's plenty of XML extensions.

Bear in mind many XML parsers will balk if you have invalid markup - XHTML should be XML but not always!

Ross
A: 

It's been a long time (2 years or more) since I worked with XML parsing in PHP, but I always had good, usable results from the XML_Parser Pear package. Having said that, I have had minimal exposure to PHP5, so I don't really know if there are better, inbuilt alternatives these days.

ZombieSheep
A: 

Why would you want to parse XHTML in the first place? Just curious what application requires this.

Allain Lalonde
Possibly a HTML scraper?
Ross
HTML scraper maybe, but you'd have to make sure it was sanitized first. That'd probably be more work than just using a scraper designed for ugly html.
Allain Lalonde
Sorry if is kind of silly, but what is a scrapper?
NachoCual
A program that retrieves a webpage and extracts some information from it. It's a poor mans webservice.
Allain Lalonde
Yes, it's kind of what I'm doing actually, didn't know it's name. I've sanitized the HTML first using Tidy library, since it was Word97 generated (pretty messy).
NachoCual
A: 

I did a little bit of XML parsing in PHP5 last year and decided to use a combination of SimpleXML.

DOM is a bit more useful if you want to create a new XML tree or add to an existing one, its slightly more flexible.

A: 

It really depends on what you're trying to accomplish. For pulling rather large amounts of data, I.E many records of say, product information from a store website, I'd probably use Expat, since its supposedly a bit faster... Personally, I've has XML's large enough to create a noticeable performance boost. At those quantities you might as well be using SQL.

I recommend using SimpleXML. It's pretty intuitive, easy to use/write. Also, works great with XPath.

Never really got to use DOM much, but if you're using the XML Parser for something as large as you're describing you might want to use it, since its a bit more functional than SimpleXML.

You can read about all three at W3C Schools:

http://www.w3schools.com/php/php_xml_parser_expat.asp

http://www.w3schools.com/php/php_xml_simplexml.asp

http://www.w3schools.com/php/php_xml_dom.asp

+3  A: 

Just to clear up the confusion here. PHP has a number of XML libraries, because php4 didn't have very good options in that direction. From PHP5, you have the choice between SimpleXml, DOM and the sax-based expat parser. The latter also existed in php4. php4 also had a DOM extension, which is not the same as php5's.

DOM and SimpleXml are alternatives to the same problem domain; They læoad the document into memory and let you access it as a tree-structure. DOM is a rather bulky api, but it's also very consistent and it's implemented in many languages, meaning that you can re-use your knowledge across languages (In Javascript for example). SimpleXml may be easier initially.

The SAX parser is a different beast. It treats an xml document as a stream of tags. This is useful if you are dealing with very large documents, since you don't need to hold it all in memory.

For your usage, I would probably use the DOM api.

troelskn
I've decided to go for DOM, since it's implemented in another languages. I think it will be a better investment of my time.
NachoCual
I think that's a sane choice.
troelskn
+1  A: 
  • DOM is a standard, language-independent API for heirarchical data such as XML which has been standardized by the W3C. It is a rich API with much functionality. It is object based, in that each node is an object.

    DOM is good when you not only want to read, or write, but you want to do a lot of manipulation of nodes an existing document, such as inserting nodes between others, changing the structure, etc.

  • SimpleXML is a PHP-specific API which is also object-based but is intended to be a lot less 'terse' than the DOM: simple tasks such as finding the value of a node or finding its child elements take a lot less code. Its API is not as rich than DOM, but it still includes features such as XPath lookups, and a basic ability to work with multiple-namespace documents. And, importantly, it still preserves all features of your document such as XML CDATA sections and comments, even though it doesn't include functions to manipulate them.

    SimpleXML is very good for read-only: if all you want to do is read the XML document and convert it to another form, then it'll save you a lot of code. It's also fairly good when you want to generate a document, or do basic manipulations such as adding or changing child elements or attributes, but it can become complicated (but not impossible) to do a lot of manipulation of existing documents. It's not easy, for example, to add a child element in between two others; addChild only inserts after other elements. SimpleXML also cannot do XSLT transformations. It doesn't have things like 'getElementsByTagName' or getElementById', but if you know XPath you can still do that kind of thing with SimpleXML.

    The SimpleXMLElement object is somewhat 'magical'. The properties it exposes if you var_dump/printr/var_export don't correspond to its complete internal representation. It exposes some of its child elements as if they were properties which can be accessed with the -> operator, but still preserves the full document internally, and you can do things like access a child element whose name is a reserved word with the [] operator as if it was an associative array.

You don't have to fully commit to one or the other, because PHP implements the functions:

  • simplexml_import_dom(DOMNode)
  • dom_import_simplexml(SimpleXMLElement)

This is helpful if you are using SimpleXML and need to work with code that expects a DOM node or vice versa.

PHP also offers a third XML library:

  • XML Parser (an implementation of SAX, a language-independent interface, but not referred to by that name in the manual) is a much lower level library, which serves quite a different purpose. It doesn't build objects for you. It basically just makes it easier to write your own XML parser, because it does the job of advancing to the next token, and finding out the type of token, such as what tag name is and whether it's an opening or closing tag, for you. Then you have to write callbacks that should be run each time a token is encountered. All tasks such as representing the document as objects/arrays in a tree, manipulating the document, etc will need to be implemented separately, because all you can do with the XML parser is write a low level parser.

    The XML Parser functions are still quite helpful if you have specific memory or speed requirements. With it, it is possible to write a parser that can parse a very long XML document without holding all of its contents in memory at once. Also, if you not interested in all of the data, and don't need or want it to be put into a tree or set of PHP objects, then it can be quicker. For example, if you want to scan through an XHTML document and find all the links, and you don't care about structure.

thomasrutter
good summary, and good point highlighting the import functions there. It seems as though a lot of people forget that you can switch if you need to.
nickf
I added some info on the SAX parser - for completeness.
thomasrutter