Re: Good XML Parser
| From: | Waynn Lue | Date: | Mon, 12 May 2008 11:28:52 +0000 |
| Subject: | Re: Good XML Parser | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-274177@lists.php.net to get a copy of this message | ||
So if I'm looking to parse certain attributes out of an XML tree, if I
use SAX, it seems that I would need to keep track of state internally.
E.g., if I have a tree like
<head>
<a>
<b></b>
</a>
<a>
<b></b>
</a>
</head>
and say I'm interested in all that's between <b> underneath any <a>,
I'd need to have a state machine that looked for an <a> followed by a
<b>. If I'm doing that, though, it seems like I should just start
using a DOM parser instead?
Thanks for any insight,
Waynn
On Mon, May 12, 2008 at 1:29 AM, David Otton
<phpmail@jawbone.freeserve.co.uk> wrote:
> 2008/5/12 Waynn Lue <waynnlue@gmail.com>:
>
> > What's the best way to pull down XML from a URL? fopen($URL), then
> > using xml_parse? Or should I be using XML_Parser or SimpleXML?
>
> XML parsers fall into two general camps - DOM and SAX. DOM parsers
> represent an entire XML document as a tree, in-memory, when they are
> first instantiated. They are generally more memory-hungry and take
> longer to instantiate, but they can answer queries like "what is the
> path to this node" or "give me the siblings of this node".
>
> SAX parsers are stream- or event-based, and are much more lightweight
> - they parse the XML in a JIT fashion, and can't answer much more than
> "give me the next node".
>
> If you just need the data, a SAX parser will probably do everything
> you need. If you need the tree structure implicit in an XML document,
> use a DOM parser. Expat, which XML Parser
> (http://uk3.php.net/manual/en/book.xml.php) is based on, is a SAX
> parser. DOM XML (http://uk3.php.net/manual/en/book.domxml.php) is,
> obviously, a DOM parser. I don't know, off the top of my head, which
> camp SimpleXML falls into.
>