Parsing XML data into an array

I want to grab this xml data: http://www.nationstates.net/cgi-bin/nationdata.cgi?nation=cork_south_central and put all of the values (not the tags) into the elements of the array.


so basically the array will end up like this;
parsed_xml_array
(
 [0] => Cork South Central
 [1] => Republic
 [2]......etc.
)


have a look at http://www.nationstates.net/cgi-bin/nationdata.cgi?nation=cork_south_central and you will know what i mean. This is from the Nationstates.net game.

PS. As my page is based on Tripod, I cant use fopen(), sorry!

I have loads of points to give on this, so the best will get the points. (Well the most I can offer is only 500)

D.
daver99Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Michael701Commented:
Take a look at the function

array xmlrpc_decode ( string xml [, string encoding])

the real problem is the lack of fopen(), the only idea i have is to manually cut and paste teh data into a text box then have the php decode the data
daver99Author Commented:
I use this as it is, but this doesnt let me get the nested nodes?

// create array to hold country names
$attributes = array();

// create a DOM object from the XML data
if(!$doc = xmldocfile("http://www.nationstates.net/cgi-bin/nationdata.cgi/nation=$nation"))
{
   die("Error parsing XML");
}

// start at the root
$root = $doc->root();

// move down one level to the root's children
$nodes = $root->children();

// iterate through the list of children
foreach ($nodes as $n)
{
   // for each element get the text node under it
   // and add it to the $attributes[] array
   $text = $n->children();
   if ($text[0]->content != "")
   {
      $attributes[] = $text[0]->content;
   }
}
?>

does this help??
aolXFTCommented:
From the way you are doing it I'd consider using a recursive algorithm. I'd write this for you if I knew more about the XML handling in PHP. I see you are using a function called xmldocfile(). I can't find that function in the PHP Manual. Did you write it yourself, or is it just me not able to find it.

A better way might be to use the domxml_xmltree function if it is availible to you.

If you're prepared to wait, an even better way would be to use SimpleXML, which will be in PHP5.
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

daver99Author Commented:
heres an explanation of it:
http://zugeschaut-und-mitgebaut.de/php/function.xmldocfile.html


ah, recursion, now theres a blast from my college days past:
"To understand recursion, you must first understand recursion!"
shmertCommented:
Here's an old function for converting a DOM node to an assoc array.  It's kind of dusty, but you pass in a domxml object as the first param.  You can map the tag names to different items in the array with the second param, but it's safe to leave it blank.  Call it like so:

        $xml = file_get_contents($url);
        $doc = domxml_open_mem($xml);
        return xmlnode2array($doc->root());


/**
* Recursive function to convert xml root node to big assoc array
*/
function xmlnode2array($node, $newMap="") {
        static $map;
        if (!$map && $newMap) $map = array_flip($newMap); // reverse map
        if ($node->type==XML_ELEMENT_NODE) {
                if ($attrArray = $node->attributes()) {
                        // parse attributes //
                        foreach($attrArray AS $attr) {
                                $key = '@' . $attr->name;
                                if ($map[$key])         $out[$map[$key]] = $attr->value;
                                else                    $out[$key] = $attr->value;
                        }
                }
                if ($childArray = $node->children()) {
                        // add child nodes //
                        foreach($childArray AS $child) {
                                if ($child->type==XML_ELEMENT_NODE) {
                                        $tagName = $child->tagname;
                                        if ($tagName=='ARRAY_DATA') {
                                                $out[] = xmlnode2array($child);
                                        } else if ($map[$tagName]) {
                                                $out[$map[$tagName]] = xmlnode2array($child);
                                        } else {
                                                $out[$tagName] = xmlnode2array($child);
                                        }
                                } else {
                                        if ($content = xmlnode2array($child))
                                                $out = $content;
                                }
                        }
                }

        } else {
                // this is a CONTENT NODE //
                $out = trim($node->content);
                if (!$out) return false;
        }
        return $out;
}



It returns the following for the array:
Array
(
    [NAME] => Cork South Central
    [TYPE] => Republic
    [FULLNAME] => The Republic of Cork South Central
    [MOTTO] => Laissez Faire
    [CATEGORY] => Inoffensive Centrist Democracy
    [UNSTATUS] => UN Member
    [FREEDOM] => Array
        (
            [CIVILRIGHTS] => Very Good
            [ECONOMY] => Powerhouse
            [POLITICALFREEDOM] => Good
        )

    [REGION] => Munster
    [POPULATION] => 1253
    [TAX] => 9
    [ANIMAL] => Cow
    [CURRENCY] => Euro
    [FLAG] => http://www.nationstates.net/images/flags/uploads/cork_south_central.jpg
    [MAJORINDUSTRY] => Information Technology
    [GOVTPRIORITY] => Education
    [LASTLOGIN] => 1068564876
    [LASTACTIVITY] => 2 days ago
)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
daver99Author Commented:
An A for you sir, thank you very much
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.