0

I have an XML file and I would like to merge two different CONTACT child nodes.

I have checked these websites it shows how to merge two different xml files into a single file.

http://www2.informatik.hu-berlin.de/~obecker/XSLT/#merge

Merge XML documents

In my case this is my first contact in the xml file:

<CONTACT>
<PDE-Identity>N65539</PDE-Identity>
<FirstName>Arun_niit</FirstName>
<LastName>Arun_niit</LastName>
<Facebook-ID/>
<EMAILS>
<EMail>
<Type>yahoo</Type>
<Value>nura_ice@yahoo.co.in</Value>
</EMail>
</EMAILS>
</CONTACT>

This is the second contact in the file:

<CONTACT>
<PDE-Identity>N65567</PDE-Identity>
<FirstName>Arun_niit</FirstName>
<LastName>Ramanathan</LastName>
<Facebook-ID/>
<EMAILS>
<EMail>
<Type>gmail</Type>
<Value>arun_niit@gmail.com</Value>
</EMail>
<EMail>
<Type>yahoo</Type>
<Value>nura_ice@yahoo.co.in</Value>
</EMail>
</EMAILS>
</CONTACT>

I know both of the contacts are belongs to the same person. How can i merge these two contacts in the same xml file.

Original XML File:

<?xml version="1.0" encoding="UTF-8"?>
<CONTACTS>

<CONTACT>
<PDE-Identity>N65539</PDE-Identity>
<FirstName>Arun_niit</FirstName>
<LastName>Arun_niit</LastName>
<Facebook-ID/>
<EMAILS>
<EMail>
<Type>yahoo</Type>
<Value>nura_ice@yahoo.co.in</Value>
</EMail>
</EMAILS>
</CONTACT>

<CONTACT>   
<PDE-Identity>N65567</PDE-Identity>
<FirstName>Arun_niit</FirstName>
<LastName>Ramanathan</LastName>
<Facebook-ID/>
<EMAILS>
<EMail>
<Type>gmail</Type>
<Value>arun_niit@gmail.com</Value>
</EMail>
<EMail>
<Type>yahoo</Type>
<Value>nura_ice@yahoo.co.in</Value>
</EMail>
</EMAILS>
</CONTACT>


<CONTACT>
<PDE-Identity>N65567</PDE-Identity>
<FirstName>Rangarajkarthik</FirstName>
<LastName>karthik Rangaraj</LastName>
<Facebook-ID/>
<EMAILS>
<EMail>
<Type>gmail</Type>
<Value>kart2006@gmail.com</Value>
</EMail>
<EMail>
<Type>yahoo</Type>
<Value>karthikrangaraj@yahoo.com</Value>
</EMail>
</EMAILS>
</CONTACT>

<CONTACTS>
Community
  • 1
  • 1
user838691
  • 320
  • 1
  • 5
  • 13
  • Welcome to SO. In order to answer your question we need to know more about the environment you're working in. Are you planning to use XSLT? A text editor? When you say "merge", what do you intend to do with conflicting data items, such as Last Name? It's not possible to write a general solution that will just "know" what to do with conflicts. – Jim Garrison Jun 29 '11 at 14:50
  • Hi Jim Garrison, thank you. I have no idea how to proceed with this...but i'm working on a Firefox extension using XUL. I would like to know how can i display this information to the user in XUL format or html format and let me user to merge two different contacts in the same XML file. For example, consider that we have check-box two select two different contacts and we could have a button to merge. Is it the right way of doing it? Please reply to me if i'm confusing. Thank you. – user838691 Jun 29 '11 at 14:59
  • What you are trying to do is a relatively complex task that will require a user interface so the user can tell your extension how to handle conflicts. Without such an interface, the extension will not be useful. Most (i.e. 90%) of your work will go into designing and implementing the interface, and merging the contacts, after the user tells you what to do in each conflict, will be the easy part. – Jim Garrison Jun 29 '11 at 15:03
  • I assume you'll be writing the code in Javascript (I've never done any FF extension work). In that case you will need to research Javascript libraries for parsing XML into a DOM representation. – Jim Garrison Jun 29 '11 at 15:03
  • Well, i couldn't find any proper tutorial with example to parse the xml file in FF using java-script. I don't have much experience on progtamming but still i'm trying to do. I'm trying to parse the xml file using java-script but i couldn't do it. Please check with this link i have posted another question about this problem: http://stackoverflow.com/questions/6518659/need-help-to-have-a-dynamic-menulist-from-xml-file-in-xul-using-xmlhttprequest – user838691 Jun 29 '11 at 15:09
  • I posted an answer that should get you started. – Jim Garrison Jun 29 '11 at 15:31

1 Answers1

0

Most browsers support XML parsing in javascript already. Here's a sample snippet, which you'll have to adapt to parse your specific input.

if (window.DOMParser)
  {
  parser=new DOMParser();
  xmlDoc=parser.parseFromString(text,"text/xml");
  }
else // Internet Explorer
  {
  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  xmlDoc.async="false";
  xmlDoc.loadXML(text);
  } 

For more information, I suggest you do a Google search for javascript xml dom.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190