I'm using classic ASP in my project. I want to merge two XMLs together. How do I do this? Below is my sample code:
我在我的项目中使用经典ASP。我想将两个XML合并在一起。我该怎么做呢?以下是我的示例代码:
XML 1
<CATALOG>
<CD>
<TITLE>1</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
<CD>
<TITLE>2</TITLE>
<ARTIST>Bonnie Tyler</ARTIST>
<COUNTRY>UK</COUNTRY>
<COMPANY>CBS Records</COMPANY>
<PRICE>9.90</PRICE>
<YEAR>1988</YEAR>
</CD>
<CD>
<TITLE>3</TITLE>
<ARTIST>Dolly Parton</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>RCA</COMPANY>
<PRICE>9.90</PRICE>
<YEAR>1982</YEAR>
</CD>
</CATALOG>
XML2
<CATALOG>
<CD>
<TITLE>4</TITLE>
<ARTIST>Gary Moore</ARTIST>
<COUNTRY>UK</COUNTRY>
<COMPANY>Virgin records</COMPANY>
<PRICE>10.20</PRICE>
<YEAR>1990</YEAR>
</CD>
<CD>
<TITLE>5</TITLE>
<ARTIST>Eros Ramazzotti</ARTIST>
<COUNTRY>EU</COUNTRY>
<COMPANY>BMG</COMPANY>
<PRICE>9.90</PRICE>
<YEAR>1997</YEAR>
</CD>
<CD>
<TITLE>6</TITLE>
<ARTIST>Bee Gees</ARTIST>
<COUNTRY>UK</COUNTRY>
<COMPANY>Polydor</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1998</YEAR>
</CD>
</CATALOG>
This is ASP code I currently use:
这是我目前使用的ASP代码:
Dim doc1 ''# As MSXML2.DOMDocument30
Dim doc2 ''# As MSXML2.DOMDocument30
Dim doc2Node ''# As MSXML2.IXMLDOMNode
Set doc1 = createobject("MSXML2.DOMDocument.3.0")
Set doc2 = createobject("MSXML2.DOMDocument.3.0")
doc1.Load "01.xml"
doc2.Load "02.xml"
For Each doc2Node In doc2.documentElement.childNodes
doc1.documentElement.appendChild doc2Node
Next
response.write doc1.xml
But now I'm getting an error:
但现在我收到一个错误:
Microsoft VBScript runtime error '800a01a8' Object required: 'documentElement'
3 个解决方案
#1
0
Expanding on Jørn Schou-Rode's answer:
扩展JørnSchou-Rode的答案:
<%
Dim doc1 'As MSXML2.DOMDocument30
Dim doc2 'As MSXML2.DOMDocument30
Dim doc2Node 'As MSXML2.IXMLDOMNode
Set doc1 = createobject("MSXML2.DOMDocument.3.0")
Set doc2 = createobject("MSXML2.DOMDocument.3.0")
doc1.Load "01.xml"
doc2.Load "02.xml"
Response.Write ( doc1.xml.Replace("</CATALOG>", doc2.xml.Replace( "<?xml version="1.0" encoding="ISO-8859-1" ?>","").Replace("<CATALOG>","") )
%>
This would replace the tag from doc1.xml with the doc2.xml without the first two lines, but again, would only work for this situation where you have these two xml files and they don't contain duplicate nodes.
这将使用doc2.xml替换doc1.xml中的标记而不使用前两行,但同样,仅适用于您拥有这两个xml文件并且不包含重复节点的情况。
You could read the files in using the FileSystemObject which would be faster, but the benefit of loading it in the DOM is that it would only load well formed xml.
您可以使用FileSystemObject读取文件,这会更快,但是在DOM中加载它的好处是它只能加载格式良好的xml。
#2
0
Your basic approach should work. I suspect one of your documents does not load correctly because it is not well-formed or has an encoding error. Please check what this modification gives you:
你的基本方法应该有效。我怀疑您的某个文档无法正确加载,因为它格式不正确或编码错误。请检查此修改为您提供的内容:
Set doc1 = LoadXml("01.xml", True)
Set doc2 = LoadXml("02.xml", True)
''# ... all the rest of your stuff ...
Function LoadXml(XmlPath, FailOnError)
Dim xml, e
Set xml = Server.CreateObject("MSXML2.DOMDocument.3.0")
xml.Load XmlPath
Set e = xml.parseError
If e.errorCode <> 0 Then
DebugPrint "XML parsing error " & e.errorCode
DebugPrint "in file " & e.url
DebugPrint "on line: " & e.line & ", pos: " & e.linePos
DebugPrint "reason: " & e.reason
DebugPrint "source: " & e.srcText
If FailOnError Then Response.End
End If
Set LoadXml = xml
End Function
Sub DebugPrint(s)
Response.Write Server.HTMLEncode(s) & "<br>"
End Sub
#3
0
Disclaimer: This answer contains a "ghetto" solution to the problem in question. While it should work just fine for this particular problem, it will not solve any XML related problem.
免责声明:这个答案包含一个解决问题的“贫民窟”解决方案。虽然它应该适用于这个特定问题,但它不会解决任何与XML相关的问题。
Considering that you need simply need to concatenate the nodes right below the document node in the two documents, it should be trivial to solve the question using simple string manipulation, without parsing the actual XML. Just to get the idea:
考虑到您只需要在两个文档中的文档节点正下方连接节点,使用简单的字符串操作解决问题应该是微不足道的,而无需解析实际的XML。只是为了得到这个想法:
- Read all lines except the last one from
01.xml
, and write them to your output stream. - Read all lines except the first two from
02.xml
, and write them to your output stream.
从01.xml读取除最后一行之外的所有行,并将它们写入输出流。
从02.xml读取除前两行之外的所有行,并将它们写入输出流。
My VBScript is pretty rusty, but I believe something like this should cut it:
我的VBScript非常生疏,但我相信这样的东西会削减它:
Const ForReading = 1
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set textStream = objFSO.OpenTextFile(Server.MapPath("01.xml"), ForReading)
lastLine = ""
Do While Not textStream.AtEndOfStream
Response.Write lastLine
lastLine = TextStream.readline
Loop
Set textStream = objFSO.OpenTextFile(Server.MapPath("02.xml"), ForReading)
counter = 0
Do While Not textStream.AtEndOfStream
counter = counter + 1
If counter > 2 Then
Response.Write TextStream.readline
End If
Loop
#1
0
Expanding on Jørn Schou-Rode's answer:
扩展JørnSchou-Rode的答案:
<%
Dim doc1 'As MSXML2.DOMDocument30
Dim doc2 'As MSXML2.DOMDocument30
Dim doc2Node 'As MSXML2.IXMLDOMNode
Set doc1 = createobject("MSXML2.DOMDocument.3.0")
Set doc2 = createobject("MSXML2.DOMDocument.3.0")
doc1.Load "01.xml"
doc2.Load "02.xml"
Response.Write ( doc1.xml.Replace("</CATALOG>", doc2.xml.Replace( "<?xml version="1.0" encoding="ISO-8859-1" ?>","").Replace("<CATALOG>","") )
%>
This would replace the tag from doc1.xml with the doc2.xml without the first two lines, but again, would only work for this situation where you have these two xml files and they don't contain duplicate nodes.
这将使用doc2.xml替换doc1.xml中的标记而不使用前两行,但同样,仅适用于您拥有这两个xml文件并且不包含重复节点的情况。
You could read the files in using the FileSystemObject which would be faster, but the benefit of loading it in the DOM is that it would only load well formed xml.
您可以使用FileSystemObject读取文件,这会更快,但是在DOM中加载它的好处是它只能加载格式良好的xml。
#2
0
Your basic approach should work. I suspect one of your documents does not load correctly because it is not well-formed or has an encoding error. Please check what this modification gives you:
你的基本方法应该有效。我怀疑您的某个文档无法正确加载,因为它格式不正确或编码错误。请检查此修改为您提供的内容:
Set doc1 = LoadXml("01.xml", True)
Set doc2 = LoadXml("02.xml", True)
''# ... all the rest of your stuff ...
Function LoadXml(XmlPath, FailOnError)
Dim xml, e
Set xml = Server.CreateObject("MSXML2.DOMDocument.3.0")
xml.Load XmlPath
Set e = xml.parseError
If e.errorCode <> 0 Then
DebugPrint "XML parsing error " & e.errorCode
DebugPrint "in file " & e.url
DebugPrint "on line: " & e.line & ", pos: " & e.linePos
DebugPrint "reason: " & e.reason
DebugPrint "source: " & e.srcText
If FailOnError Then Response.End
End If
Set LoadXml = xml
End Function
Sub DebugPrint(s)
Response.Write Server.HTMLEncode(s) & "<br>"
End Sub
#3
0
Disclaimer: This answer contains a "ghetto" solution to the problem in question. While it should work just fine for this particular problem, it will not solve any XML related problem.
免责声明:这个答案包含一个解决问题的“贫民窟”解决方案。虽然它应该适用于这个特定问题,但它不会解决任何与XML相关的问题。
Considering that you need simply need to concatenate the nodes right below the document node in the two documents, it should be trivial to solve the question using simple string manipulation, without parsing the actual XML. Just to get the idea:
考虑到您只需要在两个文档中的文档节点正下方连接节点,使用简单的字符串操作解决问题应该是微不足道的,而无需解析实际的XML。只是为了得到这个想法:
- Read all lines except the last one from
01.xml
, and write them to your output stream. - Read all lines except the first two from
02.xml
, and write them to your output stream.
从01.xml读取除最后一行之外的所有行,并将它们写入输出流。
从02.xml读取除前两行之外的所有行,并将它们写入输出流。
My VBScript is pretty rusty, but I believe something like this should cut it:
我的VBScript非常生疏,但我相信这样的东西会削减它:
Const ForReading = 1
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set textStream = objFSO.OpenTextFile(Server.MapPath("01.xml"), ForReading)
lastLine = ""
Do While Not textStream.AtEndOfStream
Response.Write lastLine
lastLine = TextStream.readline
Loop
Set textStream = objFSO.OpenTextFile(Server.MapPath("02.xml"), ForReading)
counter = 0
Do While Not textStream.AtEndOfStream
counter = counter + 1
If counter > 2 Then
Response.Write TextStream.readline
End If
Loop