Solved

VB script to edit XML

Posted on 2014-04-03
8
237 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 26

Expert Comment

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

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 51

Expert Comment

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

~bp
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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 51

Expert Comment

by:Bill Prew
ID: 39977015
Shortly.

~bp
0
 
LVL 51

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

760 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

21 Experts available now in Live!

Get 1:1 Help Now