VB script to add element in XML

I have an XML file with attributes. I want to add elements to this with VB script.

As an example I want to add this element to my example XML file.
<category name="OP\Monofilter\100\Median">
<setting name="radius" type="string" value="1" />

Open in new window

Example XML
<?xml version="1.0"?>
<OPsettings version="1.0">
  <category name="OP\ListMonofilter">
    <setting name="1" type="string" value="Pan" />
  <category name="OP\Monofilter\1\Histogram">
    <setting name="Monomatic" type="boolean" value="1" />
    <setting name="low" type="string" value="0.001" />
    <setting name="high" type="string" value="1" />
    <setting name="gamma" type="string" value="1" />

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Let's assume the example input xml file is stored in the example.xml file.
Here is a vbscript which loads the xml from the file and then inserts a xml text into it.
option explicit
dim xml
set xml = CreateObject("Microsoft.XMLDOM")
xml.async = false
xml.load "example.xml"  ' load the example xml

dim xml2    ' load a xml segment to add to a separate xml tree
set xml2 = CreateObject("Microsoft.XMLDOM")
xml2.async = false
xml2.loadXML "<category name=""OP\Monofilter\100\Median""><setting name=""radius"" type=""string"" value=""1"" /></category>"

dim clone   ' create a xml node clone which is not bound to any xml document
set clone = xml2.documentElement.cloneNode( true )   ' true means clone with all the children

dim ops ' find a container node to insert the segment to
set ops = xml.selectSingleNode("OPsettings")

ops.insertBefore clone, ops.firstChild  ' insert the clone segment before the first child of the container

xml.Save "out.xml"  ' save the result

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mathiasoAuthor Commented:
Thanks for your explanation. Is there a way to get line breaks before "<setting name..." this would make the XML easier to read
In vbscript you can insert the CR and LF characters to a string like follows:

xml2.loadXML "<category name=""OP\Monofilter\100\Median"">" & vbCrLf & "<setting name=""radius"" type=""string"" value=""1000"" />" & vbCrLf & "</category>"

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.