Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Merge XML..?

Status
Not open for further replies.

kenjoswe

Technical User
Sep 19, 2000
327
SE
Hi all,

I have a bunch of XML-files that comes every day.
I want to merge all these files to a big single file so that I can load the file in one step.

Below is a simplified version of the real file. Only difference is that BusinessPartner is repeated x number of times.

<?xml version="1.0" encoding="iso-8859-1" ?>
<BusinessPartners_Update>
<Body>
<BusinessPartner>
<Customer>
<BusinessPartnerNumber>001</BusinessPartnerNumber>
<BusinessPartnerName>Name1</BusinessPartnerName>
</Customer>
<OrganisationNumber>5560123456</OrganisationNumber>
<AccountGroup>1000</AccountGroup>
<LegalStatus>1</LegalStatus>
<Address>
<Street>StreetName</Street>
<AddressPostalCode>123 45</AddressPostalCode>
<City>CityName</City>
<CountryKey>US</CountryKey>
</Address>
</BusinessPartner>
</Body>
</BusinessPartners_Update>

I guess I've to edit it so that I only have one set with:

<BusinessPartners_Update>
<Body>
</Body>
</BusinessPartners_Update>

Can this be automated?

/Kent J.

 
I'd say, use MSXML.DomDocument (if files are not enormous).
Its easy to program in VB, or any other language for that matter.
In VB you'd have something like:
Code:
    Dim objSource As MSXML2.DOMDocument
    Dim objTarget As MSXML2.DOMDocument
    Dim objNode As MSXML2.IXMLDOMElement
    
    Set objSource = New MSXML2.DOMDocument40
    Set objTarget = New MSXML2.DOMDocument40
    
    objTarget.loadXML ("<BusinessPartners_Update />")
'   start a loop through files here
    If objSource.Load("C:\path\sourcefile.xml") Then
        For Each objNode In objSource.documentElement.childNodes
            objTarget.documentElement.appendChild objNode
        Next
    Else
    '   implement some decent errorhandling here
        MsgBox objSource.parseError.reason
    End If
'   end loop through files here
    objTarget.save "C:\path\targetfile.xml"
 
If files are not enormously huge, just use MSXML.DomDocument
Easy to program, for example in VB:
Code:
    Dim objSource As MSXML2.DOMDocument
    Dim objTarget As MSXML2.DOMDocument
    Dim objNode As MSXML2.IXMLDOMElement
    
    Set objSource = New MSXML2.DOMDocument40
    Set objTarget = New MSXML2.DOMDocument40
    
    objTarget.loadXML ("<BusinessPartners_Update />")

'   start some kind of loop through sourcefiles here

    If objSource.Load("C:\path\sourcefile.xml") Then
        For Each objNode In objSource.documentElement.childNodes
            objTarget.documentElement.appendChild objNode
        Next
    Else
    '   implement some decent errorhandling
        MsgBox objSource.parseError.reason
    End If

'   end loop through sourcefiles

    objTarget.save "C:\path\targetfile.xml"
 
jel,
I have tried this in a SQL-server DTS package:

'**********************************************************************
' Visual Basic ActiveX Script
'************************************************************************

Function Main()

Set objSource = CreateObject("MSXML2.DOMDocument.4.0")
Set objTarget = CreateObject("MSXML2.DOMDocument.4.0")


objTarget.loadXML ("<BusinessPartners_Update />")
' start a loop through files here
If objSource.Load("C:\test\kopia\test.xml") Then
For Each objNode In objSource.documentElement.childNodes
objTarget.documentElement.appendChild objNode
Next
Else
' implement some decent errorhandling here
MsgBox objSource.parseError.reason
End If
' end loop through files here
objTarget.save "C:\test\resultat.xml"

End Function

===========================
...also I got: Invalid task result

I have only stated one file (C:\test\kopia\test.xml).
But how can I loop through the rest of the files in the directory?

/Kent J.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top