desmod Posted November 7, 2013 Share Posted November 7, 2013 Hello, I am going crazy trying to get the element values from XML file which contains elements with namespaces. Here is the XML file (test.xml) <?xml version = "1.0" encoding = "UTF-8"?> <ipdr:IPDRDoc xmlns:ipdr="http://www.ipdr.org/namespaces/ipdr" xmlns="urn:broadband-forum-org:ipdr:tr-232-1-0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:broadband-forum-org:ipdr:tr-232-1-0 tr-232-1-0-0-serviceSpec.xsd http://www.ipdr.org/namespaces/ipdr http://www.ipdr.org/public/IPDRDoc3.5.1.xsd" docId="74697373-6f74-7878-7878-746973736f74" creationTime="2013-06-11T05:52:55.153Z" IPDRRecorderInfo="IPDR Collector" version="3.5.1"> <ipdr:IPDR xsi:type="BulkDataReport"> <OUI>124BEB</OUI> <ProductClass>BGW</ProductClass> <SerialNumber>1234567890</SerialNumber> <Suspect>1</Suspect> <BulkData> <Name>InternetGatewayDevice.DeviceInfo.UpTime</Name> <Value>1449</Value> </BulkData> <BulkData> <Name>InternetGatewayDevice.ManagementServer.URL</Name> <Value>www.somesite.com</Value> </BulkData> </ipdr:IPDR > <ipdr:IPDRDoc.End count="1" endTime="2013-06-11T05:52:55.207Z"></ipdr:IPDRDoc.End> </ipdr:IPDRDoc> I am trying to get element values for BulkData Here is the code I am using and having problem with: $cpe = simplexml_load_file('test.xml'); $sxe=new SimpleXMLElement($cpe); $sxe->registerXPathNamespace('ipdr','http://www.ipdr.org/namespaces/ipdr'); $result0=$sxe->xpath('ipdr:IPDR->BulkData[0]')->Value; $result1=$sxe->xpath('ipdr:IPDR->BulkData[1]')->Value; echo $result0 ' <br> ' $result1; The result I am looking for should be : 1449 www.somesite.com Thanks for any help you can offer. Quote Link to comment https://forums.phpfreaks.com/topic/283703-trouble-getting-element-values-out-of-xml-with-namespaces/ Share on other sites More sharing options...
Solution requinix Posted November 7, 2013 Solution Share Posted November 7, 2013 Side comment: rather than going by [0] and [1] to get the nodes you want, use a condition to match against the specific Name. So there's two namespaces to care about: the default "" (urn:broadband-forum-org:ipdr:tr-232-1-0) and "ipdr" (http://www.ipdr.org/namespaces/ipdr). Since you don't need to worry about where the BulkData appears in the XML (it's only in one place) you don't need to try to form a complete hierarchy to reach one and you can jump directly to the BulkData nodes directly. 1. Register the default namespace with any name $sxe->registerXPathNamespace('ns', 'urn:broadband-forum-org:ipdr:tr-232-1-0');2. To get to a node in XPath regardless of hierarchy, use //node '//ns:BulkData'3. Use a condition to target a specific node. Let's start with the uptime: '//ns:BulkData[ns:Name="InternetGatewayDevice.DeviceInfo.UpTime"]'4. Having found the BulkData you want, grab the Value node beneath it. '//ns:BulkData[ns:Name="InternetGatewayDevice.DeviceInfo.UpTime"]/ns:Value'As an example of the full process, $xml = new SimpleXMLElement(<<<XML <?xml version = "1.0" encoding = "UTF-8"?> <ipdr:IPDRDoc xmlns:ipdr="http://www.ipdr.org/namespaces/ipdr" xmlns="urn:broadband-forum-org:ipdr:tr-232-1-0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:broadband-forum-org:ipdr:tr-232-1-0 tr-232-1-0-0-serviceSpec.xsd http://www.ipdr.org/namespaces/ipdr http://www.ipdr.org/public/IPDRDoc3.5.1.xsd" docId="74697373-6f74-7878-7878-746973736f74" creationTime="2013-06-11T05:52:55.153Z" IPDRRecorderInfo="IPDR Collector" version="3.5.1"> <ipdr:IPDR xsi:type="BulkDataReport"> <OUI>124BEB</OUI> <ProductClass>BGW</ProductClass> <SerialNumber>1234567890</SerialNumber> <Suspect>1</Suspect> <BulkData> <Name>InternetGatewayDevice.DeviceInfo.UpTime</Name> <Value>1449</Value> </BulkData> <BulkData> <Name>InternetGatewayDevice.ManagementServer.URL</Name> <Value>www.somesite.com</Value> </BulkData> </ipdr:IPDR > <ipdr:IPDRDoc.End count="1" endTime="2013-06-11T05:52:55.207Z"></ipdr:IPDRDoc.End> </ipdr:IPDRDoc> XML , 0, false); $xml->registerXPathNamespace("ns", "urn:broadband-forum-org:ipdr:tr-232-1-0"); $value = $xml->xpath("//ns:BulkData[ns:Name='InternetGatewayDevice.DeviceInfo.UpTime']/ns:Value"); var_dump($value); // array(1) { // [0]=> // object(SimpleXMLElement)#2 (1) { // [0]=> // string(4) "1449" // } // } var_dump((string)$value[0]); // string(4) "1449" Quote Link to comment https://forums.phpfreaks.com/topic/283703-trouble-getting-element-values-out-of-xml-with-namespaces/#findComment-1457448 Share on other sites More sharing options...
desmod Posted November 7, 2013 Author Share Posted November 7, 2013 Amazing! Thank you so much! Quote Link to comment https://forums.phpfreaks.com/topic/283703-trouble-getting-element-values-out-of-xml-with-namespaces/#findComment-1457463 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.