Author |
Message
|
BuddhuSardar |
Posted: Tue Apr 28, 2009 10:26 pm Post subject: Merge Two XML Files in one |
|
|
Newbie
Joined: 22 Jan 2009 Posts: 5
|
Hi Can anybody help me on this. I need to merge two XML files in one but I am not able to do so. Can you please help me to code this.
Here is the XML files:
File 1:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Data>
<Employee>
<Name>Surgeet</Name>
<Company>Accenture</Company>
<Project>Wallmart</Project>
<Employee Number>1234</Employee Number>
</Employee>
<Employee>
<Name>Sachin</Name>
<Company>TCS</Company>
<Project>GE</Project>
</Employee>
<Employee>
<Name>Amit</Name>
<Company>Infi</Company>
<Project>ABN</Project>
</Employee>
</Data>
File 2:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Data>
<Employee>
<Name>Surgeet</Name>
<Company>Accenture Services</Company>
<Project>Wallmart</Project></Employee>
<Employee>
<Name>Sachin Tendulkar</Name>
<Company>TCS</Company>
<Project>Emerald</Project>
</Employee>
<Employee>
<Name>Amit Kumar</Name>
<Company>Infi</Company>
<Project>ABN</Project>
</Employee>
<Employee>
<Name>Kapil</Name>
<Company>Infi</Company>
<Project>ABN</Project>
</Employee>
</Data>
<Data>
<Employee>
<Name>Surgeet</Name>
<Company>Accenture Services</Company>
<Project>Wallmart</Project></Employee>
<Employee Number>1234</Employee Number>
<Employee>
<Name>Sachin Tendulkar</Name>
<Company>TCS</Company>
<Project>Emerald</Project>
</Employee>
<Employee>
<Name>Amit Kumar</Name>
<Company>Infi</Company>
<Project>ABN</Project>
</Employee>
<Employee>
<Name>Kapil</Name>
<Company>Infi</Company>
<Project>ABN</Project>
</Employee>
</Data>[/code] |
|
Back to top |
|
 |
Vitor |
Posted: Tue Apr 28, 2009 11:34 pm Post subject: Re: Merge Two XML Files in one |
|
|
 Grand High Poobah
Joined: 11 Nov 2005 Posts: 26093 Location: Texas, USA
|
BuddhuSardar wrote: |
I need to merge two XML files in one but I am not able to do so. |
Why not? What method have your tried? What happens when you try? Does it fail, or give an unexpected result? What's unexpected about the result?
More information in will give better advice out. It will certainly prevent us suggesting things which have already proved unsuitible. On the face of it you have a standard piece of data manipulation so it's not clear what your problem is from your post. _________________ Honesty is the best policy.
Insanity is the best defence. |
|
Back to top |
|
 |
BuddhuSardar |
Posted: Tue Apr 28, 2009 11:54 pm Post subject: |
|
|
Newbie
Joined: 22 Jan 2009 Posts: 5
|
I am facing diffficulty in traversing the XML nodes. If it would have been simple as compying <Employee> from file 2 to file 1, it would have been easier. It is something like copying children of <Employee> from file 2 to file 1 so that if there are more child node of <Employee> in file1, they dont get deleted. I hope you understodd what I want to convey.
Does anyone has a dummy code for this? |
|
Back to top |
|
 |
Vitor |
Posted: Wed Apr 29, 2009 12:00 am Post subject: |
|
|
 Grand High Poobah
Joined: 11 Nov 2005 Posts: 26093 Location: Texas, USA
|
BuddhuSardar wrote: |
I hope you understodd what I want to convey.
|
So you're merging Employee (keyed I assume on Name) in file one with the same Employee in file 2, but if file 1 has (for instance) Employee Number but file 2 does not, the finished file must retain Employee Number?
Please correct me if I've misunderstood.
Please also give more details than "difficulty in traversing the XML nodes". What difficulty? Traversing the nodes is really the parser's job, and the structure of your document is not complex. The similarity between the 2 files should be helping.
What exactly is the problem? It sounds like a simple task on the face of it (which are often the most difficult of course!!) _________________ Honesty is the best policy.
Insanity is the best defence. |
|
Back to top |
|
 |
BuddhuSardar |
Posted: Wed Apr 29, 2009 12:11 am Post subject: |
|
|
Newbie
Joined: 22 Jan 2009 Posts: 5
|
The requirement is something like this:
We have 2 files with almost same data. File1 is the master data which may have some extra fields for <Employee> and which needs to go to downstream. File2 may have less data for the same<Employee> but it would have correct and certain data as it comes from audit applcation. We need to merge these 2 files so that any extra data in file1 is preserved and correct data from file2 is written to file1 for a particular filed for example <Name> from file2 should be written to file1.
The problem is I dont have idea as to how to achieve this in code. The main issue I have is how to preserve extra data in file1. |
|
Back to top |
|
 |
Vitor |
Posted: Wed Apr 29, 2009 12:18 am Post subject: |
|
|
 Grand High Poobah
Joined: 11 Nov 2005 Posts: 26093 Location: Texas, USA
|
BuddhuSardar wrote: |
The main issue I have is how to preserve extra data in file1. |
If you apply the data from file 2 into file 1, overwriting the data if it matches, then what deletes the tag if there's no matching tag in file 1 in file 2? What's the mechanism?
 _________________ Honesty is the best policy.
Insanity is the best defence. |
|
Back to top |
|
 |
BuddhuSardar |
Posted: Wed Apr 29, 2009 12:24 am Post subject: |
|
|
Newbie
Joined: 22 Jan 2009 Posts: 5
|
Here is the trouble:
The example I showed you has a limited number of tags. In real senerio, you dont know how many tags would be there and you also dont know the tag name. So I want to code it in a way that it dynamically replaces the tag in file1 for the matching tag in file2. |
|
Back to top |
|
 |
kimbert |
Posted: Wed Apr 29, 2009 12:53 am Post subject: |
|
|
 Jedi Council
Joined: 29 Jul 2003 Posts: 5542 Location: Southampton
|
In your first post you said
Quote: |
Hi Can anybody help me on this. I need to merge two XML files in one but I am not able to do so. Can you please help me to code this. |
Now you are saying
Quote: |
We have 2 files with almost same data. File1 is the master data which may have some extra fields for <Employee> and which needs to go to downstream. File2 may have less data for the same<Employee> but it would have correct and certain data as it comes from audit applcation. We need to merge these 2 files so that any extra data in file1 is preserved and correct data from file2 is written to file1 for a particular filed for example <Name> from file2 should be written to file1. |
Please explain how anybody could possibly have helped, using only the information in your first post!
This is not difficult. Just iterate over the records in the audit file. For each one, find the equivalent record in the master file. For each child tag in the audit file, read its name ( FIELDNAME ) ( and namespace if required ) and value ( FIELDVALUE ) and use them to SET the value in the master file. The SET statement allows the use of variables in the path references.
I recommend that you
a) use the XMLNSC domain
b) use references to navigate the source and target trees
c) use LASTMOVE to check whether your references moved successfully |
|
Back to top |
|
 |
WMBDEV1 |
Posted: Wed Apr 29, 2009 12:55 am Post subject: |
|
|
Sentinel
Joined: 05 Mar 2009 Posts: 888 Location: UK
|
For a large number of records I really think that you are going to struggle to make this perform as for each element in file 2, you will have to do a scan of file 1 to find the corresponding element (unless you know something about the data that I dont that allows you to do it faster).
This will give you a time complexity order of n squared and wont perform well as the data set grows large.
How many records are you likely to have in both files? Will this be large?
If you can index the join key from the first file somehow (maybe in a DB table; you should be able to improve on this) and the merging process should then be simple enough.
The fact that you dont know the tag names isnt a major issue, as you can use fieldname / nextsibling to discover them.
Just my thoughts. |
|
Back to top |
|
 |
|