使用Excel VBA编辑XML DOM的所有文本节点

时间:2015-11-25 15:58:01

标签: xml vba excel-vba vbscript xmldom

我正在尝试使用Excel VBA编辑XML DOM文件,但面临的问题超出了1级节点。 我的要求是修改所有(并且只有)Text节点,但我编写的脚本修改了所有1级节点。

请建议代码修改。

输入XML:

<?xml version="1.0" encoding="UTF-8"?>
<Envelope>
    <Body>
        <Request>
            <individual>
                <hasName>
                    <firstName>ABC</firstName>
                    <lastName>DEF</lastName>
                </hasName>
                <birthDate>2015-01-10</birthDate>
                <hasContact>
                    <type>Mobile</type>
                    <phoneAddress>
                        <countryCode>02</countryCode>
                        <localNumber>12345678</localNumber>
                    </phoneAddress>
                </hasContact>
            </individual>
        </Request>
    </Body>
</Envelope>

Excel VB脚本:

Option Explicit
Sub Test()
Dim xmlDoc
Dim colNodes
Dim objNode
Dim objNodesParam

Set xmlDoc = CreateObject ("Microsoft.XMLDOM")
xmlDoc.Async = "False"
xmlDoc.Load("H:\Desktop\Request.xml")
Set colNodes = xmlDoc.SelectNodes ("/Envelope/Body/Request/individual/*")

For Each objNode In colNodes
objNodeParam = "{{" & objNode.nodeName & "}}"
objNode.Text = objNodeParam
Next

xmlDoc.Save "H:\Desktop\Request.xml"

End Sub

以上脚本的实际输出:

  <?xml version="1.0" encoding="UTF-8"?>
  <Envelope>
    <Body>
        <Request>
            <individual>
                <hasName>{{hasName}}</hasName>
                <birthDate>{{birthDate}}</birthDate>
                <hasContact>{{hasContact}}</hasContact>
            </individual>
        </Request>
    </Body>
</Envelope>

预期产出:

<?xml version="1.0" encoding="UTF-8"?>
<Envelope>
    <Body>
        <Request>
            <individual>
                <hasName>
                    <firstName>{{firstName}}</firstName>
                    <lastName>{{lastName}}</lastName>
                </hasName>
                <birthDate>{{birthDate}}</birthDate>
                <hasContact>
                    <type>{{type}}</type>
                    <phoneAddress>
                        <countryCode>{{countryCode}}</countryCode>
                        <localNumber>{{localNumber}}</localNumber>
                    </phoneAddress>
                </hasContact>
            </individual>
        </Request>
    </Body>
</Envelope>

1 个答案:

答案 0 :(得分:1)

有关背景信息,请参阅here。您需要递归方法:

Option Explicit

Dim oFS    : Set oFS  = CreateObject("Scripting.FileSystemObject")
Dim sFSpec : sFSpec   = oFS.GetAbsolutePathName("..\data\33921005.xml")
Dim oXML   : Set oXML = CreateObject("Msxml2.DOMDocument.6.0")
oXML.load sFSpec
If 0 = oXML.parseError Then
   recursiveTraversal oXML.documentElement, 0
   WScript.Echo "==============="
   WScript.Echo oXML.xml
Else
   WScript.Echo objMSXML.parseError.reason
End If

Sub recursiveTraversal(oElm, nIndent)
  If oElm.nodeType = 3 Then
     WScript.Echo Space(nIndent), oElm.text
     oElm.text = "{{" & oElm.parentNode.tagName & "}}"
  Else
     WScript.Echo Space(nIndent), oElm.tagName
     If 0 < oElm.childNodes.length Then
        Dim oChild
        For Each oChild In oElm.childNodes
            recursiveTraversal oChild, nIndent + 2
        Next
     End If
  End If
End Sub

输出:

cscript 33921005.vbs
 Envelope
   Body
     Request
       individual
         hasName
           firstName
             ABC
           lastName
             DEF
         birthDate
           2015-01-10
         hasContact
           type
             Mobile
           phoneAddress
             countryCode
               02
             localNumber
               12345678
===============
<?xml version="1.0"?>
<Envelope>
        <Body>
                <Request>
                        <individual>
                                <hasName>
                                        <firstName>{{firstName}}</firstName>
                                        <lastName>{{lastName}}</lastName>
                                </hasName>
                                <birthDate>{{birthDate}}</birthDate>
                                <hasContact>
                                        <type>{{type}}</type>
                                        <phoneAddress>
                                                <countryCode>{{countryCode}}</countryCode>
                                                <localNumber>{{localNumber}}</localNumber>
                                        </phoneAddress>
                                </hasContact>
                        </individual>
                </Request>
        </Body>
</Envelope>