Solved

Parsing XML data into an array

Posted on 2003-11-06
6
1,220 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Encode a password 2 39
populate dynamic drop down from a csv 3 39
php image upload 3 27
Codiing Non-Existent Links 4 28
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This article discusses four methods for overlaying images in a container on a web page
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

762 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now