【发布时间】:2013-12-19 09:32:20
【问题描述】:
我有一个 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") 行也返回一个空值,而它应该给我一个枚举列表。
你能帮我解决它吗?
【问题讨论】:
-
$class->children('property')应该是$class->property;xpath("enum")应该是xpath("//enum")
标签: php xml xpath xml-parsing