472,126 Members | 1,425 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,126 software developers and data experts.

write XML with line feeds?

Hello World,

any idea how I can convince MSXML.IXMLDOMDocument to
save the XML files using CR/LF?

I need to manually edit the XML files and it's
awkward if everything is in one huge line.


--
Arthur Hoornweg
(please remove the ".net" from my e-mail address)

Nov 12 '05 #1
4 11995
Looks like you are using the MSXML 3.0 or 4.0 via COM?

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.

-Naraen

---
"Arthur Hoornweg" <ar*************@wanadoo.nl.net> wrote in message
news:OM**************@TK2MSFTNGP11.phx.gbl...
Hello World,

any idea how I can convince MSXML.IXMLDOMDocument to
save the XML files using CR/LF?

I need to manually edit the XML files and it's
awkward if everything is in one huge line.


--
Arthur Hoornweg
(please remove the ".net" from my e-mail address)

Nov 12 '05 #2
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?
--
Arthur Hoornweg
(please remove the ".net" from my e-mail address)
Nov 12 '05 #3


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/

Nov 12 '05 #4
Without .NET, the method Martin mentioned is the way to go.

-Naraen

----
"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:O1**************@TK2MSFTNGP11.phx.gbl...


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/

Nov 12 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Frank Esser | last post: by
3 posts views Thread by towers | last post: by
3 posts views Thread by Anonymous | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.