SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Jan 2009
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    $dom->getElementsByTagName - Unable to Get Desired Results

    I am currently working on a plugin page that automatically pulls information from an XML file using SOAP/WSDL.

    I have no problem making the call and getting the data, however I have run into the following issue and I'm stuck:

    I am looping through the data on the XML file using a while loop, but I must assign a set number of times to loop.. I cannot find a way to get the number of elements with success.

    PHP Code:

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

    $i=0;

    while (
    $i<100) {
        
    $element[$i] = $dom->getElementsByTagName('element')->item($i)->nodeValue;

              
     
    $i++;
                    } 
    At this point, the data is put into an array for later use. Using this method, I am able to pull all the data from the XML file, however the number of elements contained in the file change regularly. I have attempted the following to get a count of the elements:

    PHP Code:
        $CountVar $dom->getElementsByTagName('element');
                
    echo 
    count($CountVar); 
    However, the result is 1. Through experimentation, I found this:

    PHP Code:
    $CountVar $dom->getElementsByTagName('element1');
                
    echo 
    count($CountVar); 
    The result is again 1. I can keep going with the numbers after "element" and continue to get the same result of 1 until the XML document runs out of data.

    I've been through the PHP documentation and I couldn't find anything in regard to this type of issue... can anyone please steer me in the right direction to finding a count?

  2. #2
    SitePoint Addict
    Join Date
    Sep 2007
    Posts
    371
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The method getElementsByTagName returns an instance of DOMNodeList class, it has a property called length which you can use to find out the number of Dom Nodes it contains.

  3. #3
    SitePoint Member
    Join Date
    Jan 2009
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PERFECT! Exactly what I needed! Thank you so much!



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •