2012-05-02 1 views
1

매일 써드 파티 서비스에서 2 개의 XML 파일을받으며이를 소비하는 서버에서 사용할 수있는 유일한 기술이므로 VBS를 사용하여 결합해야합니다. XML 내가 파일 (1)에서 (모든 모든 어린이 포함) 로트의 모든 노드를 가지고에 의해 소비되는 파일이 저장하기 전에 파일 2에 삽입 할 필요가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> 

- XML ​​파일의 각각 동일한 구조를 가지고 다른 서비스. VBS 사용.

웹을 검색 한 결과 VBS를 사용하여 노드를 가져 오는 방법을 찾았지만 노드를 다른 파일로 가져 오는 과정에서 길을 잃었습니다. 나는 그것을 올바르게 할 수 없었기 때문에 오늘날 3 ~ 4 개의 스크립트를 작성하고 폐기했습니다.

나는 논리는 것을 알고이 -

로드 파일 1, 로드 파일 2, 파일 1에서 많은 노드를 얻을, 파일 1에서 많은 노드를 통해 루프는의 자녀로를 추가 파일 2의 섹션 노드 세이브 파일 2

나를 올바른 방향으로 가리켜 줄 사람이 있습니까?

답변

1

두 가지 XML 파일을 모두 MSXML2.DomDocument 개체에로드하고 첫 번째 문서에서 lot 개의 노드를 모두 선택하여 복제 한 다음 두 번째 문서에 삽입하십시오. (병합-xml.vbs 불리는 경우)

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 

스크립트는 명령 줄에서 실행된다

에서 cscript 병합-xml.vbs의 xml1.xml의 xml2.xml

+0

오늘 아침에이 사진을 제공 할 것입니다. 게시 해 주셔서 감사합니다! –

+0

상황에 맞게 약간의 변경을했지만 완벽하게 작동합니다! 이것을 공유해 주셔서 감사합니다. 나는 복제 부분에 대해 생각하지 않았고 당신은 정말로 내 눈을 열었습니다. –

0

그냥 무제한 XML 파일을 읽는 방법에 대한 vbscript 예제를 제공하여 내용을 복제하고 새 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 
관련 문제