我裏面包含了很多類似線的XML文件:Xpath的讓所有的孩子用給定錨
<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(「//枚舉」)'' – Passerby