Solved

Replace XML Element Content using VB

Posted on 2007-11-17
3
727 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

729 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