[英]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 printIDValue
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.