【问题标题】:Reading XML file with PHP, XML file is not organized用PHP读取XML文件,XML文件没有组织
【发布时间】:2019-05-21 12:34:34
【问题描述】:

大家好,我有一个这样的 XML 文件:

<?xml version="1.0"?>
<catalog>
   <car>
      <title>This is car</title>
      <price>44.95</price>
      <model>2018</model>
      <description>An in-depth look at creating applications with XML.</description>
   </car>

   <bike>
      <title>this is bike</title>
      <price>33.58</price>
      <description>Just the dummy description</description>
   </bike>
   <wheels>
      <title>this is wheel</title>
      <price>33.58</price>
      <description>Just the dummy description</description>
   </wheels>

   <bike>
      <title>this is bike</title>
      <price>33.58</price>
      <description>Just the dummy description</description>
   </bike>


</catalog>

我想遍历节点并相应地处理节点。如果是汽车,我需要将汽车详细信息传递给某个函数,如果是自行车,我需要将自行车详细信息传递给另一个函数。

重点是根据节点类型(汽车或自行车或其他)处理节点。

PHP

$z = new XMLReader;
$z->open('data.xml');

$doc = new DOMDocument;

// move to the first <product /> node
while ($z->read() && $z->name !== 'product');

// now that we're at the right depth, hop to the next <product/> until the end of the tree
while ($z->name === 'product')
{
    // either one should work
    //$node = new SimpleXMLElement($z->readOuterXML());
    $node = simplexml_import_dom($doc->importNode($z->expand(), true));

    // now you can use $node without going insane about parsing
    var_dump($node->element_1);

    // go to next <product />
    $z->next('product');
}

如果节点是产品,上面的 PHP 代码可以正常工作。我需要使用不同的节点。

【问题讨论】:

  • 我的问题可能听起来很愚蠢,但是您是否要将硬编码的“产品”字符串更改为您要查找的其他字符串?
  • 我们必须改变它,但是如何知道哪个是下一个节点?
  • XmlReader 不应该解决这个问题吗?如果你有$z-&gt;next('bike'),它应该给你下一个&lt;bike&gt;,对吧?
  • @MarkusDeibel 是的,但问题是下一个元素并不总是自行车,它可以是任何东西。我的意思是订单不固定。我们需要手动检查。
  • 好吧,如果文档页面 (php.net/manual/de/xmlreader.next.php#123774) 上的注释可以信任“next() 不带参数会将您带到与深度相同的下一个兄弟节点当前光标是。"。唯一的办法就是使用while ($z-&gt;read() &amp;&amp; $z-&gt;name === 'catalog'); 找到第一个孩子。但是,在查看手册后,我认为XMLReader 相当基本,另一个库将为您提供更好的API。

标签: php xml xml-parsing


【解决方案1】:

您只需使用 SimpleXML 即可:

$xml = simplexml_load_file('data.xml');
foreach ($xml->children() as $product) {
    if ($product->getName() == 'car') {
        carFunc($product->title, $product->price, $product->model, $product->description);
    }
    if ($product->getName() == 'bike') {
        bikeFunc($product->title, $product->price, $product->description);
    }
    if ($product->getName() == 'wheels') {
        wheelFunc($product->title, $product->price, $product->description);
    }
}

function carFunc($title, $price, $model, $description) {
    echo "Car: $title: It's a $model selling for $price. In detail: $description\n";
}

function bikeFunc($title, $price, $description) {
    echo "Bike: $title: It sells for $price. In detail: $description\n";
}

function wheelFunc($title, $price, $description) {
    echo "Wheel: $title: It sells for $price. In detail: $description\n";
}

输出(用于您的示例 XML)

Car: This is car: It's a 2018 selling for 44.95. In detail: An in-depth look at creating applications with XML. 
Bike: this is bike: It sells for 33.58. In detail: Just the dummy description 
Wheel: this is wheel: It sells for 33.58. In detail: Just the dummy description 
Bike: this is bike: It sells for 33.58. In detail: Just the dummy description

Demo on 3v4l.org

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多