Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
|
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 3rd, 2004, 01:42 PM
Authorized User
 
Join Date: Jul 2004
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default Merge two Similar XML files

Hello,

I have two similar xml files. I want to merge the contetnts of these two xml file into a third xml file which will be created on the fly. I do not want to use xslt. What is the best way to accomplish this.

Thanks,

 
Old August 3rd, 2004, 01:48 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,101
Thanks: 0
Thanked 2 Times in 2 Posts
Default

XSLT is the best way to accomplish it. Unless you just want to append one to the other- without regaurd to the contents.. then you can just open the files and merge them.

Hal Levy
Web Developer, PDI Inc.

NOT a Wiley/Wrox Employee
 
Old August 3rd, 2004, 02:51 PM
Authorized User
 
Join Date: Jul 2004
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

But the problem is the root element in both the xml files are same. When i merge two xml file and create a new xml file, I need only one root element not just merged content.

for example:

[u]1.xml</u>
<person>
    <name>
       <first>Bill</first>
       <last>Gates</last>
    </name>
</person>

[u]2.xml</u>
<person>
    <name>
       <first>Steve</first>
       <last>Ballmer</last>
    </name>
</person>

when I merge these two file, i need the following xml in the third xml file.

<person>
    <name>
       <first>Bill</first>
       <last>Gates</last>
    </name>
    <name>
       <first>Steve</first>
       <last>Ballmer</last>
    </name>
</person>

 
Old August 3rd, 2004, 04:15 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You asked for the best solution, and you said you didn't want to use XSLT. Well, the best solution is XSLT, whether you like it or not.

<xsl:template match="/">
<xsl:copy>
  <xsl:copy-of select="*/node()"/>
  <xsl:copy-of select="document('other.xml')/*/node()"/>
</xsl:copy>
</xsl:template>

Of course if you're dead set against XSLT and don't mind using a working-draft language you could also use XQuery:

element {node-name(doc('first.xml')/*)} {
  doc('first.xml')/*/node(), doc('second.xml')/*/node()
}

The only other alternatives are (a) to write a couple of hundred lines of Java or similar using the DOM (or JDOM, etc), or (b) to do it at the source XML level with some kind of Perl hack: not recommended.

Michael Kay

Michael Kay
http://saxon.sf.net/
 
Old August 4th, 2004, 08:19 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,101
Thanks: 0
Thanked 2 Times in 2 Posts
Default

AS Michael has pointed out- You should use XSLT.

Hal Levy
Web Developer, PDI Inc.

NOT a Wiley/Wrox Employee
 
Old August 4th, 2004, 11:12 PM
Registered User
 
Join Date: Jan 2004
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

you can do the same with Visual basic also.
Reference to MSXML2 create a one dom object
ObjDom.Load("One.xml")
set objNodeList1 = objDom.SlectNodeList("root/person").clone;
ObjDom.Load("Two.xml")
set objNodeList2 = objDom.SlectNodeList("root/person").clone;
now append the both nodelists to the new dom object
and take the xml out.
you can make this as a dll and register in your appserver which accepts the node to be copyed and path and xmlstrings and return xml from the function so you need not have to write the code again and again.
Regards



Vinay Bhushan.S
Zenith Software Ltd.

http://www28.brinkster.com/developerhome/





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to merge two xml documents together? NewToXSL Pro VB.NET 2002/2003 2 August 22nd, 2008 05:28 AM
merge two xml file and make new xml using xslt ketan XSLT 0 September 21st, 2004 08:48 AM
Merge xml schema files mayada XSLT 0 April 16th, 2004 10:37 AM
XML Merge... babloo81 XML 3 November 14th, 2003 06:44 AM
Merge XML files into a xml file using xslt lxu XML 4 November 6th, 2003 06:01 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.