Getting DOMDocument to ommit the XML declaration

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
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Getting DOMDocument to ommit the XML declaration

Post by Ollie Saunders »

Does anyone how (and if it is even possible) to get DOMDocument to omit the XML declaration at the top:

Code: Select all

<?xml version="1.0"?>
Problem code:

Code: Select all

$dom = new DOMDocument();
$dom->loadXml('<root>foo</root>');
return $dom->saveXml(); // returns <?xml version="1.0"?><root>foo</root>
My current solution is to use substr(). I would try and use DOMDocumentFragment but it doesn't have any functionality for converting it back to a string.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

There's support for that in recent libxml2 versions but (afaik) not in the php module yet.
NumberNineteen
Forum Newbie
Posts: 7
Joined: Mon Jun 11, 2007 7:30 am

Post by NumberNineteen »

Just curious... why would you want to do that?
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

If I want to process HTML and echo it in the middle of an HTML document. If you use the loadHTML() type methods it's even worse because it adds a doctype as well.
NumberNineteen
Forum Newbie
Posts: 7
Joined: Mon Jun 11, 2007 7:30 am

Post by NumberNineteen »

Ahh... gotcha.

As an option, you could load your XML as you currently are and do a simple XSL transform on it (using an xsl:copy to output the exact format as the source XML) and set your xsl:output to use the omit-xml-declaration attribute: <xsl:output method = "xml" omit-xml-declaration = "yes" />

It's a few more lines of code, but will do what you need to do.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Yes I know about that but my server doesn't have XSL support and it is considerably more lines that substr().
NumberNineteen
Forum Newbie
Posts: 7
Joined: Mon Jun 11, 2007 7:30 am

Post by NumberNineteen »

I think a regular expression to match and delete that string might be the way to go. I say this without having tried it...
NumberNineteen
Forum Newbie
Posts: 7
Joined: Mon Jun 11, 2007 7:30 am

Post by NumberNineteen »

I was intrigued to see if it would work, so here you go:

Code: Select all

<?php
	$dom = new DOMDocument();
	$dom->loadXml('<root><stuff>foo</stuff></root>');
	$theXMLSource = $dom->saveXml();
	$pattern = '^<\?xml version\=\"1\.0\"\?>\n<^';
	$output = preg_replace($pattern, "<", $theXMLSource);
	echo $output;
?>
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Dude, substr() is much easier.

Code: Select all

echo substr($output, strlen('<?xml version="1.0"?> '));
NumberNineteen
Forum Newbie
Posts: 7
Joined: Mon Jun 11, 2007 7:30 am

Post by NumberNineteen »

HA! I completely misread your original post... I thought substr wouldn't work. :)
Post Reply