Arthur Hoornweg wrote:
NaraendiraKumar R. R. wrote:
Looks like you are using the MSXML 3.0 or 4.0 via COM?
Yes.
If you are doing this from a .NET environment, you can automate the
indentation by passing the string through an XML Writer.
If you just need it for viewing purposes, you could use some XML editor.
VS.NET does a decent job. Also XML Spy & Cooktop.
Just a second. So something as basic as "human legible XML" is
not possible with MSXML 3.0?
Any other incarnations of MSXML that do better?
It is possible, for instance using XSLT, here is a JScript/WSH example
using Msxml2.DOMDocument.4.0 to first create a document in memory, then
a simple XSLT stylesheet is used to "pretty-print" that document and
then it is saved
var xmlDocument = new ActiveXObject('Msxml2.DOMDocument.4.0');
xmlDocument.appendChild(xmlDocument.createElement( 'gods'));
var element = xmlDocument.createElement('god');
element.setAttribute('name', 'Kibo');
xmlDocument.documentElement.appendChild(element);
element = xmlDocument.createElement('god');
element.setAttribute('name', 'Xibo');
xmlDocument.documentElement.appendChild(element);
WScript.Echo(xmlDocument.xml);
var indentedDocument = new ActiveXObject('Msxml2.DOMDocument.4.0');
var xslIndenter = new ActiveXObject('Msxml2.DOMDocument.4.0');
xslIndenter.async = false;
xslIndenter.load('test20040406Xsl.xml');
xmlDocument.transformNodeToObject(xslIndenter, indentedDocument);
WScript.Echo(indentedDocument.xml);
indentedDocument.save('test20040406.xml');
Output shows the original document first and then the indented one
<gods><god name="Kibo"/><god name="Xibo"/></gods>
<?xml version="1.0"?>
<gods>
<god name="Kibo"></god>
<god name="Xibo"></god>
</gods>
The simple XSLT stylesheet is
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" encoding="UTF-8" />
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
--
Martin Honnen
http://JavaScript.FAQTs.com/