Search code examples
phpxml-parsingsimplexml

Using SimpleXMLElement::registerXPathNamespace - test case attached


I have prepared a simple test case for my question - please run it on a command line with php -f test.php and you will see my problem.

I am trying to draw a rectangle area in Google maps and for that I need to extract the width, height, longitude, latitude and ZoneType from the following XML code.

Here is my test.php file:

<?php

$XML1 =<<< XML1
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns13:Job_ActivateGeoFencing ns12:id="1957"
    xmlns:ns5="http://www.test.com/car2x/complex_types"
    xmlns:ns2="http://www.test.com/car2x/service_geofencing"
    xmlns:ns13="http://www.test.com/car2x/job_request_mechanism"
    xmlns:ns12="http://www.test.com/car2x/jobs">

       <ns2:AreaDefinition_RectangleArea
            ns2:width="668"
            ns2:height="3726"
            ns2:spatial_tolerance="25"
            ns2:rotation_angle="0"
            ns2:debouncing_time="5"
            ns2:area_id="0">
                <ns2:centerpoint ns5:longitude="116499160" ns5:latitude="39991920"/>
                <ns2:ZoneType>GreenZone</ns2:ZoneType>
        </ns2:AreaDefinition_RectangleArea>

</ns13:Job_ActivateGeoFencing>
XML1;

$xml1 = new SimpleXMLElement($XML1);
$xml1->registerXPathNamespace('ns2', 'http://www.test.com/car2x/service_geofencing');
$xml1->registerXPathNamespace('ns5', 'http://www.test.com/car2x/complex_types');
$xml1->registerXPathNamespace('ns13', 'http://www.test.com/car2x/job_request_mechanism');
$xml1->registerXPathNamespace('ns12', 'http://www.test.com/car2x/jobs');

$rects = $xml1->xpath("//ns2:AreaDefinition_RectangleArea");

var_dump($rects);
foreach ($rects as $rect) {
   var_dump($rect);
   #print($rect->width);     # line 1
   #print($rect->{'width'}); # line 2
}

?>

It does not really work as expected and only prints

array(1) {
  [0]=>
  object(SimpleXMLElement)#2 (0) {
  }
}
object(SimpleXMLElement)#2 (0) {
}

If I uncoment the line 1, then it prints nothing.

And if I uncomment the line 2, then it prints:

object(SimpleXMLElement)#2 (0) {
}

UPDATE: With MrCode's help (thanks!) here is my code:

<?php

$XML1 =<<< XML1
... see above ...
XML1;

$xml1 = new SimpleXMLElement($XML1);
$xml1->registerXPathNamespace('ns2',  'http://www.test.com/car2x/service_geofencing');
$xml1->registerXPathNamespace('ns5',  'http://www.test.com/car2x/complex_types');
$xml1->registerXPathNamespace('ns13', 'http://www.test.com/car2x/job_request_mechanism');
$xml1->registerXPathNamespace('ns12', 'http://www.test.com/car2x/jobs');

$rects = $xml1->xpath("//ns2:AreaDefinition_RectangleArea");

foreach ($rects as $rect) {
        $attributes2 = $rect->attributes('ns2', true);
        $children    = $rect->children('ns2', true);
        $centerpoint = $children->centerpoint;
        $attributes5 = $centerpoint->attributes('ns5', true);

        printf("width=%d\n", $attributes2['width']);
        printf("height=%d\n", $attributes2['height']);
        printf("rotation_angle=%d\n", $attributes2['rotation_angle']);
        printf("area_id=%d\n", $attributes2['area_id']);
        printf("ZoneType=%s\n", $children->ZoneType);
        printf("longitude=%d\n", $attributes5['longitude']);
        printf("latitude=%d\n", $attributes5['latitude']);
}

?>

Which prints the data I needed:

# php -f test.php
width=668
height=3726
rotation_angle=0
area_id=0
ZoneType=GreenZone
longitude=116499160
latitude=39991920

Any advices (maybe to make it more robust) and improvements are welcome


Solution

  • The width and height are attributes not child nodes. You're accessing them as if they're child nodes $rect->width. You can access them like so:

    foreach ($rects as $rect) {
       var_dump($rect->attributes('http://www.test.com/car2x/service_geofencing')->width);
       var_dump($rect->attributes('http://www.test.com/car2x/service_geofencing')->height);
    }
    

    The attributes() method is called on the rectangle node, passing the value of ns2 namespace.

    Note: doing var_dump or print_r on SimpleXML elements doesn't always show the full internal structure. Your first var_dump appears to show empty objects, but in actual fact the objects contain the correct data.