Link to home
Start Free TrialLog in
Avatar of Dale Fye
Dale FyeFlag for United States of America

asked on

Update on question: Merge XML Documents

Recently received assistance with VBScript to merge XML documents from a folder into a single, new XML document.

The critical element in that discussion was that I needed to add an element to each node in the new XML document which contains the name of the file that the original node was found in.  Now I have found that occasionally, the original XML file might contain more than one node (order).  But I've found that in these instances, although all of the nodes from the original file are present in the new XML file, only one of them contains the new "FileName" element.

My modifications to Bill Prew's code look like:

Public Sub MergeOrders()
	Dim oFileSys
	Dim oFolder
	Dim aFiles
	Dim file
	Dim xmlPath1
	Dim xmlPath2
	Dim xmldoc1: Set xmldoc1 = CreateObject("MSXML2.DomDocument")
	Dim xmldoc2: Set xmldoc2 = CreateObject("MSXML2.DomDocument")
	Dim Orders
	Dim Order
	Dim NodeToRemove
	Dim strZeroLenFiles
	Dim NodeFileName
	Dim node
	Dim D2 
	
	Set NodeToRemove = xmldoc1.selectNodes("/OperatorName/FileName")
	
	Set oFileSys = WScript.CreateObject("Scripting.FileSystemObject")
	Set oFolder = oFileSys.GetFolder(ftpLocalDir)
	Set aFiles = oFolder.Files
	strZeroLenFiles = ""
	
	For Each file In aFiles

		If file.size = 0 then
		
			if strzerolenfiles <> "" then strZeroLenFiles = strZeroLenFiles & vbCrLf
			strZeroLenFiles = strZeroLenFiles & File.Name
			
		Else 
	
			If len(xmlPath1) = 0 then
				xmlPath1 = oFolder +"\" + file.name
				xmldoc1.async = False
				xmldoc1.load xmlpath1		
				xmldoc1.setProperty "SelectionLanguage", "XPath"
				
				'Note: Dale Fye 2018-06-11
				'Added following 5 lines to add Filename to the Orders node for XML Authentication
				Set NodeFileName = xmldoc1.selectSingleNode("/OperatorName/FileName")
				set Orders = xmldoc1.selectnodes("OperatorName/Order")
				For each Order in Orders
					set Order = Order.appendChild(NodeFileName )
				Next
				'End addition for XML authentication 
				
			Else
				xmlPath2 = oFolder +"\" + file.name 
			End If
			
			If len(xmlPath1) > 0 And len(xmlPath2) > 0 Then

				xmldoc2.async = False
				xmldoc2.load xmlpath2
				
				'Note: Dale Fye 2018-06-11
				'Added following 1 line to add Filename to the Orders node for XML Authentication
				Set NodeFileName = xmldoc2.selectsinglenode("/OperatorName/FileName")
				'End addition of 2018-06-11
				
				set Orders = xmldoc2.selectNodes("/OperatorName/Order")
				'Note: Dale Fye 2018-06-11
				'Added following 3 line to add Filename to the Orders node for XML Authentication
				For each order in orders
					set order = order.appendchild(NodeFileName)
				next
				'End addition of 2018-06-11
				
				For Each Order In Orders
					Set D2 = Order.cloneNode(True)
					xmldoc1.documentElement.appendChild D2
				Next
			End If

		End If 'zero len file
	Next

	For Each node in NodeToRemove
		node.parentNode.removeChild(node)
	Next

	xmldoc1.save mergedFileLocation

End Sub 

Open in new window


Any help would be greatly appreciated.
Avatar of Bill Prew
Bill Prew

Dale,

Can you provide a sample XML input file that exhibits the problem, for testing here?


»bp
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dale Fye

ASKER

Bill,

Yeah, I could post an example XML file, but it will be a while, headed out for about an hour.

Thanks
Don't need it, the fix I posted should solve the problem.


»bp
Still testing, but It appears to have resolved the issue.

The only change I saw was this line?
Order.appendChild(NodeFileName.CloneNode(True))

Open in new window

Dale
Thanks, Bill.  That worked like a charm.
Bill,

That didn't work after all.  It appeared to work in testing, when I only processed the file with multiple orders, but when I processed multiple files, it did not work.

Dale
0000013261_TestFile_171298052_201806.xml
0000013261_TestFile_171297521_201806.xml
Did you change both of the script lines I changed, that read:

Order.appendChild(NodeFileName.CloneNode(True))


»bp
Uhhhhh, no  

;-(

Get back to you
Thanks, again!

That's the problem with code windows, you cannot highlight the rows that were changed.
Welcome Dale, glad that sorted it out.


»bp