【问题标题】:xml parsing node valuexml解析节点值
【发布时间】:2013-03-29 22:07:53
【问题描述】:

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

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”

我该怎么办?

【问题讨论】:

    标签: xml-parsing


    【解决方案1】:

    看看有没有用

    $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;
        }
    

    ...

    【讨论】:

    • hI MARCO 如果我使用 $feature = $product->feature;它给了我价值,问题是我有很多不同名称的功能例如:tecnocasaPrivato 等。 .. 如果我只使用 $featureName = $product->feature->attributes()->name 它会给我我不想要的价值 PRODUTTORE
    • 我看到你有不止一个特性,所以使用和以前一样的方法,为所有特性创建一个 foreach。
    • 好的 Marco,谢谢...但是很抱歉,如果我这样做了,它会在 sql MAterial 中给我...也许我必须做一个 if?所以 foreach ($listProducts->product as $product) { $name = $product->NAME; $id= $product->ID; foreach ($product->feature as $feature) { $featureName = $feature->attributes()->name; if ($featureName='PRODUTTORE'){ $material= $product->feature} } 是吗?
    • xml 中的 PRODUTTORE 在哪里?您可以使用 PRODUTTORE 更新示例中的 xml 吗?
    • 顺便说一句,如果你想比较一些东西,请使用 == 而不仅仅是一个 =
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多