问题描述
我有一个包含1000多个条目的xml文件。每个条目都有一个日期字段,我需要做的是分解这个xml文件,以便所有带有2018日期字段的条目都在一个xml文件中,所有带有2019数据字段的条目都在另一个等等,等等。每个条目有大约18个字段。
我写的代码有效但需要的时间比我想象的要长。只是想知道我是否遗漏了任何明显可能会减慢速度的东西。如果有人有任何建议他们可以提供这将是伟大的。
非常感谢,
我尝试过:
减少代码的版本是:
<%
//检查源文件是否存在。
Dim SourceFile
设置SourceFile = Server.CreateObject(Scripting.FileSystemObject )
如果SourceFile.FileExists(Server.MapPath(source.xml))那么
//检查目标文件是否存在。
Dim TargetFile
设置TargetFile = Server.CreateObject(Scripting.FileSystemObject)
如果是TargetFile.FileExists(Server.MapPath( target_2018.xml))然后
//打开源XML
设置SourceXml = Server.CreateObject(MSXML2.DOMDocument.3.0 )
SourceXml.Async =False
SourceXml.load Server.MapPath(source.xml)
设置colNodes = SourceXml.selectNodes(// person)
For Each o bjSite in colNodes
Field1 = objSite.selectSingleNode(Field1)。text
Field2 = objSite.selectSingleNode(Field2)。text
Field3 = objSite.selectSingleNode(Field3)。text
Field4 = objSite.selectSingleNode(Field4)。text
/ /找到源XML中的匹配记录并删除它。
// FIELD4是一个日期,匹配标准是年份。
Dim strLast:strLast = Right(Field4 ,4)
如果strLast =2018那么
objSite.parentNode.removeChild(objSite)
//获取TARGET.XML文件中的最高ID。
i = 1
设置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
每个xmlProduct在TargetXml.documentElement.selectNodes(person)
CurrentEntryId = xmlProduct.selectSingleNode(Entryid)。text
if CurrentEntryId> = CurrentHighestId then
CurrentHighestId = CurrentEntryId + 1
结束如果
i = i + 1
next
Dim Entryid
Entryid = CurrentHighestId
//将匹配记录添加到目标文件中。
昏暗根
设置root = TargetXml.documentElement
设置newperson = TargetXml.createNode(element,person,)
Dim newtextbox0
Dim newtextbox1
Dim newtextbox2
Dim newtextbox3
设置newtextbox0 = TargetXml.createNode(element,Field1,)
Set newtextbox1 = TargetXml.createNode(element,Field2,)
设置newtextbox2 = TargetXml.createNode(element,Field3,)
设置newtextbox3 = TargetXml.createNode(element,field4,)
newtextbox0.text = Entryid
newtextbox1.text = Field2
蝾螈extbox2.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
结束如果
//保存对源文件的更改。
SourceXml.save Server.MapPath(source.xml)
下一页
//如果找不到目标文件,则显示错误消息。
else
消息=错误 - 目标文件不能是找到。 %>
alert(<%= Message%>);
< ;%
结束如果
else
//显示错误消息如果未找到源文件。
消息=错误 - 找不到源文件。 %>
alert(<%= Message%>);
< ;%
结束如果
%>
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
%>
推荐答案
我写的代码有效但需要的时间比我想象的要长。只是想知道我是否遗漏了任何明显可能会减慢速度的东西。如果有人有任何建议他们可以提供这将是伟大的。
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.
For colNodes中的每个objSite
...
设置TargetXml = Server.CreateObject(MSXML2.DOMDocument.3.0)
每个xmlProduct在TargetXml.documentElement.selectNodes(person)
...
next
...
NEXT
For Each objSite in colNodes
...
Set TargetXml = Server.CreateObject("MSXML2.DOMDocument.3.0")
For Each xmlProduct In TargetXml.documentElement.selectNodes("person")
...
next
...
NEXT
这篇关于将数据从一个XML移动到另一个XML。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!