Jump to content

Trouble getting element values out of XML with namespaces


Go to solution Solved by requinix,

Recommended Posts

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.

 

 

 

 

 

 

 

 

  • Solution

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"
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.