XML获取属性 [英] XML get attributes

查看:133
本文介绍了XML获取属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里看到了很多教程,但是我无法理解我所缺少的..所以我需要一些帮助.

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

我有一个在线的XML,我试图这样解析它:

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>

到目前为止,我正在阅读XML并像这样进行解析:

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

如您所见,类别标记中有一个ID.这是我要抓取并继续处理我的代码的那个..

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..

我做了这样的事情:

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

我得到的错误是: 调用未定义的方法SimpleXMLElement :: getAttribute()

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

在将其插入数据库之前,我需要此ID进行测试.

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

if($id = 600) {
//insert DB
}

推荐答案

以下是几件事.第一个$product = new SimpleXMLElement($reader->readOuterXml());表示您将所有内容作为单独的XML文档读取并再次解析.这是expand(),它将直接返回DOM节点,并且DOM节点可以导入到SimpleXML中.

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

输出:

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

当然,您可以直接使用DOM并使用Xpath表达式获取详细数据:

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

这篇关于XML获取属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆