donglipi4495
2014-12-03 09:05
浏览 32

在PHP中使用DOM Document读取XML

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;
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dqfr90979 2014-12-03 09:43
    已采纳

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

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题