2017-10-09 143 views
1

我會創建一個函數(with,depth ...)來獲取xml中的一些元素。 我的循環提取信息時出現問題。如何提取特定的xml信息

謝謝

例如:

echo $IceCatByEan->getSpecByName('width'); // size 
    echo $IceCatByEan->getSpecByName('depth'); // size 
    echo $IceCatByEan->getSpecByName('dimension_type'); // mn, cm 

ceCatByEan-> getSpecByName( 'DIMENSION_TYPE'); // mn個

功能

// specificationName (like with, depth, dimension_type) 

public function getSpecByName($specificationName) { 
    $xml = $this->getSearchProductEanXML(); 
    $spec_item = $xml->xpath("//ProductFeature"); 

    foreach ($spec_item as $feature) { 
     ... 
    } 
    return $element; 
} 

陣列呈現

[特徵] =>數組 ( [44095] =>數組 ( [名稱] =>堆疊寬度 [值] => 2453.64 [sign] => mm [pres_value] => 2453.6 mm ) )

XML文件

<ProductFeature Localized="0" ID="241810932" Local_ID="0" Value="2192.02" CategoryFeature_ID="87011" CategoryFeatureGroup_ID="10073" No="100025" Presentation_Value="2192 mm" Translated="0" Mandatory="1" Searchable="0"> 
    <LocalValue Value="2192"> 
    <Measure ID="24"> 
    <Signs><Sign ID="3" langid="1"><![CDATA[mm]]></Sign></Signs> 
    </Measure> 
</LocalValue> 
    <Feature ID="1649"> 
    <Measure ID="24" Sign=""> 
     <Signs><Sign ID="3" langid="1"><![CDATA[mm]]></Sign></Signs> 
    </Measure> 
    <Name ID="5143" langid="1" Value="Width"/> 
    </Feature> 
</ProductFeature> 

<ProductFeature Localized="0" ID="241810955" Local_ID="0" Value="71.12" CategoryFeature_ID="87012" CategoryFeatureGroup_ID="10073" No="100024" Presentation_Value="71.1 mm" Translated="0" Mandatory="1" Searchable="0"> 
    <LocalValue Value="71.1"> 
    <Measure ID="24"> 
    <Signs><Sign ID="3" langid="1"><![CDATA[mm]]></Sign></Signs> 
    </Measure> 
</LocalValue> 
    <Feature ID="1650"> 
    <Measure ID="24" Sign=""> 
     <Signs><Sign ID="3" langid="1"><![CDATA[mm]]></Sign></Signs> 
    </Measure> 
    <Name ID="5145" langid="1" Value="Depth"/> 
    </Feature> 
</ProductFeature> 

<ProductFeature Localized="0" ID="241810944" Local_ID="0" Value="1249.68" CategoryFeature_ID="87013" CategoryFeatureGroup_ID="10073" No="100023" Presentation_Value="1249.7 mm" Translated="0" Mandatory="1" Searchable="0"> 
    <LocalValue Value="1249.7"> 
    <Measure ID="24"> 
    <Signs><Sign ID="3" langid="1"><![CDATA[mm]]></Sign></Signs> 
    </Measure> 
</LocalValue> 
    <Feature ID="1464"> 
    <Measure ID="24" Sign=""> 
     <Signs><Sign ID="3" langid="1"><![CDATA[mm]]></Sign></Signs> 
    </Measure> 
    <Name ID="4625" langid="1" Value="Height"/> 
    </Feature> 
</ProductFeature> 

<ProductFeature Localized="0" ID="241811006" Local_ID="0" Value="2453.64" CategoryFeature_ID="87036" CategoryFeatureGroup_ID="10078" No="99989" Presentation_Value="2453.6 mm" Translated="0" Mandatory="0" Searchable="0"> 
    <LocalValue Value="2453.6"> 
    <Measure ID="24"> 
    <Signs><Sign ID="3" langid="1"><![CDATA[mm]]></Sign></Signs> 
    </Measure> 
</LocalValue> 
    <Feature ID="3808"> 
    <Measure ID="24" Sign=""> 
     <Signs><Sign ID="3" langid="1"><![CDATA[mm]]></Sign></Signs> 
    </Measure> 
    <Name ID="44095" langid="1" Value="Package width"/> 
    </Feature> 
</ProductFeature> 

<ProductFeature Localized="0" ID="241811027" Local_ID="0" Value="604.52" CategoryFeature_ID="87037" CategoryFeatureGroup_ID="10078" No="99988" Presentation_Value="604.5 mm" Translated="0" Mandatory="0" Searchable="0"> 
    <LocalValue Value="604.5"> 
    <Measure ID="24"> 
    <Signs><Sign ID="3" langid="1"><![CDATA[mm]]></Sign></Signs> 
    </Measure> 
</LocalValue> 
    <Feature ID="3806"> 
    <Measure ID="24" Sign=""> 
     <Signs><Sign ID="3" langid="1"><![CDATA[mm]]></Sign></Signs> 
    </Measure> 
    <Name ID="44061" langid="1" Value="Package depth"/> 
    </Feature> 
</ProductFeature> 

<ProductFeature Localized="0" ID="241811019" Local_ID="0" Value="1480.82" CategoryFeature_ID="87038" CategoryFeatureGroup_ID="10078" No="99987" Presentation_Value="1480.8 mm" Translated="0" Mandatory="0" Searchable="0"> 
    <LocalValue Value="1480.8"> 
    <Measure ID="24"> 
    <Signs><Sign ID="3" langid="1"><![CDATA[mm]]></Sign></Signs> 
    </Measure> 
</LocalValue> 
    <Feature ID="3807"> 
    <Measure ID="24" Sign=""> 
     <Signs><Sign ID="3" langid="1"><![CDATA[mm]]></Sign></Signs> 
    </Measure> 
    <Name ID="44078" langid="1" Value="Package height"/> 
    </Feature> 
</ProductFeature> 

+0

這是一個有效的XML文件嗎? 預期的結果/輸出是什麼? – DanielO

+0

這個例子中,xml非常長。 resut很簡單,我想提取信息以獲得深度的大小,深度的度量。大小將包括在數據庫內,也可以測量 – yokogeri

回答

1

的方式,我會檢索各個值是使用XPath和直接提取每個值。

<?php 
error_reporting (E_ALL); 
ini_set ('display_errors', 1); 

$xml = simplexml_load_file("t1.xml"); 

$spec_item = $xml->xpath("//ProductFeature[Feature/Name/@Value='Width']/@Presentation_Value"); 
echo 'Width='.(string)$spec_item[0]; 

輸出:

Width=2192 mm 

這被簡化爲我只有一個產品(你有上面的例子中)的數據和我剛剛取得的Presentation_Value屬性。但希望這個概念可以很容易地適應你以後的事情。

XPath的工作方式是它正在尋找一個ProductFeature元素,該元素具有一個封閉的Name元素,其Value屬性設置爲'Width'。因此它將產品要素元素縮小到這些元素並返回Presentation_Value屬性。