I got a XML file which contains a lot of lines like that:
<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>
I need to get the class name and all it's properties. I also need to store all the enum values in a different array.
I've made the following code but I can't make it work properly. I can't get the property
children and the enum
list.
$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;
}
}
The line $class->children('property')
returns an empty SimpleXMLElement
. Also the line $this->struct_xml->xpath("enum")
returns a null value whereas it should give me a list of enum.
Could you help me to fix it ?