excel vba - How to parse an XML file and write in data then save it -
i working on following:
- an excel sheet cells end user asked enter required values; done
- code read values entered in these cells end users;
- load xml file; done
- parse , write values retrieved excel cells xml file (following rule) save it; stuck here!
- start application (another script use xml file values later).
i simplify as possible:
the excel file following example...
cella cellb 1 t1 v1 2 t2 v2 3 t3 v3 4 t4 v4 5 t5 v5 6 t6 v6
"t" refers title. end user enter values v1, v2, ... v6
the xml file structured this:
<?xml version="1.0" encoding="utf-8" standalone="no"?> <environment> <variable> <name></name> <caption>t1</caption> <type>text</type> <value>v1</value> <description></description> </variable> <variable> <name></name> <caption>t2</caption> <type>text</type> <value>v2</value> <description></description> </variable> <variable> <name></name> <caption>t3</caption> <type>text</type> <value>v3</value> <description></description> </variable> <variable> <name></name> <caption>t4</caption> <type>text</type> <value>v4</value> <description></description> </variable> <variable> <name></name> <caption>t5</caption> <type>text</type> <value>v5</value> <description></description> </variable> </variable> <variable> <name></name> <caption>t6</caption> <type>text</type> <value>v6</value> <description></description> </variable> </environment>
as can see need parse file , enter values (v1....v6) reference each one.
below vba code until line stuck:
'option explicit private sub runtest_click() dim envfrmwrkpath string dim applicationname string dim testiterationname, serverip, login, password, traderlitelogpath string dim objfso, app, eval object dim i, msgarea`enter code here` dim envvarxml msxml2.domdocument60 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' '''these added when trying find way parse xml --- can change them dim variable msxml2.ixmldomnode 'dim oattributes msxml.ixmldomnamednodemap dim oroot msxml2.ixmldomnode dim objuielement msxml2.ixmldomelement dim ochildren msxml2.ixmldomnodelist dim ochild msxml2.ixmldomnode dim ovariable msxml2.ixmldomnode dim oattributes msxml2.ixmldomnamednodemap 'dim objuielement object dim field object '''''''''''''''''''''''''''''''''''''''''''''''''''' 'load env variables excel applicationname = activesheet.range("e4").value envfrmwrkpath = activesheet.range("e6").value testiterationname = activesheet.range("e8").value serverip = activesheet.range("e10").value login = activesheet.range("e12").value password = activesheet.range("e14").value traderlitelogpath = activesheet.range("e16").value 'load xml file set objparser = createobject("microsoft.xmldom") set envvarxml = new msxml2.domdocument60 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ''test_load '''''''''''''''----------------------------------------------------- 'set envvarxml = createobject("microsoft.xmldom") 'set envvarxml = new msxml2.domdocument60 'if envvarxml.load(envfrmwrkpath & "\environment\envvar.xml") ' debug 'msgbox "file loaded correctly", vbokonly ' debug 'else ' debug 'msgbox "file not loaded", vbcrtical ' debug 'end if '''''''''''''''----------------------------------------------------- ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'load xml file envvarxml.load (envfrmwrkpath & "\environment\envvar.xml") 'parse file , change values '''the following may have no sense experiment 1 of set oroot = envvarxml.documentelement each ovariable in oroot.childnodes set oattributes = ovariable.attributes set ochildren = ovariable.childnodes '''deleted many lines found no way ''''''' set envvarxml = nothing next envvarxml.save (envfrmwrkpath & "\environment\envvar.xml")
here code can started
dim doc domdocument set doc = new domdocument doc.load "c:\x.xml" dim variables ixmldomnodelist dim variable ixmldomnode set variables = doc.selectnodes("/environment/variable") each variable in variables debug.print variable.selectnodes("caption").item(0).text debug.print variable.selectnodes("type").item(0).text next
to make work, select tools - references , select microsoft xml v6.0. there many ways solve this, xpath (the language used in selectnodes) know in cases this.
Comments
Post a Comment