简体   繁体   中英

How to extract specific xml information

I would create a function (with, depth ...) to take some elements inside a xml. I have a problem with my loop to extract the informations.

Thank you

example :

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

ceCatByEan->getSpecByName('dimension_type'); // mn

function

// 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;
}

array presentation

[features] => Array ( [44095] => Array ( [name] => Package width [value] => 2453.64 [sign] => mm [pres_value] => 2453.6 mm ) )

XML files

<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>

The way I would retrieve the individual values is by using XPath and fetch each value directly.

<?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];

Output:

Width=2192 mm

This is simplified as I only have the data for one product (the example you had above) and I've just retrieved the Presentation_Value attribute. But hopefully the concept can easily be adapted to what your after.

The way the XPath works is that it is looking for a ProductFeature element which has an enclosed Name element with the Value attribute set to 'Width'. So it narrows down the Product Feature elements to just these and returns the Presentation_Value attribute.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM