Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XML
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 3rd, 2004, 01:42 PM
Authorized User
 
Join Date: Jul 2004
Location: , , .
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,

Reply With Quote
  #2 (permalink)  
Old August 3rd, 2004, 01:48 PM
Friend of Wrox
Points: 3,489, Level: 24
Points: 3,489, Level: 24 Points: 3,489, Level: 24 Points: 3,489, Level: 24
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Central, NJ, USA.
Posts: 1,102
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
Reply With Quote
  #3 (permalink)  
Old August 3rd, 2004, 02:51 PM
Authorized User
 
Join Date: Jul 2004
Location: , , .
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>

Reply With Quote
  #4 (permalink)  
Old August 3rd, 2004, 04:15 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
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/
Reply With Quote
  #5 (permalink)  
Old August 4th, 2004, 08:19 AM
Friend of Wrox
Points: 3,489, Level: 24
Points: 3,489, Level: 24 Points: 3,489, Level: 24 Points: 3,489, Level: 24
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Central, NJ, USA.
Posts: 1,102
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
Reply With Quote
  #6 (permalink)  
Old August 4th, 2004, 11:12 PM
Registered User
 
Join Date: Jan 2004
Location: Bangalore, Karnataka, India.
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/
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 05:29 AM.


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