-1

我正在尝试修改 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>
4

1 回答 1

0

根据来自这里的信息:

由于 .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>
于 2012-05-17T12:43:51.730 回答