xml解析节点值

时间:2013-03-29 22:07:54

标签: xml-parsing

我有一个要在sql上传的xml文件

xml就像

 <catalog>
  <products>
  <product>
  <ID>0079</ID>
  <NAME>Casa</NAME>
  <feature name="material">cemento</feature>
  </product>
  </products>
  </catalog>

我做:

  $xml = simplexml_load_file('prova.xml');
  $listProducts = $xml->products;
  foreach ($listProducts->product as $product)
  { 
   $name           = $product->NAME;
   $id= $product->ID;
    ....................

但是,我必须定义变量“FEATURE”

我想在我的sql中插入值“CEMENTO”

我该怎么办?

1 个答案:

答案 0 :(得分:0)

看看它是否有效

$feature = $product->feature;
$featureName = $product->feature->attributes()->name;

更新

foreach ($listProducts->product as $product)
{ 
    $name = $product->NAME;
    $id= $product->ID;

    foreach ($product->feature as $feature)
    { 
        $featureName = $feature->attributes()->name;
    }

...