Jump to content

output xml doc to screen


mpsn

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/250658-output-xml-doc-to-screen/
Share on other sites

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();

?>

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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.