vbscript - Add child entry to a specific node in xml file in vbscipt -


i have xml file dataconfiguration.xml entry

<datasource>  <localdata>     <add context="localization">        <parameter name="timeout" type="int" defaultvalue="60"/>        <parameter name="address" type="string" defaultvalue="192.168.9.45" />        <parameter name="port" type="int" defaultvalue="6789"/>     </add> </localdata> </datasource> 

i need add entry "localdata"

 <datasource>      <localdata>         <add context="localization">            <parameter name="timeout" type="int" defaultvalue="60"/>            <parameter name="address" type="string" defaultvalue="192.168.9.45" />            <parameter name="port" type="int" defaultvalue="6789"/>         </add>        <add context="general">            <parameter name="timeout" type="int" defaultvalue="60"/>            <parameter name="address" type="string" defaultvalue="192.168.9.478" />            <parameter name="port" type="int" defaultvalue="5674"/>         </add>     </localdata>     </datasource> 

how add in vbscript?

my current code

'created xml file object set xmldoc = createobject("msxml2.domdocument")  xmldoc.async = false   xmldoc.preservewhitespace= true  xmldoc.load("dataconfiguration.xml")  dim entry  entry = "<add context=""general"">" & _                <parameter name=""timeout"" type=""int"" defaultvalue=""60""/>" & _                 <parameter name=""address"" type=""string"" defaultvalue=""192.168.9.478"" />" & _                <parameter name=""port"" type=""int"" defaultvalue=""5674""/>"& _             </add>"  set newnode = xmldoc.createelement(entry) set elemlist = xmldoc.getelementsbytagname("localdata") elemlist.appendchild(newnode) 

but give error

this name may not contain < character" @ " set newnode = xmldoc.createelement(entry)

also elemlist.appendchild(newnode) not work.

xmldocument.createelement accepts 3 params: node type, node name, , namespace. in example, since child element named "add", it's element (type==1), , part of global xml namespace, call xmldoc.createelement(1, "add", "") .

that gives empty element. insert data want (the context="general" attribute, , child elements), you'd need make successive calls dom manipulation methods, add in each child element, each attribute, , on. pretty laborious.

but have xml fragment string. instead of creating element using dom methods, can create 2nd xmldocument , tell content string. grab documentelement 2nd doc. call appendchild on appropriate node in first doc, passing documentelement 2nd doc.

something this:

function getelementfromxmlstring(xmlstring)     dim doc     set doc = createobject("msxml2.domdocument.6.0")     doc.async = false     doc.preservewhitespace= false     doc.loadxml(xmlstring)     set getelementfromxmlstring = doc.documentelement end function  sub main()     set doc1 = createobject("msxml2.domdocument.6.0")     doc1.async = false     doc1.preservewhitespace= false ' true     doc1.load("dataconfiguration.xml")      ' generate element xml string     dim xmlstring     xmlstring = "<add context=""general"">" & _                   " <parameter name=""timeout"" type=""int"" defaultvalue=""60""/>" & _                   " <parameter name=""address"" type=""string"" defaultvalue=""192.168.9.478"" />" & _                   " <parameter name=""port"" type=""int"" defaultvalue=""5674""/>"& _               "</add>"     dim newelt     set newelt = getelementfromxmlstring(xmlstring)      ' first child node of type=element under document root element in     ' doc1.  not same  doc1.documentelement.firstchild.  there can     ' text nodes, etc.     dim node1     set node1 = doc1.documentelement.selectsinglenode("./*[position()=1]")      ' append element node     node1.appendchild(newelt)      wscript.echo (prettyprintxml (doc1)) end sub  main() 

...where prettyprintxml function defined this:

function prettyprintxml(xmldoc)     dim reader     set reader = createobject("msxml2.saxxmlreader.6.0")     dim writer     set writer = createobject("msxml2.mxxmlwriter.6.0")     writer.indent = true     writer.omitxmldeclaration = true     reader.contenthandler = writer     reader.putproperty "http://xml.org/sax/properties/lexical-handler", writer     reader.parse(xmldoc)     prettyprintxml = writer.output end function 

the output of this, me, is:

<datasource>   <localdata>     <add context="localization">       <parameter name="timeout" type="int" defaultvalue="60"/>       <parameter name="address" type="string" defaultvalue="192.168.9.45"/>       <parameter name="port" type="int" defaultvalue="6789"/>     </add>     <add context="general">       <parameter name="timeout" type="int" defaultvalue="60"/>       <parameter name="address" type="string" defaultvalue="192.168.9.478"/>       <parameter name="port" type="int" defaultvalue="5674"/>     </add>   </localdata> </datasource> 

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -