How can I save an MSXML2.DomDocument with indentin

2019-02-13 07:57发布

问题:

I have an instance of MSXML2.DomDocument.

I wave to save it, with indenting.

This code works, but does not indent:

var dom = new ActiveXObject("MSXML2.DomDocument");
// fiddle with dom here
dom.save(filename);

I think I can use an MXXMLWriter object to inject indenting.

How?

回答1:

This oughta do it.

function saveDomWithIndent(dom, filename) {
    var writer =  new ActiveXObject("MSXML2.MXXMLWriter"),
        reader = new ActiveXObject("MSXML2.SAXXMLReader"),
        fso = new ActiveXObject("Scripting.FileSystemObject"),
        textStream = fso.CreateTextFile(filename, true);
    writer.indent = true;
    writer.omitXMLDeclaration = true;
    reader.contentHandler = writer;
    reader.parse(dom);
    textStream.Write(writer.output);
    textStream.Close();
}

Use it like this:

var root, node, newnode, 
    dom = new ActiveXObject("MSXML2.DOMDocument.6.0");
dom.async = false;
dom.resolveExternals = false;
dom.load(fullpath);
root = dom.documentElement;
node = root.selectSingleNode("/root/node1");
if (node !== null) {
    newnode = dom.createElement('node2');
    newnode.text = "hello";
    root.appendChild(newnode);
    saveDomWithIndent(dom, fullpath);
}

I could not figure out how to adjust the indent level. It always indents with a tab.



回答2:

There is another way to prettifying xml outputs, plus you can adjust the indent level manually : XSL.

var adSaveCreateOverWrite = 2
var Indent = new ActiveXObject("MSXML2.DomDocument");
    Indent.async = false;
    Indent.resolveExternals = false;
    Indent.load("indent.xsl");
var Doc = new ActiveXObject("MSXML2.DomDocument");
    Doc.async = false;
    Doc.resolveExternals = false;
    Doc.load("dirty.xml");
with(new ActiveXObject("ADODB.Stream")){
    Charset = "utf-8";
    Open();
    WriteText(Doc.transformNode(Indent));
    SaveToFile("pretty.xml", adSaveCreateOverWrite);
    Close();
}

indent.xsl

<?xml version="1.0" encoding="ISO-8859-15"?>
<!-- http://x443.wordpress.com/2011/page/34/ -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="xml"/>

  <xsl:template match="@*">
    <xsl:copy/>
  </xsl:template>

  <xsl:template match="text()">
    <xsl:value-of select="normalize-space(.)" />
  </xsl:template>

  <xsl:template match="*">
    <xsl:param name="indent" select="''"/>
    <xsl:text>&#xa;</xsl:text>
    <xsl:value-of select="$indent" />
    <xsl:copy>
      <xsl:apply-templates select="@*|*|text()">
        <xsl:with-param name="indent" select="concat($indent, '  ')"/>
      </xsl:apply-templates>
    </xsl:copy>
    <xsl:if test="count(../*)>0 and ../*[last()]=.">
      <xsl:text>&#xa;</xsl:text>
      <xsl:value-of select="substring($indent,3)" />
    </xsl:if>
  </xsl:template>

</xsl:stylesheet>


回答3:

If you don't want to use xsl, you could just insert vbcrlfs. Every ">" should follow vbcrlf, except when ">" is followed by a number. Then create new xml file from that string - now it has new lines and indents. msaccess vba:

Dim objDom As DOMDocument
Set objDom = CreateObject("MSXML2.DOMDocument")

create document with objDom.append, then play with string(my xml had just numbers):

ss = objectDom.XML
For i = 1 To Len(ss)
c = Mid(ss, i, 1)
    If InStr(1, Mid(ss, i, 1), ">") > 0 Then
        a = Asc(Mid(ss, i + 1, 1))
        If a < 48 Or a > 57 Then
            ss1 = Mid(ss, 1, i)
            ss2 = Mid(ss, i + 1, Len(ss))
            ss = Mid(ss, 1, i) & vbCrLf & Mid(ss, i + 1, Len(ss))
        End If
    End If

Next i

objDom.loadXML ss
objDom.Save (file_path)