2012-01-11 177 views
-1

嗨,我試圖用simplexml解析XML。問題是我無法訪問分類,有什麼想法爲什麼?用simplexml解析XML

PHP代碼:

$xml = simplexml_load_file($source); 
foreach($xml->children() as $node) { 
    foreach ($node->AttrList->attributes() as $attribute) { 
      print_r ($attribute); 
    } 
} 

XML的海賊王:

<ProductCatalog> 
    <Product> 
    <ProductCode>ST905003FND2E1-RK</ProductCode> 
    <Vendor>SEAGATE</Vendor> 
    <ProductType>HDD External</ProductType> 
    <ProductCategory>Cietie diski</ProductCategory> 
    <ProductDescription>HDD External SEAGATE FreeAgent Go 5400.2 (2.5",500GB,8MB cache,USB 2.0,FreeAgent software) Black</ProductDescription> 
    <Image>https://www.it4profit.com/catalogimg/wic/1/ST905003FND2E1-RK</Image> 
    <ProductCard>https://content.it4profit.com/itshop/itemcard_cs.jsp?ITEM=90728055033221913&amp;THEME=asbis&amp;LANG=lv</ProductCard> 
    <AttrList> 
     <element Name="Device Location" Value="External"/> 
     <element Name="Hard Drive Type" Value="Portable"/> 
     <element Name="Form Factor" Value="2.5&quot;"/> 
     <element Name="Storage Capacity" Value="500000 MB"/> 
     <element Name="Supports Data Channel" Value="USB 2.0"/> 
     <element Name="Installed Cache Memory Storage Capacity" Value="8 MB"/> 
     <element Name="Sector Capacity" Value="512 B"/> 
     <element Name="Rotational Speed" Value="5400 rpm"/> 
     <element Name="External Data Bit Rate" Value="480 Mbps (Max)"/> 
     <element Name="USB2.0 Interface Quantity" Value="1"/> 
     <element Name="Platform Compability" Value="PC"/> 
     <element Name="Software Included" Value="FreeAgent software"/> 
     <element Name="Requires Operating System" Value="Microsoft Windows XP, Microsoft Windows Vista"/> 
     <element Name="Requires Peripheral Devices" Value="USB 2.0 port"/> 
     <element Name="Cable Included" Value="USB Cable"/> 
     <element Name="External Color" Value="Black"/> 
     <element Name="Case Material" Value="Plastic"/> 
     <element Name="Depth" Value="146.1 mm"/> 
     <element Name="Height" Value="177.8 mm"/> 
     <element Name="Width" Value="50.8 mm"/> 
     <element Name="Nominal Weight" Value="0.3 kg"/> 
     <element Name="Warranty Products returnable" Value="Yes"/> 
     <element Name="Warranty Term (month)" Value="60 month"/> 
     <element Name="Warranty validation Criteria" Value="Serial Number"/> 
     <element Name="Box Depth (mm)" Value="196 mm"/> 
     <element Name="Box Height (mm)" Value="501 mm"/> 
     <element Name="Box Weight Brutto (kg)" Value="4 kg"/> 
     <element Name="Box Width (mm)" Value="297 mm"/> 
     <element Name="Pack Depth (mm)" Value="178 mm"/> 
     <element Name="Pack Height (mm)" Value="178 mm"/> 
     <element Name="Pack Weight Brutto (kg)" Value="0.57 kg"/> 
     <element Name="Pack Width (mm)" Value="89 mm"/> 
     <element Name="Packs in Box" Value="6"/> 
     <element Name="Pieces in pack" Value="1"/> 
     <element Name="EAN Code" Value="7636490016813"/> 
    </AttrList> 
    <MarketingInfo> 
     <element>The ultimate portable storage solution with the world㠔s first hard drive docking station for easy access to all your stuff. Sleek, ultra-thin design that㠔s as stylish as it is striking. &lt;br&gt;&lt;br&gt; &lt;ul&gt;&#13; 
&lt;li&gt;Store photos, music, and other files&#13; 
&lt;li&gt;Carry your data anywhere you want&#13; 
&lt;li&gt;Sync data between computers&#13; 
&lt;li&gt;Back up files using the optional dock&#13; 
&lt;/ul&gt;</element> 
    </MarketingInfo> 
    <Images> 
     <Image>https://content.it4profit.com/pimg/s/resize/160x160x160x160/90818105858024672.jpg</Image> 
     <Image>https://content.it4profit.com/pimg/s/resize/400x300x400x300/90818105819015057.jpg</Image> 
     <Image>https://content.it4profit.com/pimg/s/resize/200x150x200x150/90818105848608387.jpg</Image> 
     <Image>https://content.it4profit.com/pimg/s/resize/75x56x75x56/90818105909870954.jpg</Image> 
     <Image>https://content.it4profit.com/pimg/s/resize/362x362x362x362/90818105833910289.jpg</Image> 
     <Image>https://content.it4profit.com/pimg/s/resize/260x195x260x195/90818105841219714.jpg</Image> 
    </Images> 
    </Product> 
</ProductCatalog> 
+0

AttrList沒有任何屬性,它是有屬性 – 2012-01-11 19:22:24

+0

感謝您的回覆速度快元素,但 的foreach($節點 - > AttrList->元素 - >屬性()爲$屬性)沒有按」也工作。 – di3sel 2012-01-11 19:23:57

+0

查看Tim的回答 – 2012-01-11 19:24:53

回答

6

它不顯示任何東西,因爲<AttrList>節點沒有任何屬性。

您的意思是?

foreach($xml->children() as $node) { 
    foreach ($node->AttrList->children() as $child) { 
     foreach($child->attributes() as $attribute) { 
      echo $attribute->getName() , " - " , $attribute , "\n"; 
     } 
    } 
} 
+0

工程就像一個魅力,非常感謝! :) – di3sel 2012-01-11 19:29:02

1

此外,你已經忘了「元素」的孩子。 所以我測試了下面的代碼適用於我。 我將一些子部件轉換爲數組,因爲我只對simplexml解析對象有一些問題。

foreach ($xml->children() as $node) { 
    $attr_list = (array) $node->AttrList; 
    $elements = ($attr_list['element']); 
    foreach ($elements as $element) { 
     $element = (array) $element; 
     print_r($element['@attributes']); 
    } 
}