WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

OLE with XML

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Replace Existing Element with New Data

 Keywords:  

Question:

I am in the middle of creating a winbatch script which should be able to handle XML files. My problem is to find the correct syntax on how to replace an existing element with new data.

0813,0413,0409,0809,040c,0407
0813,0413,0409,0809,040c,0407
0813:00000813,0413:00000413,0409:00000409,0809:00000809,040c:0000040c,0407:00000407
So in this case to change the different locales for the WEBED6200 element. Can I replace the current element or do I need to delete the element first and than recreate it?

This is besically what I do for the moment:

oDOM = ObjectOpen( "MSXML2.DOMDocument.3.0" )
oDOM.load(file)

oRoot=oDOM.documentElement
oPCS=oRoot.firstchild

oPC=oDOM.getElementsByTagName(PC)
oTST=oPC.length

if oTST == 1 then
	display(1,"WARNING","Computer already exists")
	goto RunProgram
else
	oPCNAME=oDOM.CreateElement(PC)
	oPCS.AppendChild(oPCNAME)
	o1 = oDOM.CreateElement( "USERLOCALE" )
	o1.Text = "%user%"
	oPCNAME.AppendChild( o1 )
	o1 = oDOM.CreateElement( "SYSTEMLOCALE" )
	o1.Text = "%system%"
	oPCNAME.AppendChild( o1)
	o1 = oDOM.CreateElement( "INPUTLOCALE" )
	o1.Text = "%input%"
	oPCNAME.AppendChild( o1)
end if
Any help would be greatly appreciated.

Answer:

Here is some code that edits the existing XML file ....
file = "c:\temp\example.xml"
outfile = "c:\temp\exampleout.xml"
;element to look for
PC = "WEBED6200"

;Insert data into an array
user = "Deana"
System = WinVersion(5)
input = "Input test"
datastr = StrCat(user,",",system,",",input)
data = Arrayize(datastr, ",")

xmlDoc = ObjectOpen("Msxml2.DOMDocument.3.0");
xmlDoc.async = @false;
xmlDoc.resolveExternals = @false; 
xmlDoc.load(file);
oRoot = xmlDoc.documentElement; 
oPC=oRoot.getElementsByTagName(PC)

If oPC.length>0
	hEnum = ObjectCollectionOpen(oPC)
	While 1
	  oNode= ObjectCollectionNext(hEnum)
	  If oNode == 0 Then Break
	  Message("",oNode.xml)
	  oNodeList = oNode.childNodes
	  len = oNodeList.length
		for i = 0 to len-1 
			oItem = oNodeList.item(i);
  			Message("Original XML data",oItem.xml);
			oItem.Text = data[i]
			Message("New XML data",oItem.xml);
		next
		ObjectClose(oItem)
		ObjectClose(oNodeList)
	   ObjectClose(oNode)
	EndWhile
	ObjectCollectionClose(hEnum)
endif

xmlDoc.Save(outfile)
ObjectClose(oPC)
ObjectClose(oRoot)
ObjectClose(xmlDoc)

exit

User Reply:

Thank you. It works like a charm!

Article ID:   W15668
File Created: 2013:01:03:11:45:46
Last Updated: 2013:01:03:11:45:46