Solved

VB script to edit XML

Posted on 2014-04-03
8
243 Views
Last Modified: 2014-04-04
Hello Experts,

Can you provide an example way to insert an XML node into an existing XML file?

Currently there is a node within the XML like such: <MyFutureStuff /> and I would like to replace it with the contents of a text file.

The text file would include:

<MyFutureStuff>
      <AllMyFutureStuff>
            <Stuff>
     </AllMyFutureStuff>
</MyFutureStuff>

So, I would like the VB code to REPLACE <MyFutureStuff /> with the XML contents of the file AND if <MyFutureStuff /> doesn't exist, INSERT anyway...
0
Comment
Question by:zequestioner
  • 4
  • 3
8 Comments
 
LVL 27

Expert Comment

by:MacroShadow
ID: 39976019
If <MyFutureStuff /> doesn't exist where do you want to insert the new xml node?
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 39976165
This should do the job.  If the tag is there it will replace it, otherwise add the inserted file content to the end.  Adjust the file names near the top as needed.

' Define needed constants
Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2

' Define file locations
strMainFile = "main.xml"
strCopyFile = "copy.xml"
strReplace = "<MyFutureStuff />"

' Create file system object
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Read copy file into a string
Set objFile = objFSO.OpenTextFile(strCopyFile, ForReading, False, TriStateUseDefault)
strCopy = objFile.ReadAll
objFile.Close

' Read main file into a string
Set objFile = objFSO.OpenTextFile(strMainFile, ForReading, False, TriStateUseDefault)
strMain = objFile.ReadAll
objFile.Close

' Replace or add the copy file into the main file as needed
If Instr(1, strMain, strReplace, vbTextCompare) Then
   strMain = Replace(strMain, strReplace, strCopy)
Else
   strMain = strMain & strCopy
End If

' Rewrite file with any changes made
Set objFile = objFSO.OpenTextFile(strMainFile, ForWriting, True)
objFile.Write strMain
objFile.Close

Open in new window

~bp
0
 
LVL 1

Author Comment

by:zequestioner
ID: 39976504
BP, This works great! Thanks so much for your help!

Only one problem...

The node <MyFutureStuff/> actually exists within <MyXML>...

<MyXml>
   <SomeStuff>
      <MyFutureStuff/>
   </SomeStuff>
</MyXml>

Can you make an edit so that when inserting the XML, it always inserts inside <MyXml> and not at the end of the file? Again, it works great on replacing the text, but if the replacement text doesn't exist, it still needs to insert within <MyXml> and not at the end of the file...

Thanks again!!!
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 53

Expert Comment

by:Bill Prew
ID: 39976579
Yes, will be home in a bit and will change that.

~bp
0
 
LVL 1

Author Comment

by:zequestioner
ID: 39976662
Excellent! So sorry for missing that on the original scope.
0
 
LVL 1

Author Comment

by:zequestioner
ID: 39976967
Perhaps tomorrow? No rush! Thanks again!
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 39977015
Shortly.

~bp
0
 
LVL 53

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 39977065
Give this a try.

' Define needed constants
Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2

' Define file locations
strMainFile = "main.xml"
strCopyFile = "copy.xml"
strReplace = "<MyFutureStuff />"
strInsert = "</MyXml>"

' Create file system object
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Read copy file into a string
Set objFile = objFSO.OpenTextFile(strCopyFile, ForReading, False, TriStateUseDefault)
strCopy = objFile.ReadAll
objFile.Close

' Read main file into a string
Set objFile = objFSO.OpenTextFile(strMainFile, ForReading, False, TriStateUseDefault)
strMain = objFile.ReadAll
objFile.Close

' Replace or add the copy file into the main file as needed
If Instr(1, strMain, strReplace, vbTextCompare) Then
   strMain = Replace(strMain, strReplace, strCopy)
Else
   strMain = Replace(strMain, strInsert, strCopy & strInsert)
End If

' Rewrite file with any changes made
Set objFile = objFSO.OpenTextFile(strMainFile, ForWriting, True)
objFile.Write strMain
objFile.Close

Open in new window

~bp
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about a problem I had getting the GPS LocationListener working.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question