Moving data from one xml to another.RSS

1 reply

Last post Apr 03, 2019 07:07 AM by Jalpa Panchal

  • Moving data from one xml to another.

    Apr 02, 2019 07:17 PM|marmiterocks|LINK

    Hi,

    I have an xml file with over 1000 entries.  Each entry has about 18 fields and one of these fields is a date field.  What I need to do is to move the entries to a another xml based on the year within the date field. For example, so that all those entries with a 2018 year are in one xml and all those with a 2019 year are in another xml.

    The code I have seems to work but take a while to run, just wondering if there is anything obvious I am missing.  I have cut down the code here so it only shows 4 fields, but the code I am running has 18 fields.

    Thanks for any advice.

    Example code:

    <%
    //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." %>
    <script type="text/javascript">
    alert("<%=Message%>");
    </script>
    <%
    End if

    else

    //SHOW ERROR MESSAGE IF SOURCE FILE IS NOT FOUND.
    Message="Error - Source file can not be found." %>
    <script type="text/javascript">
    alert("<%=Message%>");
    </script>
    <%
    end If
    %>

  • Re: Moving data from one xml to another.

    Apr 03, 2019 07:07 AM|Jalpa Panchal|LINK

    Hi marmiterocks,

    Could please tell us what is your machine configuration? which OS you are using? 

    marmiterocks

    The code I have seems to work but take a while to run
    How long it takes to execute code? Could you please provide a source and target sample file?

    Regards,

    Jalpa.

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue.
    If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.