mpsn Posted November 7, 2011 Share Posted November 7, 2011 Hi, I want this output for an XML doc I create via DOM functions. <?xml version="1.0" ?> <letter> <to> <toFirstContact>Bob</toFirstContact> </to> </letter> BUT it outputs nothing to browser screen! Please see code below: <?php //TEST for REPEAT APPENDAGE $aDoc=new DOMDocument(); $root=$aDoc->createElement("letter"); //create child elem to append to root elem node $newChild1=$aDoc->createElement("to"); //append new child elem <to> to root <letter> $root->appendChild($newChild1); //make elem<toFirstContact> and also create a text, then append to <to> $toFirstContactTag=$aDoc->createElement("toFirstContact"); $textNode1=$aDoc->createTextNode("Bob"); $toFirstContactTag->appendChild($textNode1); //now append new child <toFirstContact> to <to> $newChild1->appendChild($toFirstContactTag); print $aDoc->saveXML(); ?> Any help much appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/250658-output-xml-doc-to-screen/ Share on other sites More sharing options...
requinix Posted November 7, 2011 Share Posted November 7, 2011 You also need to append the $root to the $aDoc. Quote Link to comment https://forums.phpfreaks.com/topic/250658-output-xml-doc-to-screen/#findComment-1286058 Share on other sites More sharing options...
xyph Posted November 7, 2011 Share Posted November 7, 2011 Do you have to use DOMDocument? SimpleXML is much easier <?php $base = '<?xml version="1.0" ?><letter></letter>'; $letter = new SimpleXMLElement( $base ); // Create 'to', a child of 'letter' $to = $letter->addChild('to'); // Add a child to 'to' $to->addChild( 'toFirstContact', 'Bob' ); // Output the entire thing as XML header( 'Content-type: text/xml' ); echo $letter->asXML(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/250658-output-xml-doc-to-screen/#findComment-1286059 Share on other sites More sharing options...
mpsn Posted November 7, 2011 Author Share Posted November 7, 2011 Hi xyph, you're right but when I was trying to build an xml shredder using SimpleXML, I didn't know an easy way to test if the current node is text node but DOM has if($dom->nodeType=XML_TEXT_NODE). Anyways, does anyone know why it just outputs Bob, rather than the entire XML document? I though saveXML() is supposed to convert the entire XML to a string? But I do see the xml file when I View Source though. Any help much appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/250658-output-xml-doc-to-screen/#findComment-1286062 Share on other sites More sharing options...
xyph Posted November 7, 2011 Share Posted November 7, 2011 View the source of the page. It will display as HTML unless you specify that it is XML using the header() function. Quote Link to comment https://forums.phpfreaks.com/topic/250658-output-xml-doc-to-screen/#findComment-1286065 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.