php php计数xml元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4386465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:54:23  来源:igfitidea点击:

php count xml elements

phpxml

提问by Martin

Hi what is the best way to count the number of elements in a XML file? In my case I want to count the number of XML tags with the name "OfferName" within the tag "OfferNameList".

嗨,计算 XML 文件中元素数量的最佳方法是什么?就我而言,我想计算标签“OfferNameList”中名称为“OfferName”的 XML 标签的数量。

The XML below is contained in a php variable $offers

下面的 XML 包含在一个 php 变量 $offers 中

$offers = '<OfferNameList>
  <OfferName>...</OfferName>
  <OfferName>...</OfferName>
  <OfferName>...</OfferName>
  ...
</OfferNameList>';

Thanks for the help

谢谢您的帮助

回答by Gordon

With DOMyou can either use

有了DOM您可以使用

$dom->getElementsByTagName('OfferName')->length;

to count all OfferName elements only. lengthis an attribute of DOMNodeList.

计算所有 OfferName 元素length是 的一个属性DOMNodeList

To count all OfferName elements within an OfferNameList, you can use DOMXPath::evaluate

要计算OfferNameList 中的所有 OfferName 元素,您可以使用DOMXPath::evaluate

$xpath->evaluate('count(//OfferNameList/OfferName');

Note that withinis somewhat inaccurate here as the XPath query will only consider direct children. Please adjust your question if you need OfferName elements anywhere below a OfferNameList element.

请注意,这里的内部有些不准确,因为 XPath 查询将只考虑直接子项。如果您在 OfferNameList 元素下方的任何位置需要 OfferName 元素,请调整您的问题。

Also note that //will query anywhere in the document, which might be less efficient for large documents. If you know OfferNameList elements occur at a certain position in your XML only, use a direct path.

另请注意,//将查询文档中的任何位置,这对于大型文档可能效率较低。如果您知道 OfferNameList 元素仅出现在 XML 中的某个位置,请使用直接路径。



Full working example(run on codepad):

完整的工作示例在键盘上运行):

$xml = <<< XML
<root>
    <NotOfferNameList>
      <OfferName>...</OfferName>
      <OfferName>...</OfferName>
      <OfferName>...</OfferName>
    </NotOfferNameList>
    <OfferNameList>
      <OfferName>...</OfferName>
      <OfferName>...</OfferName>
      <OfferName>...</OfferName>
    </OfferNameList>;
</root>
XML;

$dom = new DOMDocument;
$dom->loadXml($xml);

// count all OfferName elements
echo $dom->getElementsByTagName('OfferName')->length, PHP_EOL; // 6

// count all OfferNameList/OfferName elements
$xp = new DOMXPath($dom);
echo $xp->evaluate('count(//OfferNameList/OfferName)'); // 3

回答by endo.anaconda

eighter you use simplexml: http://www.php.net/manual/de/simplexmlelement.count.php

八你使用simplexml:http://www.php.net/manual/de/simplexmlelement.count.php

<?php
$xml = <<<EOF
<OfferNameList>
 <OfferName>
  <child/>
  <child/>
 </OfferName>
 <OfferName>
  <child/>
  <child/>
  <child/>
 </OfferName>
</OfferNameList>
EOF;

$elem = new SimpleXMLElement($xml);
printf("%d offers.\n", $elem->OfferNameList->count() );

?>

or do it with DOM

或者用 DOM 来做

 $domobj->getElementsByTagName('OfferName')->length;

回答by Vincent Mimoun-Prat

You can use the PHP function substr_count

您可以使用 PHP 函数substr_count

<?php
  $tag_count = substr_count($offers, "<OfferName>");
?>

You might need to extract the content of the tags by using a regular expression first.

您可能需要先使用正则表达式来提取标签的内容。

If you are the author of the XML file, you could also simply embed the count as an attribute of the OfferNameList tag, such as ....

如果您是 XML 文件的作者,您还可以简单地将计数嵌入为 OfferNameList 标记的属性,例如 ....

Finally, if you need to parse the XML file, I would strongly suggest to get a proper XML parser (DOM Parser will count the inner tags for you, assuming your XML is not too big) but you will also be able to do it with a SAX parser implementation.

最后,如果您需要解析 XML 文件,我强烈建议您使用合适的 XML 解析器(DOM 解析器将为您计算内部标签,假设您的 XML 不是太大)但您也可以使用SAX 解析器实现。

回答by Watchful Protector

Adding on to solution given by @Gordon (which works perfectly fine for the question asked), its not mentioned and for people who it is not very obvious to: It is possible to use evaluateif you want to count OfferNamefrom some XML like: `

添加到@Gordon 给出的解决方案(对于所提出的问题非常有效),它没有被提及,对于那些不是很明显的人:evaluate如果你想OfferName从一些 XML 中计数 ,可以使用:`

<OfferNameList>
 <OfferName>
  <child/>
  <child/>
 </OfferName>
 <OfferName>
  <child/>
  <child/>
  <child/>
 </OfferName>
</OfferNameList>

<OfferNameList>
 <OfferName>
  <child/>
  <child/>
 </OfferName>
</OfferNameList>

One can use

一个可以用

echo $xp->evaluate('count(//OfferNameList[1]/OfferName)');to count it from FIRSTOfferNameList and

echo $xp->evaluate('count(//OfferNameList[1]/OfferName)');FIRSTOfferNameList 和

echo $xp->evaluate('count(//OfferNameList[2]/OfferName)');to count it from SECOND.

echo $xp->evaluate('count(//OfferNameList[2]/OfferName)');SECOND算起。

P.S.:

PS:

I'm just posting this answer here because I wanted to achieve something like what I just showed and I found this post on google'ing!

我只是在这里发布这个答案,因为我想实现我刚刚展示的东西,我在 google'ing 上找到了这篇文章!

Seondly, we use 1 and 2 to access the FIRSTand SECONDlists and NOT0 and 1. Newbies like me might not know this.

其次,我们使用 1 和 2 来访问FIRSTSECOND列表,而不是0 和 1。像我这样的新手可能不知道这一点。

回答by heshanlk

You can use $dom->count()for PHP 5 >= 5.3.0

您可以使用$dom->count()PHP 5 >= 5.3.0

PHP Manual

PHP 手册