我会创建一个函数(使用,深度...)来获取xml中的一些元素。 我的循环问题是提取信息。
谢谢
示例:
echo $IceCatByEan->getSpecByName('width'); // size
echo $IceCatByEan->getSpecByName('depth'); // size
echo $IceCatByEan->getSpecByName('dimension_type'); // mn, cm
ceCatByEan-> getSpecByName(' DIMENSION_TYPE&#39); // 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;
}
阵列演示
[features] =>排列 ( [44095] =>排列 ( [name] =>包装宽度 [value] => 2453.64 [sign] =>毫米 [pres_value] => 2453.6毫米 ) )
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 :(得分: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属性设置为&#39; Width&#39;。因此,它将Product Feature元素缩小到这些元素并返回Presentation_Value属性。