is it possible to access xml data as a multidimensional array?
I have a basic XML file that I load in my PHP using
$xml = simplexml_load_file("file.xml");
I want to be able to access my data using syntax something similar to:
$xml[0]['from'];
$xml['note']['from'];
$xml['from']['email'];
I know I can access the data using this:
foreach($xml->children() as $child) {
echo $child->getName() . ": " . $child . "<br />";
}
However, this is NOT a preferred method, and I would like to use syntax similar to multidimensional arrays.
Using APIs from many different websites, this is generally how I would access XML, however开发者_如何学Go, I can't seem to do it with my own using simplexml_load_file("file.xml");
I know this is very simple but is there something I'm missing? Please help! Thanks in advance
XML File:
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove</to>
<from>
<name>Jani</name>
<email>email@email.com</email>
</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
i think its not a perfect solution for you but it might help you for your problem.
XML FILE:
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove</to>
<from>
<name>Jani</name>
<email>email@email.com</email>
</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
PHP FILE:
<?php
$xml = simplexml_load_file("new.xml");
$webroot="http://".$_SERVER['HTTP_HOST']."/";
$doc = new DOMDocument();
$doc->load( $webroot.'/new.xml' );
$note = $doc->getElementsByTagName( "note" );
$final_arr = array();
$i=0;
foreach($note as $note)
{
$to = $note->getElementsByTagName( "to" );
$to = $to->item(0)->nodeValue;
$final_arr[$i]['to'] = $to;
$from = $note->getElementsByTagName( "from" );
foreach($from as $from)
{
$name = $from->getElementsByTagName( "name" );
$name = $name->item(0)->nodeValue;
$final_arr[$i]['from']['name'] = $name;
$email = $from->getElementsByTagName( "email" );
$email = $email->item(0)->nodeValue;
$final_arr[$i]['from']['email'] = $email;
}
$heading = $note->getElementsByTagName( "heading" );
$heading = $heading->item(0)->nodeValue;
$final_arr[$i]['heading'] = $heading;
$body = $note->getElementsByTagName( "body" );
$body = $body->item(0)->nodeValue;
$final_arr[$i]['body'] = $body;
$i++;
}
echo "<pre>";
print_r($final_arr);
echo "</pre>";
?>
hope this works for you.
Unfortunately, unless you are parsing the xml file yourself, you cannot do this. Any attempt to access one of the nodes will most likely return a DOM error saying that it cannot be converted to a string.
You can take a look at this post: http://bytes.com/topic/php/answers/1364-turn-xml-into-2-dimensional-array.
If it is something you really need to do, you will probably need to do it yourself with an additional step, like creating a function or class to handle it.
"and I would like to use syntax similar to multidimensional arrays." - I don't know if it's similar enough: $notes->note[0]->to;
as in
<?php
$notes = new SimpleXMLElement('<?xml version="1.0" encoding="ISO-8859-1"?>
<notes>
<note>
<to>Tove</to>
<from>
<name>Jani</name>
<email>email@email.com</email>
</from>
<heading>Reminder</heading>
<body>Don\'t forget me this weekend!</body>
</note>
<note>...</note>
<note>...</note>
</notes>');
echo $notes->note[0]->to;
精彩评论