9

我有一个 MSXML2.DomDocument 实例。

我挥手保存它,缩进。

此代码有效,但不缩进:

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

我想我可以使用 MXXMLWriter 对象来注入缩进。

如何?

4

4 回答 4

7

这应该做。

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();
}

像这样使用它:

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);
}

我不知道如何调整缩进级别。它总是以 . 缩进tab

于 2012-06-21T19:24:11.543 回答
1

还有另一种美化 xml 输出的方法,另外您可以手动调整缩进级别: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();
}

缩进.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>
于 2012-06-29T17:23:33.483 回答
0

如果你不想使用 xsl,你可以插入 vbcrlfs。每个“>”都应该跟在 vbcrlf 之后,除非“>”后面跟一个数字。然后从该字符串创建新的 xml 文件 - 现在它有新的行和缩进。msaccess vba:

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

使用 objDom.append 创建文档,然后使用字符串(我的 xml 中只有数字):

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)
于 2014-06-24T08:42:00.663 回答
0

有点复杂??
对我不起作用(在 WSH / c脚本中) - 错误和空文件。
然后发现您可以使用保存方法-我的新过滤掉VS(空)C++过滤器脚本

var arg = WScript.arguments(0);
var doc = readFile(arg);
var xml = doc.documentElement.firstChild;
do {
    var filters = {};
    while (xml && xml.nodeName != "ItemGroup") xml = xml.nextSibling;
    if (!xml) break;
    var xml2 = xml.firstChild;
    while (xml2 && (xml2.nodeName == "ClInclude" || xml2.nodeName == "ClCompile"))
    {
        var path = xml2.attributes.getNamedItem("Include").nodeValue;
        var filter = xml2.firstChild;
        if (filter != null) {
            filter = filter.text;
        }
        if (filters[path])
        { //WScript.Echo(path + " had " + filter);
            if (!filter)
            {
                var prev = xml2.previousSibling;
                xml.removeChild(xml2);
                xml2 = prev;
            }
        } else {
            if (filter != null) {
                filters[path] = filter;
            }
        }
        xml2 = xml2.nextSibling;
    }
    xml = xml.nextSibling;
} while (xml);
doc.save(arg);

function readFile(filename)
{
    var xml = new ActiveXObject("Msxml2.DOMDocument.6.0");  
    xml.async = false;
    xml.resolveExternals = false;
    xml.async = false;
    xml.load(arg);
    return xml;
}
于 2022-01-26T09:10:31.680 回答