PDA

View Full Version : Parsing XML with PHP when tags out of order or missing



raymonger
September 10th, 2007, 05:50 PM
Hello,

I have an XML parser in PHP, just like the one described here: http://www.kirupa.com/web/xml_php_parse_intermediate.htm.

However, I can't figure out how to make it bulletproof when the tags might be out of order or missing. For example, if the XML is usually:

<news>
<story>
<headline>blah blah</headline>
<description>this is the description</description>
<byline>who</byline>
<newspaper>paper</newspaper>
</story>
</news>

Where <story> is one item, and we open with <headline> and close with <newspaper>, what happens when <newspaper> is missing is the parser blows up. The parser sees the next <story> item and hits <headline> again before it's closed out the previous item.

Is there a way to open and close at the <story> level rather than need to close out based on individual tags like <headline> and <newspaper>? Or is there an even easier way to handle this when we can't ensure all the tags will be there?

Thanks,
Ray