2013-12-19 39 views
0

我裏面包含了很多類似線的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")返回一個空值,而它應該給我一個枚舉的列表。

你能幫我解決嗎?

+1

'$ class-> children('property')'應該是'$ class-> property'; 'xpath(「enum」)'應該是'xpath(「//枚舉」)'' – Passerby

回答

2

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

雖然SimpleXMLElement::children()指當前元素的孩子,它的第一個參數是表示命名空間,而不是標籤名稱。

所以$class->children('property')意味着$class在命名空間property檢查子元素。

你可以使用$class->property檢索這個類裏面的所有<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

請試試這個代碼打印您的數據,並在循環中添加條件,

$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/>"; 
} 
相關問題