Page 1 of 1

how to print dom xml document

Posted: Thu Dec 30, 2010 9:04 am
by zzclare
Hello,

I have a xml file created by using php domdocument. I want to display the xml file in webpage. I google it, and find the following help:
http://www.phpbuilder.com/manual/en/fun ... avexml.php

In this web page the following code

Code: Select all

<?php

$doc = new DOMDocument('1.0');
// we want a nice output
$doc->formatOutput = true;

$root = $doc->createElement('book');
$root = $doc->appendChild($root);

$title = $doc->createElement('title');
$title = $root->appendChild($title);

$text = $doc->createTextNode('This is the title');
$text = $title->appendChild($text);

echo "Retrieving all the document:\n";
echo $doc->saveXML() . "\n";

echo "Retrieving only the title part:\n";
echo $doc->saveXML($title);

?> 
will produce
<?xml version="1.0"?>
<book>
<title>This is the title</title>
</book>
I tested, it will only print the value of the element, not the xml file. It will print only:
this is the title.

How can I display the entire xml file. I want the the output as:

<?xml version="1.0"?>
<book>
<title>This is the title</title>
</book>
Thanks

Re: how to print dom xml document

Posted: Thu Dec 30, 2010 11:53 am
by rcrd.ortiz
Hi, try this (before any output):

Code: Select all

header ("Content-Type:text/xml");
Cheers,

Rcrd

Re: how to print dom xml document

Posted: Fri Dec 31, 2010 11:06 am
by zzclare
Hello,

Thanks. I put the code
header ("Content-Type:text/xml");
before the echo statement, but it still gives the error:

The XML page cannot be displayed
Cannot view XML input using style sheet. Please correct the error and then click the Refresh button, or try again later.
Invalid at the top level of the document.

Is my XML file invalid. I used the exactelly the same code. Can you help me?

Thanks

Re: how to print dom xml document

Posted: Sat Jan 01, 2011 4:14 am
by zzclare
Hello,

It works with firefox browser but not internet explorer. Why?