Im trying to figure out the most effective way to parse som quite complicated XML using SimpleXML, and I've become stuck when there are namespaces in the document.
Okay, so my XML looks something like this:
<ns:event xmls:ns="http://example.com/event/1.1">
<ns:eventinfo>
<ns:start year="2011" month="9" />
<ns:eventnames>
<ns:eventname>Superevent</ns:eventname>
<开发者_开发技巧/ns:eventnames>
</ns:eventinfo>
<ns:eventlocale>My place</ns:eventlocale>
</ns:event>
I am able to extract the information from the "normal" tags via:
$data = simplexml_load_string($xml);
foreach ($data->children('ns', true) as $children) {
$child = $children->children('ns',true);
$eventname = ($child->eventname);
}
This would give $eventname as Superevent. However, this approach doesn't work with attributes...
But if there wouldn't be any namespaces, I would easily extract the attributes with for example:
$startyear = $data->$start['year'];
So - anyone got an idea to approach this problem with ease? Any info or ideas would be much appreciated.
Have you considered using SimpleXML's xpath functionality?
// your data example missed an 'n' in xmlns, so reposted, just to be sure
// with some additional ns:event elements in a root element
$xml = '<?xml version="1.0" encoding="utf-8"?>
<root>
<ns:event xmlns:ns="http://example.com/event/1.1">
<ns:eventinfo>
<ns:start year="2011" month="9" />
<ns:eventnames>
<ns:eventname>Superevent</ns:eventname>
</ns:eventnames>
</ns:eventinfo>
<ns:eventlocale>My place</ns:eventlocale>
</ns:event>
<ns:event xmlns:ns="http://example.com/event/1.1">
<ns:eventinfo>
<ns:start year="2011" month="8" />
<ns:eventnames>
<ns:eventname>Another Superevent</ns:eventname>
</ns:eventnames>
</ns:eventinfo>
<ns:eventlocale>Your place</ns:eventlocale>
</ns:event>
</root>';
$data = simplexml_load_string($xml);
$data->registerXPathNamespace( 'ns', 'http://example.com/event/1.1' );
# '//ns:event' means: find ns:event elements anywhere in the document
# '/ns:event' would mean: find ns:event elements that are direct children of the root
$events = $data->xpath( '//ns:event' );
foreach( $events as $event )
{
# from now on, we are using $event (ns:event elements) as our contexts to query
# '.' means: from our current context node find...
# '/ns:eventinfo[1]' means: find the first ns:eventinfo element that is a direct child of the preceding expression
# '/ns:eventnames[1]' means: same as previous but ns:eventnames element
# '/ns:eventname[1]' means: same as previous but ns:eventname element
$eventname = $event->xpath( './ns:eventinfo[1]/ns:eventnames[1]/ns:eventname[1]' );
# '/@year' means: the year attribute that is a direct child of the preceding expression
$year = $event->xpath( './ns:eventinfo[1]/ns:start[1]/@year' );
# '/@month' means: same as previous but month attribute
$month = $event->xpath( './ns:eventinfo[1]/ns:start[1]/@month' );
$eventlocale = $event->xpath( './ns:eventlocale[1]' );
# echo the first elements from the results found by our xpath queries
echo 'Event "' . $eventname[ 0 ] . '" taking place at ' . $eventlocale[ 0 ] . ' sometime in ' . $month[ 0 ] . '/' . $year[ 0 ] . '<br>';
}
edit:
What I did in the previous example was rather verbose by the way. This should probably work just as well inside the loop, as you are taking the first found nodes anyway already in the echo
statement:
# relative to our context node '.' find all descendants '//' that is an ns:eventname element
$eventname = $event->xpath( './/ns:eventname' );
# relative to our context node '.' find all descendants '//' that is a year attribute of an ns:start element
$year = $event->xpath( './/ns:start/@year' );
# relative to our context node '.' find all descendants '//' that is a month attribute of an ns:start element
$month = $event->xpath( './/ns:start/@month' );
# relative to our context node '.' find all children '/' that is an ns:eventlocale element
$eventlocale = $event->xpath( './ns:eventlocale' );
You can use something like before you use simplexml_load_string:
$xml = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $xml);
See http://php.net/manual/en/function.preg-replace.php for the explanation ot the replacement parameter ("$1$2$3").
This will change <ns:eventinfo>
to <nseventinfo>
In your example, shouldn't it be $children->start['year']
not $data->start['year']
?
Here is an example of it working:
http://codepad.org/8i0fHYdo
You may access eventname by code:
$data->children("http://example.com/event/1.1")->eventinfo->children("http://example.com/event/1.1")->eventnames->children("http://example.com/event/1.1")->eventname
More detail about how to use children and parse XML with namespace may be found on my post Parse XML with namespace by SimpleXML in PHP. The XML is generated from Salesforce Output Message which contains XML namespace. I use SimpleXML to parse it with the help of children().
精彩评论