how to print dom xml document

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
zzclare
Forum Newbie
Posts: 11
Joined: Wed Nov 10, 2010 5:22 am

how to print dom xml document

Post 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
rcrd.ortiz
Forum Newbie
Posts: 11
Joined: Sun Dec 26, 2010 10:46 am

Re: how to print dom xml document

Post by rcrd.ortiz »

Hi, try this (before any output):

Code: Select all

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

Rcrd
zzclare
Forum Newbie
Posts: 11
Joined: Wed Nov 10, 2010 5:22 am

Re: how to print dom xml document

Post 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
zzclare
Forum Newbie
Posts: 11
Joined: Wed Nov 10, 2010 5:22 am

Re: how to print dom xml document

Post by zzclare »

Hello,

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