dsxz84851 2017-07-19 09:22 采纳率: 100%
浏览 22

XML获取属性

I saw lots of tutorials here in overflow, but I could not understand what I am missing.. So I need some help..

I have an XML which it is online and I am trying to parse it like this:

<products>
    <product>
    <id>13389</id>
    <name><![CDATA[ product name ]]></name>
    <category id="14"><![CDATA[ Shoes > test1 ]]></category>
    <price>41.30</price>
</products>

As far, I am reading the XML and parsing it like this:

$reader = new XMLReader();
$reader->open($product_xml_link);
while($reader->read()) {
if($reader->nodeType == XMLReader::ELEMENT && $reader->name == 'product' ) {
    $product = new SimpleXMLElement($reader->readOuterXml());
    $pid = $product->id;
    $name = $product->name;
    $name = strtolower($name);
    $link = $product->link;
    $price = $product->Price;
    ...
    ...
}
} //end while loop

As you can see, there is an id in category tag.. This is the one I would like to grab and procceed to my code..

I did something like this:

echo "prodcut= " . (string)$product->category->getAttribute('id');

The error I am getting is: Call to undefined method SimpleXMLElement::getAttribute()

I need this id in order to test it before insert it in DB.. So,

if($id = 600) {
//insert DB
}
  • 写回答

2条回答 默认 最新

  • douluchuo0801 2017-07-19 10:20
    关注

    Here are several things. First $product = new SimpleXMLElement($reader->readOuterXml()); means that you're reading all that as an separate XML document and parse again. Here is expand(), that will return directly an DOM node and DOM nodes can be imported into SimpleXML.

    For attributes use array syntax..

    $reader = new XMLReader();
    $reader->open($product_xml_link);
    
    // an document to expand to
    $document = new DOMDocument();
    
    // find the first product node
    while ($reader->read() && $reader->localName !== 'product') {
      continue;
    }
    
    while ($reader->localName === 'product') {
      $product = simplexml_import_dom($reader->expand($document));
      $data = [
        'id' => (string)$product->id,
        'name' => (string)$product->name,
        'category_id' => (string)$product->category['id'],
        // ...
      ];
      var_dump($data);
      // move to the next product sibling
      $reader->next('product');
    }
    $reader->close();
    

    Output:

    array(3) {
      ["id"]=>
      string(5) "13389"
      ["name"]=>
      string(14) " product name "
      ["category_id"]=>
      string(2) "14"
    }
    

    Of course you can use the DOM directly and fetch the detail data using Xpath expressions:

    $reader = new XMLReader();
    $reader->open($product_xml_link);
    
    // prepare a document to expand to
    $document = new DOMDocument();
    // and an xpath instance to use
    $xpath = new DOMXpath($document);
    
    // find the first product node
    while ($reader->read() && $reader->localName !== 'product') {
      continue;
    }
    
    while ($reader->localName === 'product') {
      $product = $reader->expand($document);
      $data = [
        'id' => $xpath->evaluate('string(id)', $product),
        'name' => $xpath->evaluate('string(name)', $product),
        'category_id' => $xpath->evaluate('string(category/@id)', $product),
        // ...
      ];
      var_dump($data);
      // move to the next product sibling
      $reader->next('product');
    }
    $reader->close();
    
    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图