我正在尝试修改XML节点名称而不是其值。我无法做到这一点。此外,我必须修改父节点名称。请帮忙。
示例XML就在这里。在这个XML中,我需要将ICOSTx修改为ICOS_ORDER_TX。请有人帮忙做到这一点。我必须修改大约50K xmls。尽快帮助我。如果可能,请将代码邮寄给我。 ipand.19@gmail.com。如果还有VBscript以外的代码,请告诉我。
<ICOSTx SOURCETYPE="XX" >
<HEADER>
<ID> 49909171</ID>
<EMP>P9004952</EMP>
<STARTDT>10/04/2012 14:23:04</STARTDT>
<TOTAL>5849.59</TOTAL>
<CNP>1</CNP>
</HEADER>
</ICOSTx>
答案 0 :(得分:0)
基于here的信息:
由于.tagName是只读的,因此您必须创建一个具有合适名称的新节点,克隆要更改的节点,并将旧节点替换为新节点。在代码中:
Dim sFSpec : sFSpec = resolvePath( "..\data\00.xml" )
WScript.Echo goFS.OpenTextFile(sFSpec).ReadAll()
Dim oXDoc : Set oXDoc = CreateObject( "Msxml2.DOMDocument" )
oXDoc.setProperty "SelectionLanguage", "XPath"
oXDoc.async = False
oXDoc.load sFSpec
If 0 = oXDoc.ParseError Then
WScript.Echo sFSpec, "looks ok"
Dim sXPath, ndFnd
sXPath = "/ICOSTx"
Set ndFnd = oXDoc.selectSingleNode( sXPath )
If ndFnd Is Nothing Then
WScript.Echo "|", sXPath, "| not found"
Else
WScript.Echo "found |" & ndFnd.tagName & "|"
On Error Resume Next
ndFnd.tagName = "ICOS_ORDER_TX"
WScript.Echo "** Bingo:", Err.Description
On Error Goto 0
Dim ndParent : Set ndParent = ndFnd.parentNode
Dim ndNew : Set ndNew = oXDoc.createElement("ICOS_ORDER_TX")
ndNew.appendChild ndFnd.firstChild.cloneNode(True)
ndParent.removeChild ndFnd
ndParent.appendChild ndNew
sFSpec = resolvePath( "..\data\02.xml" )
oXDoc.save sFSpec
WScript.Echo goFS.OpenTextFile(sFSpec).ReadAll()
End If
Else
WScript.Echo oXDoc.ParseError.Reason
End If
输出:
<ICOSTx SOURCETYPE="XX">
<HEADER>
<ID>49909171</ID>
<EMP>P9004952</EMP>
<STARTDT>10/04/2012 14:23:04</STARTDT>
<TOTAL>5849.59</TOTAL>
<CNP>1</CNP>
</HEADER>
</ICOSTx>
E:\trials\SoTrials\answers\10632220\data\00.xml looks ok
found |ICOSTx|
** Bingo: Wrong number of arguments or invalid property assignment
<ICOS_ORDER_TX><HEADER>
<ID>49909171</ID>
<EMP>P9004952</EMP>
<STARTDT>10/04/2012 14:23:04</STARTDT>
<TOTAL>5849.59</TOTAL>
<CNP>1</CNP>
</HEADER>
</ICOS_ORDER_TX>