Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP读取XML文件时,XML文件没有组织_Php_Xml_Xml Parsing - Fatal编程技术网

使用PHP读取XML文件时,XML文件没有组织

使用PHP读取XML文件时,XML文件没有组织,php,xml,xml-parsing,Php,Xml,Xml Parsing,大家好,我有一个如下的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 creati

大家好,我有一个如下的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>

这是一辆小汽车
44.95
2018
深入了解如何使用XML创建应用程序。
这是一辆自行车
33.58
只是虚幻的描述
这是轮子
33.58
只是虚幻的描述
这是一辆自行车
33.58
只是虚幻的描述
我想循环遍历节点并相应地处理节点。如果是一辆车,我需要将车的详细信息传递给某个函数,如果是自行车,我需要将自行车的详细信息传递给另一个函数

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

PHP

$z=新的XMLReader;
$z->open('data.xml');
$doc=新文档;
//移动到第一个节点
而($z->read()&&$z->name!=='product');
//现在我们已经到达了正确的深度,跳到下一个直到树的尽头
而($z->name===‘产品’)
{
//任何一个都应该起作用
//$node=newsimplexmlement($z->readOuterXML());
$node=simplexml\u import\u dom($doc->importNode($z->expand(),true));
//现在,您可以使用$node而不必为解析而发狂
变量转储($node->element\u 1);
//转到下一个
$z->next('product');
}

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

只需使用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)


我的问题可能听起来很愚蠢,但您是否正在将硬编码的“product”字符串更改为您试图查找的其他字符串?我们必须更改它,但如何知道下一个节点是哪个?XmlReader不应该处理这个问题吗?如果你有
$z->next('bike')
,它应该会给你下一个
,对吗?@MarkusDeibel是的,但问题是下一个元素并不总是自行车,它可以是任何东西。我的意思是订单没有定下来。我们需要手动检查。如果文档页()上的注释可信,“不带参数的next()将带您到达与当前光标所在深度相同的下一个同级节点。”。唯一的方法是使用
while($z->read()&&&$z->name=='catalog')找到第一个子项XMLReader
是相当基本的,另一个库将为您提供更好的API。
$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";
}
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