0

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;
3
  • 2
    Please show your code so we can help you understand where you went wrong. SO is not for getting people to write code for you. Commented Dec 3, 2014 at 9:09
  • 1
    Does your HTML really have all those - characters before some tags? Commented Dec 3, 2014 at 9:09
  • i copied and pasted the XML output from IE, that's why it has minuses. Commented Dec 3, 2014 at 9:11

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

5 Comments

thanks, just found it by myself 5 mins ago :) thanks a lot....i'm still having trouble in locating the stock value tho'.,..browsing the tree...
Look up DomXPath and use that. $xp->query ("/products/product[@no='AP1126S-07']/stocks")->item (0) will return the stocks element. I'll leave you to extrapolate from the query how to find which stock you are looking for.
thanks Mike! DomXPath is an awesome tool to pinpoint the exact location of an item in a XML tree :))) you saved my day. seriously :)
But use DOMXPath::evaluate(), it allows to fetch scalar values, not only node lists: $xp->evaluate("string(/products/product[@no='AP1126S-07']/stocks[@name='navi_central']/@value)")
I chose query () over evaluate () because it's simpler for someone who has not come across it (or the xpath syntax) before.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.