Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8364219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:22:48+00:00 2026-06-09T12:22:48+00:00

I have to parse xml files that look like this : http://goo.gl/QQirq How can

  • 0

I have to parse xml files that look like this : http://goo.gl/QQirq

How can I count number of items/records in this xml- by ‘item’ I mean a ‘productItem’ element, ie there are 5 items in the example xml. I don’t specify the tag name ‘productItem’ when parsing the xml, so I can’t count occurrences of ‘productItem’. Here is the code I have:

        <?php
$doc = new DOMDocument();
$doc->load("test.xml");
$xpath = new DOMXpath( $doc );
$nodes = $xpath->query( '//*| //@*' );
$nodeNames = array();
foreach( $nodes as $node )
{
    $nodeNames = $node->nodeName;
    $name=$node->nodeName;
    $value=$node->nodeValue;
    echo ''.$name.':'.$value.'<br>';
    }

?> 

How can I count number of items and display them one by one, like this ideally : http://goo.gl/O1FI8 ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T12:22:50+00:00Added an answer on June 9, 2026 at 12:22 pm

    Why don’t you use DOMDocument::getElementsByTagName?

    //get the number of product items
    echo $doc->getElementsByTagName('productitem')->length;
     //traverse the collection of productitem
    foreach($doc->getElementsByTagName('productitem') as $element){
      //$element is a DOMElement
      $nodeNames = $element->nodeName;
      $name=$element->nodeName;
      $value=$element->nodeValue;
      echo ''.$name.':'.$value.'<br>';
    }
    

    As you want to traverse your document, use XPath is just greedy. Moreover you will instantiate each node of the document even if you only want one or two.

    You can use hasChildNodes methode and childNodes attribute to traverse your document

    function searchInNode(DOMNode $node){
    
         if(isGoodNode($node)){//if your node is good according to your database
             mapTheNode($node);
         }
         if($node->hasChildNodes()){
             foreach($node->childNodes as $nodes){
                 searchInNode($nodes);
             }
         }
    }
    searchInNode($domdocument);    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any tool that can parse/conver xml files to this format? I have
I want to parse xml files that have elements like these: <element>&amp</element> <element>&amp;</element> But
Normally the XML files I have to parse are like this: <row id=1> <title>widget<title>
i have a xml file on a server that look for example like this
I have an XML File with Elements that look like the following: <level> <name>Name
I have One XML file which will be look like this :: <Note> <NoteId>13328</NoteId>
I have a simple xml file that looks like this: <?xml version=1.0 encoding=UTF-8 standalone=yes
I have multiple xml files that I am trying to parse using JQuery. So
I have an XML file that I'm trying to parse with Linq-to-XML. One of
I have written a simple C++ shell program to parse large XML files and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.