I have the following asp-classic code which appends 3 variables into an XML document, 'location_x', 'location_y' and 'date_and_time'.
我有以下asp-classic代码,它将3个变量附加到XML文档中,'location_x','location_y'和'date_and_time'。
The ASP-Classic code - creates or appends an existing XML document.
ASP-Classic代码 - 创建或附加现有的XML文档。
Function LoadObjecttoXML(strXMLFilePath, strFileName)
Dim objDom
Dim objGpx
Dim objWpt
Dim objRte
Dim objRtept
Dim objDateTime
Dim objattLat
Dim objattLon
Dim objPI
Dim blnFileExists
'Instantiate the Microsoft XMLDOM
Set objDom = server.CreateObject("Microsoft.XMLDOM")
objDom.preserveWhiteSpace = True
blnFileExists = objDom.Load(strXMLFilePath & "\" & strFileName)
If blnFileExists = True Then
Set objGpx = objDom.documentElement
Else
'GPX root element and append it to the XML document.
Set objGpx = objDom.createElement("gpx")
objDom.appendChild objGpx
End If
Set objWpt = objDom.createElement("wpt")
'Create "Lat" attribute'
Set objattLat = objDom.createAttribute("lat")
objattLat.Text = (location_x)
objWpt.setAttributeNode objattLat
'Create "Lon" attribute'
Set objattLon = objDom.createAttribute("lon")
objattLon.Text = (location_y)
objWpt.setAttributeNode objattLon
'Create "date_and_time" element'
Set objDateTime = objDom.createElement("time")
objDateTime.Text = (date_and_time)
'Append "wpt" element as a child container element "gpx".'
objGpx.appendChild objWpt
'Append the "time" element as a child of the "wpt" element'
objWpt.appendChild objDateTime
'Append "rte" element only once'
If blnFileExists = True Then
Set objRte = objDom.documentElement
Else
Set objRte = objDom.createElement("rte")
objGpx.appendChild objRte
End If
Set objRtept = objDom.createElement("rtept")
'Create "Lat" attribute'
Set objattLat = objDom.createAttribute("lat")
objattLat.Text = (location_x)
objRtept.setAttributeNode objattLat
'Create "Lon" attribute'
Set objattLon = objDom.createAttribute("lon")
objattLon.Text = (location_y)
objRtept.setAttributeNode objattLon
'Create "date_and_time" element'
Set objDateTime = objDom.createElement("time")
objDateTime.Text = (date_and_time)
'Append "rtept" element as a child container element "rte".'
objRte.appendChild objRtept
'Append the "time" element as a child of the "rtept" element'
objRtept.appendChild objDateTime
If blnFileExists = False Then
'Create the xml processing instruction - and append to XML file
Set objPI = objDom.createProcessingInstruction("xml", "version='1.0'")
objDom.insertBefore objPI, objDom.childNodes(0)
End If
'Save the XML document.
objDom.save strXMLFilePath & "\" & strFileName
Set objDom = Nothing
Set objGpx = Nothing
Set objWpt = Nothing
Set objRte = Nothing
Set objRtept = Nothing
Set objDateTime = Nothing
Set objattLat = Nothing
Set objattLon = Nothing
Set objPI = Nothing
Set blnFileExists = Nothing
End Function
On Error Resume Next
'File path c:\
LoadObjecttoXML "c:\Inetpub\wwwroot\xml","doc.xml"
'error check
If err.number <> 0 then
Response.write("Error: " & err.number )
Else
Response.write("Success!")
End If
The problem I have is when I append the variables for a second time, the appended XML isn't placed inside the correct elements.
我遇到的问题是当我第二次附加变量时,附加的XML不会放在正确的元素中。
For example the XML code would look something like this on the first append:
例如,XML代码在第一个追加时看起来像这样:
<gpx>
<wpt lat="52.000" lon="-1.000">
<time>2016-09-23 23:38:00</time>
</wpt>
<rte>
<rtept lat="52.000" lon="-1.000">
<time>2016-09-23 23:38:00</time>
</rtept>
</rte>
</gpx>
When I append for the second time:
当我第二次追加时:
<gpx>
<wpt lat="52.000" lon="-1.000">
<time>2016-09-23 23:38:00</time>
</wpt>
<rte>
<rtept lat="52.000" lon="-1.000">
<time>2016-09-23 23:38:00</time>
</rtept>
</rte>
<wpt lat="52.100" lon="-1.100">
<time>2016-09-23 23:39:00</time>
</wpt>
<rte>
<rtept lat="52.100" lon="-1.100">
<time>2016-09-23 23:39:00</time>
</rtept>
</rte>
</gpx>
I would like the XML to append like this:
我希望XML附加如下:
<gpx>
<wpt lat="52.000" lon="-1.000">
<time>2016-09-23 23:38:00</time>
</wpt>
<wpt lat="52.100" lon="-1.100">
<time>2016-09-23 23:39:00</time>
</wpt>
<rte>
<rtept lat="52.000" lon="-1.000">
<time>2016-09-23 23:38:00</time>
</rtept>
<rtept lat="52.100" lon="-1.100">
<time>2016-09-23 23:39:00</time>
</rtept>
</rte>
</gpx>
I'd love to hear of any suggestions; hints or tips, on how I could achieve this.
我很想听听任何建议;关于如何实现这一目标的提示或提示。
1 个解决方案
#1
1
Checking blnFileExists
is useless and irrelevant with rte
. Remove the following part first.
检查blnFileExists是无用的,与rte无关。首先删除以下部分。
'Append "rte" element only once'
If blnFileExists = True Then
Set objRte = objDom.documentElement
Else
Set objRte = objDom.createElement("rte")
objGpx.appendChild objRte
End If
Instead you need to make sure that there's a rte
in the document.
相反,你需要确保文档中有一个rte。
To check whether a node exists you can use selectSingleNode. It simply returns the first matched object. If no nodes match the expression, returns Nothing
.
要检查节点是否存在,可以使用selectSingleNode。它只返回第一个匹配的对象。如果没有节点与表达式匹配,则返回Nothing。
'Append "rte" element only once'
Set objRte = objGpx.SelectSingleNode("rte")
If objRte Is Nothing Then ' rte does not exist
Set objRte = objDom.createElement("rte")
objGpx.appendChild objRte
End If
Update #1
ASP File:
ASP文件:
<%
' junk variables required in the function
Dim location_x, location_y, date_and_time
location_x = 56
location_y = 43
date_and_time = Now
'calling function twice
'will result an example.xml file near the asp file
LoadObjecttoXML Server.Mappath("."), "example.xml"
LoadObjecttoXML Server.Mappath("."), "example.xml"
Function LoadObjecttoXML(strXMLFilePath, strFileName)
Dim objDom
Dim objGpx
Dim objWpt
Dim objRte
Dim objRtept
Dim objDateTime
Dim objattLat
Dim objattLon
Dim objPI
Dim blnFileExists
'Instantiate the Microsoft XMLDOM
Set objDom = server.CreateObject("Microsoft.XMLDOM")
objDom.preserveWhiteSpace = True
blnFileExists = objDom.Load(strXMLFilePath & "\" & strFileName)
If blnFileExists = True Then
Set objGpx = objDom.documentElement
Else
'GPX root element and append it to the XML document.
Set objGpx = objDom.createElement("gpx")
objDom.appendChild objGpx
End If
Set objWpt = objDom.createElement("wpt")
'Create "Lat" attribute'
Set objattLat = objDom.createAttribute("lat")
objattLat.Text = (location_x)
objWpt.setAttributeNode objattLat
'Create "Lon" attribute'
Set objattLon = objDom.createAttribute("lon")
objattLon.Text = (location_y)
objWpt.setAttributeNode objattLon
'Create "date_and_time" element'
Set objDateTime = objDom.createElement("time")
objDateTime.Text = (date_and_time)
'Append "wpt" element as a child container element "gpx".'
objGpx.appendChild objWpt
'Append the "time" element as a child of the "wpt" element'
objWpt.appendChild objDateTime
'Append "rte" element only once'
Set objRte = objGpx.SelectSingleNode("rte")
If objRte Is Nothing Then ' rte does not exist
Set objRte = objDom.createElement("rte")
objGpx.appendChild objRte
End If
Set objRtept = objDom.createElement("rtept")
'Create "Lat" attribute'
Set objattLat = objDom.createAttribute("lat")
objattLat.Text = (location_x)
objRtept.setAttributeNode objattLat
'Create "Lon" attribute'
Set objattLon = objDom.createAttribute("lon")
objattLon.Text = (location_y)
objRtept.setAttributeNode objattLon
'Create "date_and_time" element'
Set objDateTime = objDom.createElement("time")
objDateTime.Text = (date_and_time)
'Append "rtept" element as a child container element "rte".'
objRte.appendChild objRtept
'Append the "time" element as a child of the "rtept" element'
objRtept.appendChild objDateTime
If blnFileExists = False Then
'Create the xml processing instruction - and append to XML file
Set objPI = objDom.createProcessingInstruction("xml", "version='1.0'")
objDom.insertBefore objPI, objDom.childNodes(0)
End If
'Save the XML document.
objDom.save strXMLFilePath & "\" & strFileName
Set objDom = Nothing
Set objGpx = Nothing
Set objWpt = Nothing
Set objRte = Nothing
Set objRtept = Nothing
Set objDateTime = Nothing
Set objattLat = Nothing
Set objattLon = Nothing
Set objPI = Nothing
Set blnFileExists = Nothing
End Function
%>
example.xml Output (prettified manually):
example.xml输出(手动美化):
<?xml version="1.0"?>
<gpx>
<wpt lat="56" lon="43">
<time>26.09.2016 16:08:49</time>
</wpt>
<rte>
<rtept lat="56" lon="43">
<time>26.09.2016 16:08:49</time>
</rtept>
<rtept lat="56" lon="43">
<time>26.09.2016 16:08:49</time>
</rtept>
</rte>
<wpt lat="56" lon="43">
<time>26.09.2016 16:08:49</time>
</wpt>
</gpx>
#1
1
Checking blnFileExists
is useless and irrelevant with rte
. Remove the following part first.
检查blnFileExists是无用的,与rte无关。首先删除以下部分。
'Append "rte" element only once'
If blnFileExists = True Then
Set objRte = objDom.documentElement
Else
Set objRte = objDom.createElement("rte")
objGpx.appendChild objRte
End If
Instead you need to make sure that there's a rte
in the document.
相反,你需要确保文档中有一个rte。
To check whether a node exists you can use selectSingleNode. It simply returns the first matched object. If no nodes match the expression, returns Nothing
.
要检查节点是否存在,可以使用selectSingleNode。它只返回第一个匹配的对象。如果没有节点与表达式匹配,则返回Nothing。
'Append "rte" element only once'
Set objRte = objGpx.SelectSingleNode("rte")
If objRte Is Nothing Then ' rte does not exist
Set objRte = objDom.createElement("rte")
objGpx.appendChild objRte
End If
Update #1
ASP File:
ASP文件:
<%
' junk variables required in the function
Dim location_x, location_y, date_and_time
location_x = 56
location_y = 43
date_and_time = Now
'calling function twice
'will result an example.xml file near the asp file
LoadObjecttoXML Server.Mappath("."), "example.xml"
LoadObjecttoXML Server.Mappath("."), "example.xml"
Function LoadObjecttoXML(strXMLFilePath, strFileName)
Dim objDom
Dim objGpx
Dim objWpt
Dim objRte
Dim objRtept
Dim objDateTime
Dim objattLat
Dim objattLon
Dim objPI
Dim blnFileExists
'Instantiate the Microsoft XMLDOM
Set objDom = server.CreateObject("Microsoft.XMLDOM")
objDom.preserveWhiteSpace = True
blnFileExists = objDom.Load(strXMLFilePath & "\" & strFileName)
If blnFileExists = True Then
Set objGpx = objDom.documentElement
Else
'GPX root element and append it to the XML document.
Set objGpx = objDom.createElement("gpx")
objDom.appendChild objGpx
End If
Set objWpt = objDom.createElement("wpt")
'Create "Lat" attribute'
Set objattLat = objDom.createAttribute("lat")
objattLat.Text = (location_x)
objWpt.setAttributeNode objattLat
'Create "Lon" attribute'
Set objattLon = objDom.createAttribute("lon")
objattLon.Text = (location_y)
objWpt.setAttributeNode objattLon
'Create "date_and_time" element'
Set objDateTime = objDom.createElement("time")
objDateTime.Text = (date_and_time)
'Append "wpt" element as a child container element "gpx".'
objGpx.appendChild objWpt
'Append the "time" element as a child of the "wpt" element'
objWpt.appendChild objDateTime
'Append "rte" element only once'
Set objRte = objGpx.SelectSingleNode("rte")
If objRte Is Nothing Then ' rte does not exist
Set objRte = objDom.createElement("rte")
objGpx.appendChild objRte
End If
Set objRtept = objDom.createElement("rtept")
'Create "Lat" attribute'
Set objattLat = objDom.createAttribute("lat")
objattLat.Text = (location_x)
objRtept.setAttributeNode objattLat
'Create "Lon" attribute'
Set objattLon = objDom.createAttribute("lon")
objattLon.Text = (location_y)
objRtept.setAttributeNode objattLon
'Create "date_and_time" element'
Set objDateTime = objDom.createElement("time")
objDateTime.Text = (date_and_time)
'Append "rtept" element as a child container element "rte".'
objRte.appendChild objRtept
'Append the "time" element as a child of the "rtept" element'
objRtept.appendChild objDateTime
If blnFileExists = False Then
'Create the xml processing instruction - and append to XML file
Set objPI = objDom.createProcessingInstruction("xml", "version='1.0'")
objDom.insertBefore objPI, objDom.childNodes(0)
End If
'Save the XML document.
objDom.save strXMLFilePath & "\" & strFileName
Set objDom = Nothing
Set objGpx = Nothing
Set objWpt = Nothing
Set objRte = Nothing
Set objRtept = Nothing
Set objDateTime = Nothing
Set objattLat = Nothing
Set objattLon = Nothing
Set objPI = Nothing
Set blnFileExists = Nothing
End Function
%>
example.xml Output (prettified manually):
example.xml输出(手动美化):
<?xml version="1.0"?>
<gpx>
<wpt lat="56" lon="43">
<time>26.09.2016 16:08:49</time>
</wpt>
<rte>
<rtept lat="56" lon="43">
<time>26.09.2016 16:08:49</time>
</rtept>
<rtept lat="56" lon="43">
<time>26.09.2016 16:08:49</time>
</rtept>
</rte>
<wpt lat="56" lon="43">
<time>26.09.2016 16:08:49</time>
</wpt>
</gpx>