简体   繁体   English

PHP - 从XML文件中的特定子元素获取数据

[英]PHP - Fetch data from specific child elements in XML file

I'm a PHP/developing novice and I haven't been able to find a tutorial with a solution to this particular problem. 我是PHP /开发新手,我无法找到解决这个特定问题的教程。

I have a XML file from an API with product information for an online store. 我有一个API的XML文件,其中包含在线商店的产品信息。 Each physical product has multiple "styles" that need to be listed on the same page. 每个物理产品都有多个“样式”,需要在同一页面上列出。 I want to do that using their id numbers. 我想用他们的id号做到这一点。

Here's a simplified version of what my feed looks like: 这是我的Feed的简化版本:

<products>
  <product id="001">
    <name>Shirt 1 - Women's</name>
    <price>12.00</price>
    <color>blue</color>
  </product>

  <product id="002">
    <name>Shirt 1 - Men's</name>
    <price>12.00</price>
    <color>red</color>
  </product>
   ...
  <product id="023">
    <name>Shirt 12 - Women's</name>
    <price>15.00</price>
    <color>purple</color>
  </product>

  <product id="024">
    <name>Shirt 12 - Men's</name>
    <price>15.00</price>
    <color>yellow</color>
  </product>

</products>

So on shirt12.php , I would need to fetch just the info from <product id="023"> and <product id="024"> since those contain my information for the "Shirt 12" styles. 因此,对shirt12.php ,我需要获取距离信息<product id="023"><product id="024">因为那些包含我的信息为“12衬衫”的风格。

Using simplexml I'm able to fetch the data of one <product> using this code: 使用simplexml我可以使用以下代码获取一个<product>的数据:

foreach ($products->product as $product) {
if ($product['id'] == '024') { 
  //display data using some code
  }
}

I have no clue how I'd do this for two ore more. 我不知道我是怎么做两个以上的。 If it can be done with simplexml, that would be great because I'm most familiar it. 如果可以用simplexml完成​​,那就太好了,因为我最熟悉它。 But if I need to use a different XML parsing method, I'm open to that as well, however it needs to be compatible with wordpress. 但是如果我需要使用不同的XML解析方法,我也会对此持开放态度,但它需要与wordpress兼容。

Things can be easier with XPath : 使用XPath可以更轻松:

$xml=simplexml_load_string(<<<XML
<products>
  <product id="001">
    <name>Shirt 1 - Women's</name>
    <price>12.00</price>
    <color>blue</color>
  </product>

  <product id="002">
    <name>Shirt 1 - Men's</name>
    <price>12.00</price>
    <color>red</color>
  </product>
  <product id="023">
    <name>Shirt 12 - Women's</name>
    <price>15.00</price>
    <color>purple</color>
  </product>

  <product id="024">
    <name>Shirt 12 - Men's</name>
    <price>15.00</price>
    <color>yellow</color>
  </product>

</products>
XML
);
foreach($xml->xpath('//product[starts-with(name/text(),"Shirt 12")]') as $product)
{
    echo $product->name;
    echo "\n";
    echo $product->price;
    echo "\n";
    echo $product->color;
    echo "\n\n";
}

Online Demo 在线演示

Update : 更新

If you need to fetch by id, just change the XPath: 如果你需要通过id获取,只需更改XPath:

foreach($xml->xpath('//product[@id="023" or @id="024"]') as $product)

Online Demo that also demonstrate how to fetch the ID attribute. 在线演示 ,还演示了如何获取ID属性。

There are many ways to solve that, one of them is that you could make use of the in_array() function to test against multiple id values: 有很多方法可以解决这个问题,其中一个方法是你可以使用in_array()函数来测试多个id值:

$ids = array('023', '024');

foreach ($product->product as $product) {
    if (in_array($product['id'], $ids)) { 
        //display data using some code
    }
}

See as well a related question: PHP If Statement with Multiple Conditions . 请参阅相关问题: PHP如果具有多个条件的语句

I choosed that one because you can use it in other situations, too and also to show that your problem is not specifically related to XML but more for PHP in itself. 我选择了那个,因为你也可以在其他情况下使用它,并且还表明你的问题与XML没有特别的关系,但对于PHP本身更多。

I hope its helpful to you. 我希望它对你有所帮助。

Do you mean like this? 你的意思是这样的吗?

foreach ($product->product as $product) {
if ($product['id'] == '023'||$product['id'] == '024') { 
  //display data using some code
  }
}

Also , not sure why you have a different php page for each id, if it's a product page it should be dynamic so 另外,不确定为什么每个id都有不同的php页面,如果它是一个产品页面,它应该是动态的

shirt.php?id=12 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM