Solved

Replace XML Element Content using VB

Posted on 2007-11-17
3
721 Views
Last Modified: 2013-11-26
Hi,

I am new to XML and have a couple of questions. I have a XML document. How can I replace the content of the elements using VB? I want to replace the content with another similar XML document. Some of the information will be there, and some not. I hope this is enough information to get started.

Here is a small sample of my XML:

<Core>

<title>DocTitle</title>

<publisher>Who Published</publisher>

<date>0</date>

<format>text/xml</format>

<language>EN</language>

<rights>Who has rights<rights>

</Core>

Open in new window

0
Comment
Question by:ALawrence007
  • 2
3 Comments
 
LVL 19

Accepted Solution

by:
weellio earned 500 total points
ID: 20304766
here is one i use.
 
you can either script it or at commandline you can just type
"cscript replace.vbs C:\Scripts\file.xml Bob superman"
and it will replace all instances of bob with superman

Const ForReading = 1

Const ForWriting = 2
 

If Wscript.Arguments.Count <> 3 Then

WScript.Echo "example" & VbCrLf & "cscript replace.vbs C:\Scripts\Text.txt Jim James"

WScript.Quit

End if
 

strFileName = Wscript.Arguments(0)

strOldText = Wscript.Arguments(1)

strNewText = Wscript.Arguments(2)
 

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFile = objFSO.OpenTextFile(strFileName, ForReading)
 

strText = objFile.ReadAll

objFile.Close

strNewText = Replace(strText, strOldText, strNewText)
 

Set objFile = objFSO.OpenTextFile(strFileName, ForWriting)

objFile.WriteLine strNewText

objFile.Close

Open in new window

0
 

Author Comment

by:ALawrence007
ID: 20304851
Thanks weellio,

Would this work if I want to replace just the title maybe or maybe the title and the publisher?

<Core>
<title>DocTitle</title>
<publisher>Who Published</publisher>
0
 
LVL 19

Expert Comment

by:weellio
ID: 20308939
to make sure i understand correctly,.
do you just want to replace one word with another word?
if so, then the script above will do exactly what you need

start - run
type "replace.vbs C:\Scripts\file.xml Doctitle newdoctitle"
and all instances will be replaced.

or you can just open the file in notepad and click edit-replace



if you want a script to seach and find <title>sometitle</title> then replace everying between the 'title' fields, then i'll need to write a script that is a little different.
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

Suggested Solutions

Title # Comments Views Activity
C# Error - Add Failed 12 78
What's the most efficient way to load up a couple of large table in C#? 5 37
Not showing JavaScript in the list 5 37
SQL Exceptions 3 37
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

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

25 Experts available now in Live!

Get 1:1 Help Now