-1

我每天从第三方服务收到 2 个 XML 文件,我需要使用 VBS 将它们组合起来,因为它是服务器上使用 XML 的唯一可用技术。每个 XML 文件都具有相同的结构 -

<section>
    <lot>
        <number>8</number>
        <identifier>au23-zx78a</identifier>
    </lot>
    <lot>
        <number>23</number>
        <identifier>au23-zx78a</identifier>
    </lot>
    ...and so on...
</section>

我需要从文件 1 中获取所有批次节点(包括所有子节点)并将它们插入到文件 2 中,然后再保存文件 2 以供其他服务使用。使用 VBS。

我在网上搜索过,我找到了使用 VBS 取出节点的方法,但我在将节点放入另一个文件时迷失了方向。我今天已经编写并废弃了 3 或 4 个脚本,因为我没能把它做好。

我知道逻辑是这样的——

加载文件 1,加载文件 2,从文件 1 中获取批次节点,遍历文件 1 中的批次节点,并将它们作为部分节点的子节点附加到文件 2 中,保存文件 2

谁能帮我指出正确的方向?

4

2 回答 2

1

这是一个简单的方法:将两个 xml 文件加载到MSXML2.DomDocument对象中,lot从第一个文档中选择所有节点,克隆它们并将它们插入到第二个文档中。

option explicit

' collect xml file paths from the command line
dim xmlpath1: xmlpath1 = WScript.Arguments(0)
dim xmlpath2: xmlpath2 = WScript.Arguments(1)

' open up the xml files, this might need some error handling
dim xmldoc1: set xmldoc1 = CreateObject("MSXML2.DomDocument")
xmldoc1.async = false
xmldoc1.load xmlpath1
xmldoc1.setProperty "SelectionLanguage", "XPath"
dim xmldoc2: set xmldoc2 = CreateObject("MSXML2.DomDocument")
xmldoc2.async = false
xmldoc2.load xmlpath2

' get a collection of lot nodes
dim lots: set lots = xmldoc1.selectNodes("/section/lot")
' loop through each lot, clone it and add it to the second document
dim lot
for each lot in lots
    dim l: set l = lot.cloneNode(true)
    xmldoc2.documentElement.appendChild l
next

' overwrite the second document
xmldoc2.save xmldoc2.url

该脚本(如果称为 merge-xml.vbs)将从命令行运行

cscript merge-xml.vbs xml1.xml xml2.xml

于 2012-05-03T11:45:21.770 回答
0

只是想提供 vbscript 示例,说明我如何读取无限的 XML 文件克隆内容并创建一个新的 XML 文件,然后将其读入记录集。即将多个 XML 文件读入记录集。

Dim xmlDoc,cn,fso,f,xmlDoc2,rs
    Set cn = CreateObject("ADODB.Connection")   
    Set rs = CreateObject("ADODB.Recordset")  
    Set xmlDoc = CreateObject("MSXML2.DOMDocument.3.0")
    Set xmlDoc2 = CreateObject("MSXML2.DOMDocument.3.0")
    Set fso = CreateObject("Scripting.FileSystemObject")
    xmlDoc.async="False"
    xmlDoc2.async="False"
    xmlDoc2.setProperty "SelectionLanguage","XPath"

    ' Create a root element in the new merged xml
    Set objRoot = xmlDoc.createElement("OutDelData")  
    xmlDoc.appendChild objRoot  

    ' Loop all xmls in a folder and extract all OutDelData/OutDeliveries 
     'change the xpath to //* to clone it all
     For Each f In 
     fso.GetFolder("C:\inetpub\wwwroot\HandheldWebService\data\OutDel\ToDeliver").Files
 If LCase(fso.GetExtensionName(f))="xml" Then
    xmlDoc2.load f.Path     
        For Each node In xmlDoc2.selectNodes("//OutDelData/OutDeliveries/*")
            Set newnode = node.cloneNode(True)
            xmlDoc.documentElement.appendChild newnode
        Next

End If
    Next

    ' Create new xml header
    Set objIntro = xmlDoc.createProcessingInstruction ("xml","version='1.0'")  
    xmlDoc.insertBefore objIntro,xmlDoc.childNodes(0) 

    ' save the nw merged file
    xmlDoc.save "C:\temp\XML\temp.xml"

    ' Open connection and make use of the data in a data set
    cn.Open "Provider=MSDAOSP;Data Source=MSXML2.DSOControl.3.0"
    rs.Open "C:\temp\XML\temp.xml",cn,3,3
    Debug.WriteLine rs.GetString
    rs.Close 
于 2017-09-26T09:24:54.897 回答