dynamic xml and xmlhttp

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jbkielis
Forum Newbie
Posts: 6
Joined: Thu Aug 04, 2005 4:24 pm

dynamic xml and xmlhttp

Post 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
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

You might want to try the following:

Code: Select all

...
header('Content-Type: text/xml');
echo $dom->saveXML();
jbkielis
Forum Newbie
Posts: 6
Joined: Thu Aug 04, 2005 4:24 pm

Post by jbkielis »

wow...I should have stopped banging my head against a wall and asked on this forum earlier. Worked like a charm. thanks.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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 :)
Post Reply