Solved

VB script to edit XML

Posted on 2014-04-03
8
239 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 52

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
 
LVL 52

Expert Comment

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

~bp
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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 52

Expert Comment

by:Bill Prew
ID: 39977015
Shortly.

~bp
0
 
LVL 52

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article will show, step by step, how to integrate R code into a R Sweave document
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

911 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now