Parsing XML with multiple namespaces in PHP - php

I have XML in the following form that I want to parse with PHP (I can't change the format of the XML). Neither SimpleXML nor DOM seem to handle the different namespaces - can anyone give me sample code? The code below gives no results.
<atom:feed>
<atom:entry>
<atom:id />
<otherns:othervalue />
</atom:entry>
<atom:entry>
<atom:id />
<otherns:othervalue />
</atom:entry>
</atom:feed>
$doc = new DOMDocument();
$doc->load($url);
$entries = $doc->getElementsByTagName("atom:entry");
foreach($entries as $entry) {
$id = $entry->getElementsByTagName("atom:id");
echo $id;
$othervalue = $entry->getElementsByTagName("otherns:othervalue");
echo $othervalue;
}

I just want to post with an answer to this awful question. Sorry.
Namespaces are irrelavent with DOM - I just wasn't getting the nodeValue from the Element.
$doc = new DOMDocument();
$doc->load($url);
$feed = $doc->getElementsByTagName("entry");
foreach($feed as $entry) {
$id = $entry->getElementsByTagName("id")->item(0)->nodeValue;
echo $id;
$id = $entry->getElementsByTagName("othervalue")->item(0)->nodeValue;
echo $othervalue;
}

You need to register your name spaces. Otherwise simplexml will ignore them.
This bit of code I got from the PHP manual and I used in my own project
$xmlsimple = simplexml_load_string('YOUR XML');
$namespaces = $xmlsimple->getNamespaces(true);
$extensions = array_keys($namespaces);
foreach ($extensions as $extension )
{
$xmlsimple->registerXPathNamespace($extension,$namespaces[$extension]);
}
After that you use xpath on $xmlsimple

Related

Parse YouTube RSS XML data <media:description> using PHP [duplicate]

This question already has answers here:
Reference - How do I handle Namespaces (Tags and Attributes with a Colon in their Name) in SimpleXML?
(2 answers)
Closed 1 year ago.
I have this as xml:
<root xmlns:event="http://www.webex.com/schemas/2002/06/service/event">
<event:event>
<event:sessionKey></event:sessionKey>
<event:sessionName>Learn QB in Minutes</event:sessionName>
<event:sessionType>9</event:sessionType>
<event:hostWebExID></event:hostWebExID>
<event:startDate>02/12/2009</event:startDate>
<event:endDate>02/12/2009</event:endDate>
<event:timeZoneID>11</event:timeZoneID>
<event:duration>30</event:duration>
<event:description></event:description>
<event:status>NOT_INPROGRESS</event:status>
<event:panelists></event:panelists>
<event:listStatus>PUBLIC</event:listStatus>
</event:event>
...
</root>
How can I loop through all of the event:event nodes and display, for example, all of the event:SessionKey's?
This does not work:
$xml = new SimpleXMLElement($r);
$xml->registerXPathNamespace('e', 'http://www.webex.com/schemas/2002/06/service/event');
foreach($xml->xpath('//e:event') as $event) {
var_export($event->xpath('//e:sessionKey'));
}
it does work without registerXPathNamespace and the full namespace prefix in the xpath queries:
$xml = new SimpleXMLElement($r);
foreach($xml->xpath('//event:event') as $event) {
var_export($event->xpath('event:sessionKey'));
}
You have to register the namespace for each simpleXMLElement object you use.
$xml = new SimpleXMLElement($r);
$xml->registerXPathNamespace('e', 'http://www.webex.com/schemas/2002/06/service/event');
foreach($xml->xpath('//e:event') as $event) {
$event->registerXPathNamespace('e', 'http://www.webex.com/schemas/2002/06/service/event');
var_export($event->xpath('//e:sessionKey'));
}
The namespace should also be declared somewhere in the xml file.
<event:event xmlns:event="http://www.webex.com/schemas/2002/06/service/event">
...
The method ax described works too. You can skip the registerXPathNamespace if you know the xml file will always use the same prefix.
Having worked a lot with simplexml, this is how I do it.
The magic trick if you already have an element and just want to get its different namespaced children, say for a structure like this:
<entry>
<title type="text">My test entry</title>
<gd:when startTime="2017-02-26T02:00:00Z" endTime="2017-02-26T03:00:00Z"/>
<gc:notes type="string">A type</gc:notes>
</entry>
Is to send TRUE as the second parameter to the children function:
$title = (string) $entry->title;
$gd = $entry->children('gd', TRUE);
$attrs = $gd->when->attributes();
$startTime = (string) $attrs->startTime;
$gc = $entry->children('gc', TRUE);
$notes = (string) $gc->notes();
here alternative that worked for me.
$xml = simplexml_load_string($r);
$ns = $xml->getNamespaces(true);
foreach ($xml->children($ns['event'])->event as $skey) {
$sessionKey = $skey->children($ns['event'])->sessionKey;
echo $sessionKey;
}
Another approach is to use SimpleXML for parsing and DOMDocument for manipulation/access, which bypasses namespacing issues altogether:
$xml = new SimpleXMLElement($r);
$xml = dom_import_simplexml($xml);
$nodelist= $xml->getElementsByTagName('event');
for($i = 0; $i < $nodelist->length; $i++) {
$sessions = $nodelist->item($i)->getElementsByTagName('sessionKey');
echo $sessions->item(0)->nodeValue;
}
Using registerXPathNamespace and then calling xpath didn't actually work for me.
I had to go with the solution provided in this great post : http://blog.preinheimer.com/index.php?/archives/172-SimpleXML,-Namespaces-Hair-loss.html
So in your case, this :
echo $xml->children('http://www.webex.com/schemas/2002/06/service/event')->sessionName;
Will output:
Learn QB in Minutes

XML attributes and PHP [duplicate]

This question already has answers here:
Reference - How do I handle Namespaces (Tags and Attributes with a Colon in their Name) in SimpleXML?
(2 answers)
Closed 1 year ago.
I have this as xml:
<root xmlns:event="http://www.webex.com/schemas/2002/06/service/event">
<event:event>
<event:sessionKey></event:sessionKey>
<event:sessionName>Learn QB in Minutes</event:sessionName>
<event:sessionType>9</event:sessionType>
<event:hostWebExID></event:hostWebExID>
<event:startDate>02/12/2009</event:startDate>
<event:endDate>02/12/2009</event:endDate>
<event:timeZoneID>11</event:timeZoneID>
<event:duration>30</event:duration>
<event:description></event:description>
<event:status>NOT_INPROGRESS</event:status>
<event:panelists></event:panelists>
<event:listStatus>PUBLIC</event:listStatus>
</event:event>
...
</root>
How can I loop through all of the event:event nodes and display, for example, all of the event:SessionKey's?
This does not work:
$xml = new SimpleXMLElement($r);
$xml->registerXPathNamespace('e', 'http://www.webex.com/schemas/2002/06/service/event');
foreach($xml->xpath('//e:event') as $event) {
var_export($event->xpath('//e:sessionKey'));
}
it does work without registerXPathNamespace and the full namespace prefix in the xpath queries:
$xml = new SimpleXMLElement($r);
foreach($xml->xpath('//event:event') as $event) {
var_export($event->xpath('event:sessionKey'));
}
You have to register the namespace for each simpleXMLElement object you use.
$xml = new SimpleXMLElement($r);
$xml->registerXPathNamespace('e', 'http://www.webex.com/schemas/2002/06/service/event');
foreach($xml->xpath('//e:event') as $event) {
$event->registerXPathNamespace('e', 'http://www.webex.com/schemas/2002/06/service/event');
var_export($event->xpath('//e:sessionKey'));
}
The namespace should also be declared somewhere in the xml file.
<event:event xmlns:event="http://www.webex.com/schemas/2002/06/service/event">
...
The method ax described works too. You can skip the registerXPathNamespace if you know the xml file will always use the same prefix.
Having worked a lot with simplexml, this is how I do it.
The magic trick if you already have an element and just want to get its different namespaced children, say for a structure like this:
<entry>
<title type="text">My test entry</title>
<gd:when startTime="2017-02-26T02:00:00Z" endTime="2017-02-26T03:00:00Z"/>
<gc:notes type="string">A type</gc:notes>
</entry>
Is to send TRUE as the second parameter to the children function:
$title = (string) $entry->title;
$gd = $entry->children('gd', TRUE);
$attrs = $gd->when->attributes();
$startTime = (string) $attrs->startTime;
$gc = $entry->children('gc', TRUE);
$notes = (string) $gc->notes();
here alternative that worked for me.
$xml = simplexml_load_string($r);
$ns = $xml->getNamespaces(true);
foreach ($xml->children($ns['event'])->event as $skey) {
$sessionKey = $skey->children($ns['event'])->sessionKey;
echo $sessionKey;
}
Another approach is to use SimpleXML for parsing and DOMDocument for manipulation/access, which bypasses namespacing issues altogether:
$xml = new SimpleXMLElement($r);
$xml = dom_import_simplexml($xml);
$nodelist= $xml->getElementsByTagName('event');
for($i = 0; $i < $nodelist->length; $i++) {
$sessions = $nodelist->item($i)->getElementsByTagName('sessionKey');
echo $sessions->item(0)->nodeValue;
}
Using registerXPathNamespace and then calling xpath didn't actually work for me.
I had to go with the solution provided in this great post : http://blog.preinheimer.com/index.php?/archives/172-SimpleXML,-Namespaces-Hair-loss.html
So in your case, this :
echo $xml->children('http://www.webex.com/schemas/2002/06/service/event')->sessionName;
Will output:
Learn QB in Minutes

How to load a xml file in php so that i can use xpath on it?

I have a problem with php,
If I implement this code below then nothing will be happen.
$filename = "/opt/olat/olatdata/bcroot/course/85235053647606/runstructure.xml";
if (file_exists($filename)) {
$xml = simplexml_load_file($filename, 'SimpleXMLElement', LIBXML_NOCDATA);
// $xpath = new DOMXPath($filename);
}
$doc = new DOMDocument();
$doc->loadXML($xml);
$xpath = new DOMXpath($doc);
$res = $xpath->query('/org.olat.course.Structure/rootNode/children/org.olat.course.nodes.STCourseNode/shortTitle');
foreach ($res as $entry) {
echo "{$entry->nodeValue}<br/>";
}
If I change the contents of $xml in the content with the content of the $filename
$xml = '<org.olat.course.Structure><rootNode class="org.olat.course.nodes.STCourseNode"> ... ';
then it works, so i think that there is something wrong with loading methode of the xml file,
I've also tried to load the xml file as a Domdocument but it won't work neither.
And in both cases, it does work if I collect xml data via xml
for example this works
echo $Course_name = $xml->rootNode->longTitle;
loadXML takes a string as input, not the return value of simplexml_load_file. Just use file_get_contents to get the (full) contents of a file as string

Parsing xml data from website

I want to be able to retrieve the data on a certain site with the font size="6"
I plan on doing this with the xml parser but so far have had no luck. This is my code, if anyone knows where my mistake is, it would be much appreciated.
Thanks
#$doc=new DOMDocument();
#$doc->loadHTML($html4);
$xml=simplexml_import_dom($doc); // just to make xpath more simple
$data=$xml->xpath('//font size=6');
$arr= array();
foreach ($data as $img) {
echo $img;
}
Something like:
$doc = new DOMDocument();
$doc->loadHTML($html4);
$xpath = new DOMXpath($doc);
$data = $xpath->query("*/font[#size='6']");

Parse XML with Namespace using SimpleXML [duplicate]

This question already has answers here:
Reference - How do I handle Namespaces (Tags and Attributes with a Colon in their Name) in SimpleXML?
(2 answers)
Closed 1 year ago.
I have this as xml:
<root xmlns:event="http://www.webex.com/schemas/2002/06/service/event">
<event:event>
<event:sessionKey></event:sessionKey>
<event:sessionName>Learn QB in Minutes</event:sessionName>
<event:sessionType>9</event:sessionType>
<event:hostWebExID></event:hostWebExID>
<event:startDate>02/12/2009</event:startDate>
<event:endDate>02/12/2009</event:endDate>
<event:timeZoneID>11</event:timeZoneID>
<event:duration>30</event:duration>
<event:description></event:description>
<event:status>NOT_INPROGRESS</event:status>
<event:panelists></event:panelists>
<event:listStatus>PUBLIC</event:listStatus>
</event:event>
...
</root>
How can I loop through all of the event:event nodes and display, for example, all of the event:SessionKey's?
This does not work:
$xml = new SimpleXMLElement($r);
$xml->registerXPathNamespace('e', 'http://www.webex.com/schemas/2002/06/service/event');
foreach($xml->xpath('//e:event') as $event) {
var_export($event->xpath('//e:sessionKey'));
}
it does work without registerXPathNamespace and the full namespace prefix in the xpath queries:
$xml = new SimpleXMLElement($r);
foreach($xml->xpath('//event:event') as $event) {
var_export($event->xpath('event:sessionKey'));
}
You have to register the namespace for each simpleXMLElement object you use.
$xml = new SimpleXMLElement($r);
$xml->registerXPathNamespace('e', 'http://www.webex.com/schemas/2002/06/service/event');
foreach($xml->xpath('//e:event') as $event) {
$event->registerXPathNamespace('e', 'http://www.webex.com/schemas/2002/06/service/event');
var_export($event->xpath('//e:sessionKey'));
}
The namespace should also be declared somewhere in the xml file.
<event:event xmlns:event="http://www.webex.com/schemas/2002/06/service/event">
...
The method ax described works too. You can skip the registerXPathNamespace if you know the xml file will always use the same prefix.
Having worked a lot with simplexml, this is how I do it.
The magic trick if you already have an element and just want to get its different namespaced children, say for a structure like this:
<entry>
<title type="text">My test entry</title>
<gd:when startTime="2017-02-26T02:00:00Z" endTime="2017-02-26T03:00:00Z"/>
<gc:notes type="string">A type</gc:notes>
</entry>
Is to send TRUE as the second parameter to the children function:
$title = (string) $entry->title;
$gd = $entry->children('gd', TRUE);
$attrs = $gd->when->attributes();
$startTime = (string) $attrs->startTime;
$gc = $entry->children('gc', TRUE);
$notes = (string) $gc->notes();
here alternative that worked for me.
$xml = simplexml_load_string($r);
$ns = $xml->getNamespaces(true);
foreach ($xml->children($ns['event'])->event as $skey) {
$sessionKey = $skey->children($ns['event'])->sessionKey;
echo $sessionKey;
}
Another approach is to use SimpleXML for parsing and DOMDocument for manipulation/access, which bypasses namespacing issues altogether:
$xml = new SimpleXMLElement($r);
$xml = dom_import_simplexml($xml);
$nodelist= $xml->getElementsByTagName('event');
for($i = 0; $i < $nodelist->length; $i++) {
$sessions = $nodelist->item($i)->getElementsByTagName('sessionKey');
echo $sessions->item(0)->nodeValue;
}
Using registerXPathNamespace and then calling xpath didn't actually work for me.
I had to go with the solution provided in this great post : http://blog.preinheimer.com/index.php?/archives/172-SimpleXML,-Namespaces-Hair-loss.html
So in your case, this :
echo $xml->children('http://www.webex.com/schemas/2002/06/service/event')->sessionName;
Will output:
Learn QB in Minutes

Categories