Click to See Complete Forum and Search --> : childNodes or getElementsByTagName equivilent in php


jclarke
09-01-2006, 07:26 PM
I have a string from an xml file that looks something like this:

<product>
<brand>Jonsered</brand>
<name>CS 2121 EL</name>
<type>Chainsaw</type>
<colour>Red</colour>
<ID>1</ID>
<price>---</price>
<description>
While versatile electric CS2121EL is perfect for pruning around the garder and precision cutting.[br]
</description>
</product>


I want to be able to seperate each product's attributes into a multi-dimensional array.

so far I can do this:

$productsFile_text = file_get_contents("xml/products.xml");
$products_array = explode("</product>",$productsFile_text);
$products_array = implode("",$products_array);
$products_array = explode("<product>",$products_array);

so that split's up all the products (there is more than 1 of them) into and array with the tags "brand" and "price" etc. in each part of the array.

But when it comes to the loop that will create a new array with all the data it eg.

$new_products_array = array();
for($i = 0; $i < count($products_array); $i++) {
//do summat like
$new_products_array[] = ("brand" => $daBrand,"name" => $daName etc.);
}


I just don't know how to get the content in between the tags "<brand>content</brand>". In javascript i know i could do something like:

curBrand = prods[i].getElementByTagNames("brand")[0].innerHTML;


But is there any way to do that or similar in php?

I know this is a long post, but any help would be muchly appreciated! :)
[/code]

Daniel T
09-01-2006, 07:29 PM
If you're familiar with the Javascript DOM functions, you'll find this library very easy to use: http://php.net/domxml

EDIT
There's also this one, specialised for use with HTML: http://php.net/dom This one should already be included in PHP, if I remember correctly.

bokeh
09-01-2006, 07:31 PM
Have a look at the DOM functions (http://www.php.net/dom).

jclarke
09-01-2006, 08:23 PM
Ok i had a look at domxml_xmltree but when i used it it gave me a fatal error:

$productsFile_text = file_get_contents("xml/products.xml");
$xmltree = domxml_xmltree($productsFile_text);


can anybody show me an example of how to use "productsFile"?

Thanks:)

Daniel T
09-01-2006, 08:29 PM
This extension makes use of the GNOME XML library. Download and install this library. You will need at least libxml-2.4.14. To use DOM XSLT features you can use the libxslt library and EXSLT enhancements from http://www.exslt.org/. Download and install these libraries if you plan to use (enhanced) XSLT features. You will need at least libxslt-1.0.18.
Try just using the DOM classes, as the DOMXML ones require extra files to be installed. Try this:$xmltree = DOMDocument->load('xml/products.xml');That should load it into an XML tree. Here's documentation on DOMDocument->load() (http://php.net/manual/en/function.dom-domdocument-load.php)