Adrian Tanase
Adrian Tanase

Reputation: 700

XML reading with DOM Document in PHP

I have this XML (part of it posted here)

<products>
- <product no="AP1126S-07" name=""Clergoux" set kravata" price="449.8" currency="Kč">
- <folders>
  <folder category="Fashion" subcategory="kravaty" /> 
  </folders>
 <description name="POPIS PRODUKTU">Hedvábná kravata André Philippe s manžetovými         knoflíčky a kapesníčkem v dárkové krabičce zabalené do stejné látky, ze které je vyrobená kravata.</description> 
- <properties>
  <property name="ROZMĚRY VÝROBKU" value="110×105×110 mm" /> 
  <property name="KS / KARTON" value="96" /> 
  <property name="HMOTNOST KARTONU" value="31,5" /> 
  <property name="NETTO HMOTNOST / KARTON" value="29,5" /> 
  <property name="DIM1" value="90" /> 
  <property name="DIM2" value="45" /> 
  <property name="DIM3" value="36" /> 
  <property name="TECHNOLIGIE POTISKU" value="T1 (8C, 80×50 mm)" /> 
  <property name="TARIF" value="6215100090" /> 
  <property name="M3/CARTON" value="0,146" /> 
  <property name="COOL 2014 KAPITOLA" value="fashion" /> 
  <property name="COOL 2014 STRANY" value="407" /> 
  <property name="main category" value="fashion" /> 
   </properties>
 - <images>
   <image src="http://www.andapresent.com/kepek/cms/original/26484.jpg" /> 
   </images>
 - <stocks>
   <stock name="navi_central" value="150" /> 
   <stock name="navi_arrive" value="" date="" /> 
   <stock name="eu_central" value="" date="" /> 
   <stock name="eu_arrive_1" value="" date="" /> 
   <stock name="eu_arive_2" value="" date="" /> 
   </stocks>

I need to check if the code (such as in product no='AP1126S-07') is some code, and if it checks

i need to go read the stock value (items in the warehouse, current stock listing).

I'm using DOMDocument, but I never used it before and I have a hard time understanding how to read the value in section in the XML.

Thanks! Any help is appreciated

my UPDATED code

$xmlString = 'anda_xml_export2.xml';
$doc = new DomDocument();
$doc->load($xmlString);

    $product = $doc->getElementsByTagName('product');
    $sku = $product->item(0)->getAttribute('no');

    echo $sku;

    if($sku=='AP1126S-07'){

        $my_stocks_node = $product->getElementsByTagName("stocks");
        $my_stock_node = $my_stocks_node->getElementsByTagName("stock");

        $stock = $my_stock_node->item(0)->getAttribute('value');

        echo "stock : ";
        echo $stock;

    }

echo $sku;

Upvotes: 0

Views: 81

Answers (1)

Mike
Mike

Reputation: 2761

$product is a DomElement. You access attributes of DomElements with the getAttribute function so, in this case, $product->getAttribute ('no');

Upvotes: 2

Related Questions