简体   繁体   English

PHP XML Feed-如何在父级中打印特定子级的值。 父级与其他节点同名

[英]PHP XML Feed - How to print value of specific child within parent. Parent has same name as other nodes

I'm trying to figure out how to grab and print a specific IDValue in the code below, but the number and order of ProductIdentifier nodes varies per Product , so I can't always get the correct value by using: 我试图在下面的代码中弄清楚如何获取和打印特定的IDValue ,但是ProductIdentifier节点的数量和顺序因每个Product而有所不同,因此我无法始终使用以下方法获得正确的值:

<?php print $productXML[0]->ProductIdentifier[2]->IDValue; ?>

Is there a way to do something like: "If ProductIDType is 03, then print IDValue of that child node"? 有没有办法做类似的事情:“如果ProductIDType为03,则打印该子节点的IDValue ”?

<Product>
  <RecordReference>PublishingGroup</RecordReference>
  <NotificationType>03</NotificationType>
  <RecordSourceType>01</RecordSourceType>
  <RecordSourceName>Publishing Group</RecordSourceName>
  <ProductIdentifier>
    <ProductIDType>01</ProductIDType>
    <IDTypeName>ID</IDTypeName>
    <IDValue>28831</IDValue>
  </ProductIdentifier>
  <ProductIdentifier>
    <ProductIDType>02</ProductIDType>
    <IDValue>0163668869</IDValue>
  </ProductIdentifier>
  <ProductIdentifier>
    <ProductIDType>03</ProductIDType>
    <IDValue>9180763668860</IDValue>
  </ProductIdentifier>
  <ProductIdentifier>
    <ProductIDType>15</ProductIDType>
    <IDValue>9180763668860</IDValue>
  </ProductIdentifier>

UPDATE: 更新:

I tried using xpath, as suggested by the very helpful comments below, but it simply returned "Array" when trying to print via php. 我尝试使用xpath,如以下非常有用的注释所建议的那样,但是当尝试通过php打印时,它只是返回了“ Array”。 I tried several ways to fix it, taking note from other questions here on SO and on W3C docs and other places, but no luck. 我尝试了几种解决方法,注意到了SO,W3C文档和其他地方的其他问题,但没有运气。

Here's what I ultimately ended up doing: 这是我最终要做的事情:

<?php
  foreach($productXML[0]->ProductIdentifier as $value) {
    if($value->ProductIDType=='03')
      {
      print ($value->IDValue);
      }
  }
?>

Not the most elegant of solutions, but it outputs the proper value now. 不是最优雅的解决方案,但它现在可以输出适当的价值。

First of all there is the operation to extract the node (-value) from the document. 首先,存在从文档中提取节点(值)的操作。 As the whole document is kind of a tree of nodes, running an xpath query helps a lot in getting things done here, especially as you have a condition to obtain a specific node (-value). 由于整个文档都是一棵节点树,因此运行xpath查询对在此处完成工作很有帮助,尤其是当您有条件获取特定的节点(值)时。

However using Xpath also introduces a new language. 但是,使用Xpath也引入了一种新语言。 So there is the xpath-part and there is also the PHP-part. 因此,这里有xpath部分,还有PHP部分。 Let's do it Xpath only first (like with pseudo-code). 让我们仅首先使用Xpath(类似于伪代码)。 You want: 你要:

Is there a way to do something like: "If ProductIDType is 03, then print IDValue of that child node"? 有没有办法做类似的事情:“如果ProductIDType为03,则打印该子节点的IDValue ”?

As with every programming question that is asking for possibilities, there is the answer "most certainly yes" ;) - so here it is: 就像每一个要求可能性的编程问题一样,答案都是“最肯定是”;)-所以这里是:

//*[ProductIDType = "03"]/IDValue

This is pure Xpath and reads as the following: Give me the element named IDValue that is a child-node of any element ( * is a wildcard for any name) within the whole document ( // is root or deeper) which has another child-node named ProductIDType with the string-value " 03 " (so called predicate). 这是纯Xpath,其内容如下:给我一个名为IDValue的元素,它是整个文档( //是root或更深)中任何元素的子节点( *是任何名称的通配符),并且具有另一个孩子名为ProductIDType节点,其字符串值为“ 03 ”(所谓谓词)。

In your case you know the name of the parent element already, so instead of * you can use the element-name directly ( ProductIdentifier ). 在您的情况下,您已经知道父元素的名称,因此可以直接使用元素名称( ProductIdentifier )代替* And you also can say what the parent-element name is ( Product ), so you can use it as well (this makes things more specific, so you show that you've understood exactly what you're looking for): 而且,您还可以说出父元素名称是( Product ),因此您也可以使用它(这使事情更加具体,因此您表明您已经完全了解要查找的内容):

//Product/ProductIdentifier[ProductIDType = "03"]/IDValue

Many words for some more or less simple pseudo-code that on it's own doesn't run. 单独运行或多或少的简单伪代码的许多单词无法运行。 But it's worth to understand as it's a valid Xpath expression. 但是值得一读,因为它是有效的Xpath表达式。

Now back to PHP and SimpleXML. 现在回到PHP和SimpleXML。 As SimpleXML can only query Xpath expressions, it always returns an array (and not a single value). 由于SimpleXML只能查询 Xpath表达式,因此它总是返回一个数组(而不是单个值)。 But this is no problem. 但这没问题。 If you're looking for a single value, it's the first member (value) of the array. 如果要查找单个值,则它是数组的第一个成员(值)。 Which is accessed by [0] on zero-indexed arrays (which are returned by SimpleXMLElement::xpath() unless there was an error (or there was no element at all)). 这是由访问[0]上的零索引的数组(其返回SimpleXMLElement::xpath()除非有错误(或不存在元件的话))。

In case of your XML fragment: 对于您的XML片段:

<?php

$buffer = <<<XML
<Product>
  <RecordReference>PublishingGroup</RecordReference>
  <NotificationType>03</NotificationType>
  <RecordSourceType>01</RecordSourceType>
  <RecordSourceName>Publishing Group</RecordSourceName>
  <ProductIdentifier>
    <ProductIDType>01</ProductIDType>
    <IDTypeName>ID</IDTypeName>
    <IDValue>28831</IDValue>
  </ProductIdentifier>
  <ProductIdentifier>
    <ProductIDType>02</ProductIDType>
    <IDValue>0163668869</IDValue>
  </ProductIdentifier>
  <ProductIdentifier>
    <ProductIDType>03</ProductIDType>
    <IDValue>9180763668860</IDValue>
  </ProductIdentifier>
  <ProductIdentifier>
    <ProductIDType>15</ProductIDType>
    <IDValue>9180763668860</IDValue>
  </ProductIdentifier>
</Product>
XML;

There is an result for the xpath query discussed so far: 迄今所讨论的XPath查询的结果:

$xml = simplexml_load_string($buffer);

$first = $xml->xpath('//Product/ProductIdentifier[ProductIDType = "03"]/IDValue')[0];

echo $first, "\n"; # prints: "9180763668860\n"

This is also the example ( see it in full as online-demo ). 这也是示例( 完整示例请参见online-demo )。

So until this very line, this should show the whole picture unless I've rushed too much at the end. 因此,直到这一行为止,这应该可以显示整个图片,除非我最后匆匆忙忙。 Please leave some feedback if it helped you and what is missing from your point of view. 如果有帮助,请留下一些反馈,并从您的角度考虑遗漏的内容。

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

相关问题 PHP XML - 如何构建与父子关系处于同一级别的xml节点树 - PHP XML - How to build a tree of xml nodes that are at at same level with parent child relationship PHP继承:子代与父代具有相同的类名 - PHP Inheritance: Child has same class name as parent 如何在PHP中的每个子节点和孙子节点旁边打印最高级别的父节点 - How to print Highest level parent nodes next to each of their child and grandchild nodes in PHP 具有相同名称的PHP XMLReader父级和子级 - PHP XMLReader parent and child with the same name 使用PHP从XML中具有相同父级的另一个子级的值中获取子级的值 - Get value of child from the value of another child with the same parent in XML using PHP 如何通过给定的父 ID 在 PHP 变量中存储 XML 子节点? - How to store XML child nodes in PHP variables through a given parent ID? 用父子元素重新创建XML feed - Recreating XML feed with parent - child elements 子类中的 PHP 构造函数覆盖父类中的构造函数。 所以父构造函数不会运行。 打开购物车 - PHP constructor in child class overwrites the one in parent. So parent constructor does not run. OpenCart 如果xml没有子级父级结构,如何以所需格式解析此xml - How to parse this xml in required format if the xml has no child parent structure XML:在php中显示两个具有相同名称的子节点 - XML: Show two child nodes with the same name in php
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM