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

Using SimpleXMLElement::registerXPathNamespace - test case attached

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

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

这是我的 test.php 文件:

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

?>

它并没有像预期的那样真正工作,只打印

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

如果我取消第 1 行的注释,那么它什么都不打印。

如果我取消注释第 2 行,那么它会打印:

object(SimpleXMLElement)#2 (0) {
}

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

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

?>

打印我需要的数据:

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

欢迎任何建议(可能使其更健壮)和改进

宽高是属性,不是子节点。您正在访问它们,就像它们是子节点一样 $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 似乎显示空对象,但实际上对象包含正确的数据。