Click here to Skip to main content
15,889,838 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have an xml file with over 1000 entries. Each entry has a date field and what I need to do is break down this xml file so that all entries with a date field for 2018 are in one xml file, all entries with a data field of 2019 are in another, etc, etc. Each entry has about 18 fields.

The code which I have written works but takes longer than I thought it would. Just wondering if I have missed anything obvious that could be slowing it down. If anyone has any advise they can offer that would be great.


Many thanks,

What I have tried:

Cut down version of the code is:
<%
//CHECK THAT THE SOURCE FILE EXISTS.
Dim SourceFile
Set SourceFile = Server.CreateObject("Scripting.FileSystemObject")
If SourceFile.FileExists(Server.MapPath("source.xml")) Then

//CHECK THAT THE TARGET FILE EXISTS.
Dim TargetFile
Set TargetFile = Server.CreateObject("Scripting.FileSystemObject")
If TargetFile.FileExists(Server.MapPath("target_2018.xml")) Then

//OPEN THE SOURCE XML
Set SourceXml = Server.CreateObject("MSXML2.DOMDocument.3.0")
SourceXml.Async = "False"
SourceXml.load Server.MapPath("source.xml")

Set colNodes = SourceXml.selectNodes ("//person")
For Each objSite in colNodes
Field1=objSite.selectSingleNode("Field1").text
Field2=objSite.selectSingleNode("Field2").text
Field3=objSite.selectSingleNode("Field3").text
Field4=objSite.selectSingleNode("Field4").text

//FIND THE MATCHING RECORD IN THE SOURCE XML AND REMOVE IT.
//FIELD4 IS A DATE, MATCHING CRITERIA IS THE YEAR.
Dim strLast: strLast = Right(Field4, 4)
If strLast = "2018" Then
objSite.parentNode.removeChild(objSite)

//GET THE HIGHEST ID IN THE TARGET.XML FILE.
i = 1
Set TargetXml = Server.CreateObject("MSXML2.DOMDocument.3.0")
TargetXml.async = False
TargetXml.load Server.MapPath("target_2018.xml")
Dim xmlProduct
Dim CurrentHighestId
Dim CurrentEntryId
CurrentHighestId = 1
For Each xmlProduct In TargetXml.documentElement.selectNodes("person")
CurrentEntryId = xmlProduct.selectSingleNode("Entryid").text
if CurrentEntryId >= CurrentHighestId then
CurrentHighestId = CurrentEntryId + 1
end if
i = i + 1
next
Dim Entryid
Entryid = CurrentHighestId

//ADD THE MATCHING RECORD TO THE TARGET FILE.
Dim root
Set root = TargetXml.documentElement
Set newperson = TargetXml.createNode("element", "person", "")
Dim newtextbox0
Dim newtextbox1
Dim newtextbox2
Dim newtextbox3
Set newtextbox0 = TargetXml.createNode("element", "Field1", "")
Set newtextbox1 = TargetXml.createNode("element", "Field2", "")
Set newtextbox2 = TargetXml.createNode("element", "Field3", "")
Set newtextbox3 = TargetXml.createNode("element", "field4", "")
newtextbox0.text = Entryid
newtextbox1.text = Field2
newtextbox2.text = Field3
newtextbox3.text = Field4
newperson.appendChild(newtextbox0)
newperson.appendChild(newtextbox1)
newperson.appendChild(newtextbox2)
newperson.appendChild(newtextbox3)
root.appendChild(newperson)
TargetXml.save(Server.Mappath("target_2018.xml"))
else
End if

//SAVE THE CHANGES MADE TO THE SOURCE FILE.
SourceXml.save Server.MapPath("source.xml")

Next

//SHOW ERROR MESSAGE IF TARGET FILE IS NOT FOUND.
else
Message="Error - Target file can not be found." %>

alert("<%=Message%>");

<%
End if

else

//SHOW ERROR MESSAGE IF SOURCE FILE IS NOT FOUND.
Message="Error - Source file can not be found." %>

alert("<%=Message%>");

<%
end If
%>
Posted
Updated 3-Apr-19 8:44am
Comments
RedDk 3-Apr-19 14:30pm    
... and that code looks like? Noone is going to offer up a solution to this problem without a poster showing some code. If you got an error message use a debugger to find where it occured. I type the exact error message along with quotation marks into a search engine and always get back returns. This "error message" you have here is a trap built into the code. If you're not getting errors, what are you getting?

1 solution

Quote:
The code which I have written works but takes longer than I thought it would. Just wondering if I have missed anything obvious that could be slowing it down. If anyone has any advise they can offer that would be great.


Bad design. Building and destroying. Over and over.

Quote:
For Each objSite in colNodes
...

Set TargetXml = Server.CreateObject("MSXML2.DOMDocument.3.0")

For Each xmlProduct In TargetXml.documentElement.selectNodes("person")
...
next

...
NEXT
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900