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');

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 这是网络安全里面的poem code
  • ¥15 用js遍历数据并对非空元素添加css样式
  • ¥15 使用autodl云训练,希望有直接运行的代码(关键词-数据集)
  • ¥50 python写segy数据出错
  • ¥20 关于线性结构的问题:希望能从头到尾完整地帮我改一下,困扰我很久了
  • ¥30 3D多模态医疗数据集-视觉问答
  • ¥20 设计一个二极管稳压值检测电路
  • ¥15 内网办公电脑进行向日葵
  • ¥15 如何输入双曲线的参数a然后画出双曲线?我输入处理函数加上后就没有用了,不知道怎么回事去掉后双曲线可以画出来
  • ¥15 soildworks装配体的尺寸问题