Xpath获取具有给定锚点的所有子项

时间:2013-12-19 09:32:20

标签: php xml xpath xml-parsing

我有一个XML文件,其中包含很多这样的行:

<class name="CustomerProfileLite" inList="true" >
    <enum name="Order" takeOtherValuesFromProperties="true">
        <value>None</value>
    </enum>
    <property name="Guid" type="guid" />
    <property name="CreationDate" type="datetime" isInEnum="true" />
    <property name="LastUpdateDate" type="datetime" isIndexed="true" isInEnum="true" />
    <property name="Revision" type="int" isIndexed="true" isInEnum="true" />
    <property name="Thumbnail" type="string" convertToRelativePathInDB="true" />
    <property name="UmsJob" type="string" isIndexed="true"/>
    <property name="FirstName" type="string" isIndexed="true" isInEnum="true" />
    <property name="LastName" type="string" isIndexed="true" isInEnum="true" />
    <property name="Address" type="string" isInEnum="true" />
    <property name="City" type="string" isInEnum="true" />
    <property name="PhoneNumber" type="string" isIndexed="true" isInEnum="true" />
    <property name="CellPhoneNumber" type="string" isIndexed="true" isInEnum="true" />
    <property name="Birthdate" type="OptionalDateTime" isInEnum="true" />
    <property name="HasFrames" type="bool" />
    <property name="LatestEquipementHasFarVisionBoxings" type="bool" />
    <property name="LatestEquipementHasFarVisionImages" type="bool" />
    <property name="LatestEquipementHasSplines" type="bool" />
    <property name="LatestEquipementHasIpadMeasure" type="bool" />
    <sattribute name="IsModified" type="bool" />
    <sattribute name="LastModificationDate" type="datetime" />
</class>

我需要获取类名及其所有属性。我还需要将所有枚举值存储在不同的数组中。

我已经制作了以下代码,但我无法使其正常工作。我无法获得property个孩子和enum列表。

$this->struct_xml =  simplexml_load_file("assets/archi.xml");
$archi = array();
$types = $this->struct_xml->xpath("type");
foreach ($types as $type) {
    $name = (string) $type['name'];
    $archi[$name] = array();

    if ((bool) $type['isComplexType']) {
        $class = first($this->struct_xml->xpath("class[@name='$name']"));
        if (!$class) throw new Exception("Unknown type found $name !");

        foreach ($class->children('property') as $property) {
            $archi[$name]['children'][] = array(
                'name' => (string) $property['name'],
                'type' => (string) $property['type'],
            );
        }
    }
}

$enums = $this->struct_xml->xpath("enum");
foreach ($enums as $enum) {
    $name = (string) $type['name'];
    $archi[$name] = array();
    foreach ($enum->children() as $value) {
        $archi[$name]['values'][] = $value;
    }
}

$class->children('property')返回空SimpleXMLElement。行$this->struct_xml->xpath("enum")也返回一个空值,而它应该给我一个枚举列表。

你能帮我解决一下吗?

3 个答案:

答案 0 :(得分:2)

  

$class->children('property')行返回空SimpleXMLElement

虽然SimpleXMLElement::children()指的是当前元素的子元素,但它的第一个参数是指示命名空间,而不是标记名称。

所以$class->children('property')表示检查$class位于名称空间property 中的子元素。

您可以使用$class->property来检索&#34;列表&#34;该类中的所有<property>标记;


  

$this->struct_xml->xpath("enum")返回一个空值,而它应该给我一个枚举列表

XPath与HTML DOM中的document.getElementByTagName不同。你必须至少指定标签是某个东西的孩子。懒惰形式为xpath("//enum")


<强> Here is a simple demo

$dom=simplexml_load_file("xml");
foreach($dom->xpath("//class") as $class)
{
    echo (string)$class["name"];
    echo "\n";
    foreach($class->property as $property)
    {
        echo (string)$property["name"];
        echo "\t";
        echo (string)$property["type"];
        echo "\n";
    }
}
foreach($dom->xpath("//enum") as $enum)
{
    echo (string)$enum["name"];
    echo "\t";
    echo (string)$enum->value;
}

输出:

CustomerProfileLite
Guid    guid
CreationDate    datetime
LastUpdateDate  datetime
Revision    int
...
Order   None

答案 1 :(得分:1)

SimpleXML :: children的参数用于获取具有特定名称空间前缀的子项。您必须直接指向节点。例如:

$class->property->children()

有关命名空间和前缀的更多信息,请查看http://www.w3schools.com/xml/xml_namespaces.asp

答案 2 :(得分:1)

请尝试使用此代码打印您的数据,并在该循环中添加条件

$this_struct_xml =  simplexml_load_file("archi.xml"); //$this->struct_xml = $struct_xml;
foreach($this_struct_xml->property as $property) { 
    echo "name:".$property->attributes()->name." / ";
    echo "type:".$property->attributes()->type." / ";
    echo "isInEnum:".$property->attributes()->isInEnum." / ";
    echo "isIndexed:".$property->attributes()->isIndexed."<br/>";
}

echo "-------------------------------<br/>";

foreach($this_struct_xml->enum as $enum) { 
    echo "name:".$enum->attributes()->name." / ";
    echo "type:".$enum->attributes()->takeOtherValuesFromProperties." / ";
    echo "Value:".$enum->value."<br/>";
}