For Programmers: Free Programming Magazines  


Home > Archive > PHP Programming > November 2004 > problem when outputting an xml file using php









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author problem when outputting an xml file using php
mr_burns

2004-11-26, 4:07 pm

Hi there,

I am trying to output a file, coutries.xml, which is in the following
format:

<xml...
<countries>
<country id="1">Afghanistan</country>
<country id="2">Albania</country>
Janwillem Borleffs

2004-11-26, 9:09 pm

mr_burns wrote:
> It is managing to output the 'id' attribute but the node value isnt
> appearing. Can anybody tell me how to sort it? Cheers
>


Switched over to PHP 5 already? Then try the following:

<?php

$xml_document = new DomDocument;
$xml_document->load('xml/countries.xml');

if ($xml_document) {
$items = $xml_document->getElementsByTagName('country');
for ($i = 0; $i < $items->length; $i++) {
$item = $items->item($i);
print $item->getAttribute('id') . " = " . $item->nodeValue . "<br>";
}
}

?>


JW



mr_burns

2004-11-27, 3:55 pm

Hi,

I tried your code but got the following error:

Warning: domdocument() expects at least 1 parameter, 0 given in
/home/martyn69/public_html/oarsome/test_countries.php on line 35

Fatal error: Call to undefined function: load() in
/home/martyn69/public_html/oarsome/test_countries.php on line 36



It seems that the code I am using is opening the XML file, getting
into the FOR loop and is managing to spit out the 'id' attribute. The
only problem is the $item->node_value() isnt producing the node value.

Any ideas? Why isnt node_value() producing the node value? Am I
missing something? Cheers

Burnsy
mr_burns

2004-11-27, 3:55 pm

Ive just posted a reply saying that I was still having trouble (this
one might beat it to it though).

I have managed to sort node_value(), Ive used get_content() instead
and its working now. Cheers
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com