Page 1 of 1

dynamic xml and xmlhttp

Posted: Wed Aug 10, 2005 9:22 pm
by jbkielis
Hey everyone. Thanks in advance for any help.

I am trying to use the javascript XMLHTTP object in order to parse XML data. This procedure works fine when I have a static XML document. However, I need to use a dynamically created XML file created through PHP, such as the following:

text_xml.php

Code: Select all

class Articles extends DomDocument { 
    function __construct() { 
        //has to be called! 
        parent::__construct(); 
    } 
     
    function addArticle($name, $id) { 
                $item = $this->createElement("university"); 
		
                $attr1 = $this->createAttribute("name");
		$attr1text = $this->createTextNode($name); 
		$attr1->appendChild($attr1text); 
		
		$attr2 = $this->createAttribute("id");
		$attr2text = $this->createTextNode($id); 
		$attr2->appendChild($attr2text); 
	
	
                $item->appendChild($attr1);
		$item->appendChild($attr2); 
		
                $this->documentElement->appendChild($item); 
    } 
} 
$dom = new Articles(); 
$titlespace = $dom->createElement("schools");
$dom->appendChild($titlespace);
$dom->addArticle('test article','article id');
echo $dom->saveXML();
When I view this file, it correctly outputs the XML. However, I am unable to parse it using the XMLHTTP object. Is a php dynamically generated XML file somehow different from a static xml file?

Any ideas? Thanks.
Jonathan

Posted: Wed Aug 10, 2005 9:24 pm
by timvw
You might want to try the following:

Code: Select all

...
header('Content-Type: text/xml');
echo $dom->saveXML();

Posted: Wed Aug 10, 2005 11:37 pm
by jbkielis
wow...I should have stopped banging my head against a wall and asked on this forum earlier. Worked like a charm. thanks.

Posted: Thu Aug 11, 2005 12:03 am
by s.dot
jbkielis wrote:wow...I should have stopped banging my head against a wall and asked on this forum earlier. Worked like a charm. thanks.
Not just any forum. THIS forum. Where geniuses of the likes of timvw can assist you. :-D

Posted: Thu Aug 11, 2005 5:57 am
by timvw
I'm not a genius.. But i've also been banging my head a couple of hours when i experienced the same issue :)