Solved

Parsing XML data into an array

Posted on 2003-11-06
6
1,225 Views
Last Modified: 2013-11-19
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.
0
Comment
Question by:daver99
6 Comments
 
LVL 19

Expert Comment

by:Michael701
ID: 9696778
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
 

Author Comment

by:daver99
ID: 9700707
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
 
LVL 6

Expert Comment

by:aolXFT
ID: 9707044
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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

Author Comment

by:daver99
ID: 9716073
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
 
LVL 11

Accepted Solution

by:
shmert earned 500 total points
ID: 9740995
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
 

Author Comment

by:daver99
ID: 9748225
An A for you sir, thank you very much
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question