使用SimpleXMLElement :: registerXPathNamespace - 附加测试用例

时间:2015-01-30 19:28:42

标签: php xml-parsing simplexml

我为我的问题准备了一个简单的测试用例 - 请在php -f test.php的命令行上运行它,你会看到我的问题。

我正在尝试在Google地图中绘制一个矩形区域,为此我需要提取widthheightlongitudelatitude和{{1}来自以下XML代码。

这是我的test.php文件:

ZoneType

它并没有按预期工作,只打印

<?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
}

?>

如果我解开第1行,那么它什么都不打印。

如果我取消注释第2行,则打印出来:

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

更新:借助MrCode的帮助(谢谢!),这是我的代码:

object(SimpleXMLElement)#2 (0) {
}

打印我需要的数据:

<?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']);
}

?>

欢迎任何建议(可能使其更加强大)和改进

1 个答案:

答案 0 :(得分:1)

宽度和高度是属性而不是子节点。您正在访问它们,就好像它们是子节点$rect->width一样。您可以这样访问它们:

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);
}

在矩形节点上调用attributes()方法,传递ns2命名空间的值。

注意:在SimpleXML元素上执行var_dumpprint_r并不总是显示完整的内部结构。您的第一个var_dump似乎显示空对象,但实际上对象包含正确的数据。