11

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".

The XML below is contained in a php variable $offers

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

Thanks for the help

5 Answers 5

27

With DOM you can either use

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

to count all OfferName elements only. length is an attribute of DOMNodeList.

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

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

Note that within is 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.

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.


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
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the code example! Please could you post this in your question so future readers can see this in case the above link goes dead.
Thank you! Well answered and very much appreciated!
I am still seeking an question answer as my question is closed and days duplicated as this one. stackoverflow.com/questions/12234985/php-xml-count-numbers I want to count the "string" from array, but just element. could you please help. thanks
4

eighter you use 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

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

1 Comment

This assumes OfferNameList only has OfferName children and is exactly why I am not using SimpleXml. It forces you to make assumptions about the markup.
2

You can use the PHP function 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 ....

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.

Comments

1

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 evaluate if you want to count OfferName from some XML like: `

<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 FIRST OfferNameList and

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

P.S.:

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!

Seondly, we use 1 and 2 to access the FIRST and SECOND lists and NOT 0 and 1. Newbies like me might not know this.

Comments

0

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

PHP Manual

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.