Solved

Parsing XML data into an array

Posted on 2003-11-06
6
1,230 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

628 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