2012-06-07 162 views
1

我一直在打我的頭撞在牆上試圖在第一XS下面解析出項目:順序(從由assetid):解析XML命名空間PHP

<Response> 
    <xs:schema id="Response" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"> 
    <xs:element name="Response" msdata:IsDataSet="true" msdata:UseCurrentLocale="true"> 
     <xs:complexType> 
     <xs:choice minOccurs="0" maxOccurs="unbounded"> 
      <xs:element name="Server"> 
      <xs:complexType> 
       <xs:sequence> 
       <xs:element name="AssetID" type="xs:int" minOccurs="0" /> 
       <xs:element name="Asset_x0020_Number" type="xs:int" minOccurs="0" /> 
       <xs:element name="Asset_x0020_Name" type="xs:string" minOccurs="0" /> 
       <xs:element name="Tag" type="xs:string" minOccurs="0" /> 
       <xs:element name="Server_x0020_Material" type="xs:string" minOccurs="0" /> 
       <xs:element name="Cabinet_x0020_Name" type="xs:string" minOccurs="0" /> 
       <xs:element name="Server_x0020_Project_x0020_Purpose" type="xs:string" minOccurs="0" /> 
       <xs:element name="Tranche_x0020_Number" type="xs:int" minOccurs="0" /> 
       <xs:element name="Strategy" type="xs:string" minOccurs="0" /> 
       <xs:element name="Total_x0020_System_x0020_U_x0020_Height" type="xs:int" minOccurs="0" /> 
       <xs:element name="Cabinet_x0020_U_x0020_Number" type="xs:int" minOccurs="0" /> 
       <xs:element name="Mounting_x0020_Direction" type="xs:string" minOccurs="0" /> 
       <xs:element name="Serial_x0020_Number" type="xs:string" minOccurs="0" /> 
       <xs:element name="Purchase_x0020_Price" type="xs:decimal" minOccurs="0" /> 
       <xs:element name="Location_x0020_Group" type="xs:string" minOccurs="0" /> 
       <xs:element name="Row" type="xs:string" minOccurs="0" /> 
       <xs:element name="Column" type="xs:string" minOccurs="0" /> 
       <xs:element name="Rotation" type="xs:int" minOccurs="0" /> 
       <xs:element name="AssetOperationalStatus" type="xs:string" minOccurs="0" /> 
       <xs:element name="Discovered" type="xs:boolean" minOccurs="0" /> 
       <xs:element name="Power_x0020_State" type="xs:string" minOccurs="0" /> 
       </xs:sequence> 
      </xs:complexType> 
      </xs:element> 
      <xs:element name="Strategy"> 
      <xs:complexType> 
      [...continues on...] 

我一直想這個使用PHP的SimpleXMLElement,但我無法弄清楚如何隔離這20行。 所有我需要的是元素的名稱的數組,例如:

{"AssetID", "Asset_x0020_Number","Asset_x0020_Tag","Tag", ... ,"Power_x0020_State"} 

回答

2

下面是一個例子,用於顯示每個XS的「名稱」屬性:元素

$xml = simplexml_load_string($xmlString); 
$namespaces = $xml->getNameSpaces(true); 
$xs = $xml->children($namespaces['xs']); 

foreach($xs->schema->element->complexType->choice->element->complexType->sequence->element as $element) { 
    echo $element->attributes()->name.PHP_EOL; 
} 
+0

真棒!謝謝,蒙戈。 XML似乎比它更困難,但這對於看到結構有很大的幫助。 – AaplMike