spacer
Yehuda Shiran January 28, 2002
Preserving White Spaces in XML
Tips: January 2002

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?

Sometimes, you may want to preserve white spaces in the input XML file. You want the DOMDocument's xml property to reflect the input XML file as is, without any formatting. You can use the DOMDocument's preserveWhiteSpace property to control the parser behavior with respect to preservation of white spaces. By default, preserveWhiteSpace is false. If you load an XML file with these three lines:

<SCRIPT LANGUAGE="JavaScript">
<!--
function load2() {
  var xmldoc;
  xmldoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
  xmldoc.async = false;
  xmldoc.load("mydvd.xml");
  alert(xmldoc.xml);
}
// -->
</SCRIPT>
you will get reformatted XML when echoing the xml property. But when you set preserveWhiteSpace to true in the above code:

<SCRIPT LANGUAGE="JavaScript">
<!--
function load1() {
  var xmldoc;
  xmldoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
  xmldoc.preserveWhiteSpace = true;
  xmldoc.async = false;
  xmldoc.load("mydvd.xml");
  alert(xmldoc.xml);
}
// -->
</SCRIPT>
you will get the original XML when echoing the xml property.


People who read this tip also read these tips:

Look for similar tips by subject:

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Rolling Out Your Own HTML Application Version Control · HTML 5: Client-side Storage · Working with Ajax Server Extensions
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Wi-Fi Product Watch, November 2009 · Chip Market Recovering From '08 Collapse · Low-Cost Tools to Kickstart Your New Business