2017-10-09 177 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属性。