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

?>

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.