将XML条目从一个文件移动到另一个文件

时间:2019-04-02 20:01:40

标签: xml vbscript asp-classic

我有一个包含1000多个条目的XML文件。每个条目都有大约18个字段,其中一个字段是日期字段。我需要做的是根据日期字段中的年份将条目移动到另一个XML。例如,所有具有2018年年份的条目都使用一种XML,而所有具有2019年年份的条目都使用另一种XML。

我的代码似乎正常工作,但是要花一些时间才能运行,只是想知道我是否缺少任何明显的东西。我在这里减少了代码,因此只显示4个字段,但是我正在运行的代码有18个字段。

<%
Dim SourceFile
Set SourceFile = Server.CreateObject("Scripting.FileSystemObject") 
If SourceFile.FileExists(Server.MapPath("source.xml")) Then
    Dim TargetFile
    Set TargetFile = Server.CreateObject("Scripting.FileSystemObject") 
    If TargetFile.FileExists(Server.MapPath("target_2018.xml")) Then
        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)

                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

                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

            SourceXml.Save Server.MapPath("source.xml")
        Next
    Else
        Message = "Error - Target file can not be found."
%>
<script type="text/javascript">
alert("<%=Message%>");
</script>
<%
    End If
Else
    Message = "Error - Source file can not be found." %>
<script type="text/javascript">
alert("<%=Message%>");
</script>
<%
End If
%>

0 个答案:

没有答案