Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New Coder
    Join Date
    Feb 2007
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Parse Multiple XML files?

    I'm using the following code to parse an .xml file, although I need a way to do multiple files at once ... Is that possible?

    PHP Code:

    <?php

    $request 
    =  'file.xml';

    // Fetch it
    $response file_get_contents($request);

    if (
    $response === false) {
        die(
    'The request failed');
    }

    // Create a new DOM object
    $dom = new DOMDocument('1.0''UTF-8');

    // Load the XML into the DOM
    if ($dom->loadXML($response) === false) {
        die(
    'Parsing failed');
    }

    function 
    xml_to_result($dom) {
        
    $root $dom->firstChild;
        foreach(
    $root->attributes as $attr$res[$attr->name] = $attr->value;
        
    $node $root->firstChild;
        
    $i 0;
        while(
    $node) {
            switch(
    $node->nodeName) {
                case 
    'Result':
                    
    $subnode $node->firstChild;
                    while(
    $subnode) {
                        
    $subnodes $subnode->childNodes;
                        foreach(
    $subnodes as $n) {
                            if(
    $n->hasChildNodes()) {
                                foreach(
    $n->childNodes as $cn$res[$i][$subnode->nodeName][$n->nodeName]=trim($cn->nodeValue);
                            } else 
    $res[$i][$subnode->nodeName]=trim($n->nodeValue);
                        }
                        
    $subnode $subnode->nextSibling;
                    }
                    break;
                default:
                    
    $res[$node->nodeName] = trim($node->nodeValue);
                    
    $i--;
                    break;
            }
            
    $i++;
            
    $node $node->nextSibling;
        }
        return 
    $res;
    }

    $res xml_to_result($dom);

    echo 
    '<pre>';
    print_r($res);
    echo 
    '</pre>';

    ?>

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    yes...parse them one after the other

  • #3
    New Coder
    Join Date
    Feb 2007
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by shyam View Post
    yes...parse them one after the other
    I think anyone could come to that conclusion.

    Could you be a little more specific? An example is what I'm looking for ...

  • #4
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    Can't you just put all the requested XML files into an array and loop thru with a foreach statement?

    PHP Code:
    <?php

    $request 
    =  array('file.xml','file2.xml','file3.xml','file4.xml');

    foreach (
    $request as $req) {
    // Fetch it
    $response file_get_contents($req);

    if (
    $response === false) {
        die(
    'The request failed');
    }

    // Create a new DOM object
    $dom = new DOMDocument('1.0''UTF-8');

    // Load the XML into the DOM
    if ($dom->loadXML($response) === false) {
        die(
    'Parsing failed');
    }

    function 
    xml_to_result($dom) {
        
    $root $dom->firstChild;
        foreach(
    $root->attributes as $attr$res[$attr->name] = $attr->value;
        
    $node $root->firstChild;
        
    $i 0;
        while(
    $node) {
            switch(
    $node->nodeName) {
                case 
    'Result':
                    
    $subnode $node->firstChild;
                    while(
    $subnode) {
                        
    $subnodes $subnode->childNodes;
                        foreach(
    $subnodes as $n) {
                            if(
    $n->hasChildNodes()) {
                                foreach(
    $n->childNodes as $cn$res[$i][$subnode->nodeName][$n->nodeName]=trim($cn->nodeValue);
                            } else 
    $res[$i][$subnode->nodeName]=trim($n->nodeValue);
                        }
                        
    $subnode $subnode->nextSibling;
                    }
                    break;
                default:
                    
    $res[$node->nodeName] = trim($node->nodeValue);
                    
    $i--;
                    break;
            }
            
    $i++;
            
    $node $node->nextSibling;
        }
        return 
    $res;
    }

    $res xml_to_result($dom);

    echo 
    '<pre>';
    print_r($res);
    echo 
    '</pre>';
    }
    ?>
    Last edited by StupidRalph; 03-08-2007 at 05:57 PM.
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • #5
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Quote Originally Posted by StupidRalph View Post
    Can't you just put all the requested XML files into an array and loop thru with a foreach statement?

    PHP Code:
    $response file_get_contents($request); 
    Yes, but change $request to $req.

  • #6
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    doh! I thought I changed it...I'll edit it.
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • #7
    New Coder
    Join Date
    Feb 2007
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I now get:

    Fatal error: Cannot redeclare xml_to_result() on line 32


  • #8
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Move the function out of the loop.

  • #9
    New Coder
    Join Date
    Feb 2007
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I did that, although now only the last .xml file displays the array info ...

  • #10
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    PHP Code:
    <?php

    $request 
    =  array('file.xml','file2.xml','file3.xml','file4.xml');

    function 
    xml_to_result($dom) {
        
    $root $dom->firstChild;
        foreach(
    $root->attributes as $attr$res[$attr->name] = $attr->value;
        
    $node $root->firstChild;
        
    $i 0;
        while(
    $node) {
            switch(
    $node->nodeName) {
                case 
    'Result':
                    
    $subnode $node->firstChild;
                    while(
    $subnode) {
                        
    $subnodes $subnode->childNodes;
                        foreach(
    $subnodes as $n) {
                            if(
    $n->hasChildNodes()) {
                                foreach(
    $n->childNodes as $cn$res[$i][$subnode->nodeName][$n->nodeName]=trim($cn->nodeValue);
                            } else 
    $res[$i][$subnode->nodeName]=trim($n->nodeValue);
                        }
                        
    $subnode $subnode->nextSibling;
                    }
                    break;
                default:
                    
    $res[$node->nodeName] = trim($node->nodeValue);
                    
    $i--;
                    break;
            }
            
    $i++;
            
    $node $node->nextSibling;
        }
        return 
    $res;
    }

    foreach (
    $request as $req)
    {
        
    // Fetch it
        
    $response file_get_contents($req);
        
        if (
    $response === false)
        {
            die(
    'The request failed');
        }
        
        
    // Create a new DOM object
        
    $dom = new DOMDocument('1.0''UTF-8');
        
        
    // Load the XML into the DOM
        
    if ($dom->loadXML($response) === false)
        {
            die(
    'Parsing failed');
        }
        
        
    $res xml_to_result($dom);
        
        echo 
    '<pre>';
        
    print_r($res);
        echo 
    '</pre>';
    }
    ?>
    That works fine for me.

  • #11
    New Coder
    Join Date
    Feb 2007
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up

    Quote Originally Posted by Inigoesdr View Post
    That works fine for me.
    I guess it helps to know what you're doing too ...

    Works perfectly now! Thank you sooooo much Inigoesdr and StupidRalph ~
    Last edited by warlord; 03-08-2007 at 07:01 PM.

  • #12
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    Post your current code...Does your code look something like this? If its only printin the last xml file its b/c the variable is being overwritten with each consecutive loop. You need to print out the variable before you leave the loop.
    PHP Code:
    <?php


    function xml_to_result($dom) {
        
    $root $dom->firstChild;
        foreach(
    $root->attributes as $attr$res[$attr->name] = $attr->value;
        
    $node $root->firstChild;
        
    $i 0;
        while(
    $node) {
            switch(
    $node->nodeName) {
                case 
    'Result':
                    
    $subnode $node->firstChild;
                    while(
    $subnode) {
                        
    $subnodes $subnode->childNodes;
                        foreach(
    $subnodes as $n) {
                            if(
    $n->hasChildNodes()) {
                                foreach(
    $n->childNodes as $cn$res[$i][$subnode->nodeName][$n->nodeName]=trim($cn->nodeValue);
                            } else 
    $res[$i][$subnode->nodeName]=trim($n->nodeValue);
                        }
                        
    $subnode $subnode->nextSibling;
                    }
                    break;
                default:
                    
    $res[$node->nodeName] = trim($node->nodeValue);
                    
    $i--;
                    break;
            }
            
    $i++;
            
    $node $node->nextSibling;
        }
        return 
    $res;
    }

    $request =  array('file.xml','file2.xml','file3.xml','file4.xml'); 

    foreach (
    $request as $req) { 

    // Fetch it
    $response file_get_contents($req);

    if (
    $response === false) {
        die(
    'The request failed');
    }

    // Create a new DOM object
    $dom = new DOMDocument('1.0''UTF-8');

    // Load the XML into the DOM
    if ($dom->loadXML($response) === false) {
        die(
    'Parsing failed');
    }
    $res xml_to_result($dom);

    echo 
    '<pre>';
    print_r($res);
    echo 
    '</pre>';
    }




    ?>
    Edit: Nevermind
    Last edited by StupidRalph; 03-08-2007 at 06:57 PM.
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.


  •  

    Posting Permissions

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