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?
 
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
)
0
 
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
0
 
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??
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
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.
0
 
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!"
0
 
daver99Author Commented:
An A for you sir, thank you very much
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.