-1

I am trying to modify the XML node name not its value. I could nt do it.moreover i have to modify parent node name. Pls help it.

Sample XML is here. in this XML, i need to modify ICOSTx into ICOS_ORDER_TX. Pls somebody help to do this. I have to modify around 50K xmls.Help me as soon as possible. If possible mail me the code. ipand.19@gmail.com. Please let me know if there is code other than VBscript also.

         <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>
kavin
  • 1
  • 1
  • 3

1 Answers1

0

Based on info from here:

As the the .tagName is read-only, you'l have to create a new node with a suitable name, clone the node to change, and replace the old with the new node. In code:

  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

output:

<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>
Community
  • 1
  • 1
Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96
  • hi Ekkehard..thanks for your code. but i am getting the following error while executing the same.pls help me.give me complete details.i want to update many xmls present in particular folder. Please find the error : Microsoft VBScript runtime error: Type mismatch: 'resolvePath – kavin May 18 '12 at 05:25
  • @kavin resolvePath() is just my function to get an absolute path; hardcode your file spec, use FSO.GetAbsolutePathName, or roll your own. – Ekkehard.Horner May 18 '12 at 09:00