...

View Full Version : Reading XML with PHP



martynball
08-15-2012, 05:13 PM
Is there no similar way to JS to select tags? In JavaScript it's "getElementsByTagName"...

But anyway, main question. Why isn't this displaying the tag names inside the <menu_item> tag. So it should display "name, image, text, color".



//Create navigation HTML from XML
$xml = simplexml_load_file("xml/menus.xml");

foreach( $xml -> children() as $child ) {
if ( $menu = $child -> getName() == "menu_item" ) {
echo $menu -> children();
}
}


XML:


<?xml version="1.0"?>
<menu>
<menu_item>
<name>reptiles</name>
<image>images/snake.gif</image>
<text>Reptiles</text>
<color>green</color>
</menu_item>
</menu>

DrDOS
08-15-2012, 05:32 PM
How is this XML file generated? Is it on your server, or from another domain?

Fou-Lu
08-15-2012, 05:39 PM
SimpleXML is designed as such. Its flat and simple.
If you want to manipulate the dom like JS you need to use the DOMDocument object, or write triggers for handles using XMLReader depending on if you want dom, sax or pull parsing.

martynball
08-15-2012, 06:08 PM
SimpleXML is designed as such. Its flat and simple.
If you want to manipulate the dom like JS you need to use the DOMDocument object, or write triggers for handles using XMLReader depending on if you want dom, sax or pull parsing.

Any idea why my current script doesn't do what I want it to do at the moment?

Also, the XML doc is written by me manually at the moment.

tracknut
08-15-2012, 06:55 PM
Try it like this:


foreach( $xml -> children() as $child ) {
foreach ($child as $value) {
echo $value->getName()." => ".$value."<br>";
}
}


Dave

Fou-Lu
08-15-2012, 07:02 PM
Nope, I don't write in SimpleXML. If I had to guess children() would be an array of collected children matching the object its pulled from, so you cannot print it directly (unless the object has overridden the toString functionality and the collection is an object that can traverse).

If you use dom, you would be able to use getElementsByTagName as well. You can also use xPath to target the node paths.

DrDOS
08-15-2012, 08:26 PM
Given all that you're said so far, I would write a php script that generated the XML file and also made whatever code you wanted produced in a usable format in another file. That way you only have to do things once.

martynball
08-15-2012, 09:18 PM
Given all that you're said so far, I would write a php script that generated the XML file and also made whatever code you wanted produced in a usable format in another file. That way you only have to do things once.

What? I have hand written the XML file at the moment so I can actually write a script that can read and use it... what use is generating the exact same file at the moment, there is no need until I can actually use the data.

One step at a time.

--------


And okay thanks Fou-Lu, I will look into using DOM.

tracknut
08-15-2012, 09:30 PM
Wasn't my suggestion what you wanted to do?

martynball
08-15-2012, 09:52 PM
Wasn't my suggestion what you wanted to do?

Sorry, I did not see your post. That worked thanks, but I think I will look into using the DOM model instead.

martynball
08-15-2012, 10:16 PM
Edit:

Never mind, I found this on the PHP website and adapted it to work for me:



$doc = new DOMDocument();

if ($doc->loadXML($xml)) {
$items = $doc->getElementsByTagName('item');
$headlines = array();

foreach($items as $item) {
$headline = array();

if($item->childNodes->length) {
foreach($item->childNodes as $i) {
$headline[$i->nodeName] = $i->nodeValue;
}
}

$headlines[] = $headline;
}


My version:



//Level 1
$menu_level = $xmlDoc->getElementsByTagName('menu_item');

//Make variables
$list = array();

foreach ($menu_level as $item) {
$x = array();

if($item->childNodes->length) {
foreach($item->childNodes as $i) {
$x[$i -> nodeName] = $i->nodeValue;
}
}

$list[] = $x;
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum