[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

I want to I want to merge 2 xml files.

Posted on 2010-04-09
15
Medium Priority
?
283 Views
Last Modified: 2012-05-09
I want to merge 2 xml files.

If the nodes are common their content should get merged.

If the node is not common it should just added as is.

I am looking for a solution which does not use xslt but by parsing xml and merging manually.

Can you provide some code which does that?

I saw a previous thread on this but was not much useful?

Can you give some help on this?
0
Comment
Question by:9900166556
14 Comments
 

Author Comment

by:9900166556
ID: 30214571
Hi,
The links you mentioned only do simple merging.
But I want the duplicate attributes to be removed.
0
 
LVL 92

Expert Comment

by:objects
ID: 30252748
doubt you will find a ready made solution, you're going to have to implement it yourself ie. you need to handle the merging manually.
start by looping through the nodes, use importNode() to create a copy, then checking if its common and update appropriately
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 

Author Comment

by:9900166556
ID: 30297832
Thats fine...

<?xml version="1.0" encoding="UTF-8"?>
<table name="ADMINISTRATIONROLE">

  <column
    name="ADMINISTRATIONROLEID"
    type="id"
  />
  <column
    name="USERNAME"
    type="text"
  />
  <column
    name="STATUSCODE"
    type="text"
  />
  <column
    name="VERSIONNO"
    type="number"
  />
  <row>
    <attribute name="ADMINISTRATIONROLEID">
      <value>1001</value>
    </attribute>
    <attribute name="USERNAME">
      <value>superuser</value>
    </attribute>
    <attribute name="STATUSCODE">
      <value>AS1</value>
    </attribute>
    <attribute name="VERSIONNO">
      <value>1</value>
    </attribute>
  </row>
</table>


With


<?xml version="1.0" encoding="UTF-8"?>
<table name="ADMINISTRATIONROLE">

  <column
    name="ADMINISTRATIONROLEID"
    type="id"
  />
  <column
    name="USERNAME"
    type="text"
  />
  <column
    name="STATUSCODE"
    type="text"
  />
  <column
    name="VERSIONNO"
    type="number"
  />
  <row>
    <attribute name="ADMINISTRATIONROLEID">
      <value>1002</value>
    </attribute>
    <attribute name="USERNAME">
      <value>superuser</value>
    </attribute>
    <attribute name="STATUSCODE">
      <value>AS1</value>
    </attribute>
    <attribute name="VERSIONNO">
      <value>1</value>
    </attribute>
  </row>
</table>

And the O/P should look like:

<?xml version="1.0" encoding="UTF-8"?>
<table name="ADMINISTRATIONROLE">

  <column
    name="ADMINISTRATIONROLEID"
    type="id"
  />
  <column
    name="USERNAME"
    type="text"
  />
  <column
    name="STATUSCODE"
    type="text"
  />
  <column
    name="VERSIONNO"
    type="number"
  />
  <row>
    <attribute name="ADMINISTRATIONROLEID">
      <value>1001</value>
    </attribute>
    <attribute name="USERNAME">
      <value>superuser</value>
    </attribute>
    <attribute name="STATUSCODE">
      <value>AS1</value>
    </attribute>
    <attribute name="VERSIONNO">
      <value>1</value>
    </attribute>
  </row>

<row>
    <attribute name="ADMINISTRATIONROLEID">
      <value>1002</value>
    </attribute>
    <attribute name="USERNAME">
      <value>superuser</value>
    </attribute>
    <attribute name="STATUSCODE">
      <value>AS1</value>
    </attribute>
    <attribute name="VERSIONNO">
      <value>1</value>
    </attribute>
  </row>
</table>

Any suggestion?
0
 
LVL 92

Expert Comment

by:objects
ID: 30346750
why can't write it for you sorry. have a crack at it and let us know if you get stuck
0
 

Author Comment

by:9900166556
ID: 30362213
I have done like this

try{
                SAXBuilder builder = new SAXBuilder();
                Document books = builder.build("D:/in.xml");
                Document onebook = builder.build("D:/out.xml");
                Element root = books.getRootElement();
                List rows = root.getChildren();
               
                for (int i = 0; i < rows.size(); i++) {
                    Element row = (Element) rows.get(i);
                    onebook.getRootElement().addContent(row.detach());
                    System.out.println(row.getName());
                }
                new XMLOutputter(Format.getPrettyFormat()).output(onebook, System.out);
                  
            }catch(Exception e){
                  e.printStackTrace();
            }
      }
}

But its adding only 1st node...
Am i missing something?
0
 
LVL 92

Expert Comment

by:objects
ID: 30362623
>                     onebook.getRootElement().addContent(row.detach());

haven't used jdom for a while but I think thats not going to add all the nodes children
0
 

Author Comment

by:9900166556
ID: 30501664
I did the fix now. I used DOM which was easier solution.
I also able to remove the duplicate entry if the flag is set to.

public void getRow(String xmlOne,String xmlTwo,String xmlThree){

    try {

       File file = new File(xmlOne);
       File file2 = new File(xmlTwo);
       File file3 = new File(xmlThree);
          if (file.exists()){

              DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
              DocumentBuilder builder = fact.newDocumentBuilder();
              Document doc = builder.parse(xmlOne);
              Document doc2 = builder.parse(xmlTwo);
              doc.getDocumentElement().normalize();
              doc2.getDocumentElement().normalize();
              Node node = doc.getDocumentElement();
              Node node2 = doc2.getDocumentElement();
              //System.out.println("Root is " + node.getNodeName());
              NodeList childNodes = node.getChildNodes();
                //NodeList adc = childNodes.

              /* code for checking primary key for table */
              boolean isPrimaryKeyPresent = isPrimayKeyPresnt();
              List names = new ArrayList();
              for (int i = 0; i < node.getChildNodes().getLength(); i++) {
                if((node.getChildNodes().item(i).getNodeName() != "#text")){

                  if(node.getChildNodes().item(i).getNodeName().equals("row") ){
                       names.add(node.getChildNodes().item(i).getChildNodes().item(1).getChildNodes().item(1).getTextContent());
                    }
                 }
              }

              for (int i = 0; i < node2.getChildNodes().getLength(); i++) {
                if((node2.getChildNodes().item(i).getNodeName() != "#text")){
              //    System.out.println("@ 1");
                if(  node2.getChildNodes().item(i).getNodeName().equals("row") ){
                  boolean isPresent =false;
                  String name =node2.getChildNodes().item(i).getChildNodes().item(1).getChildNodes().item(1).getTextContent();
                  //System.out.println("%%%%"+node2.getChildNodes().item(i).getChildNodes().item(1).getChildNodes().item(1).getTextContent());

                  if(isPrimaryKeyPresent){
                    for(int j=0;j<names.size();j++){
                      if(name.equals(names.get(j))){
                      isPresent=true;
                      break;
                      }
                    }

                  }

                  if(!isPresent){
                  Node dup=doc.importNode(node2.getChildNodes().item(i), true);
                  doc.getDocumentElement().appendChild(dup);
                  }
                }
                }

              }



              org.apache.xml.serialize.OutputFormat format = new org.apache.xml.serialize.OutputFormat(doc);
               format.setIndenting(true);
               org.apache.xml.serialize.XMLSerializer output = new org.apache.xml.serialize.XMLSerializer(new FileOutputStream(xmlThree), format);
                output.serialize(doc);
          }
    }catch(Exception e){
      e.printStackTrace();
    }


  }
0
 
LVL 92

Expert Comment

by:objects
ID: 30566766
yes I find DOM a lot easier to use.
Did you need any more help?
0
 

Author Comment

by:9900166556
ID: 30598950
Hi,
How to get all the attribute names in DOM?
I want to store ADMINISTRATIONROLEID,USERNAME ...to a list.

0
 
LVL 92

Expert Comment

by:objects
ID: 30601715
you could use xpath to extract all the elements with the attribute name

http://www.exampledepot.com/egs/org.w3c.dom/xpath_GetElemByAttr.html
0
 

Author Comment

by:9900166556
ID: 30626254
I want to get the value of the "Attribute" in DOM

<row>
    <attribute name="ADMINISTRATIONROLEID">
      <value>1002</value>
    </attribute>
    <attribute name="USERNAME">
      <value>superuser</value>
    </attribute>
    <attribute name="STATUSCODE">
      <value>AS1</value>
    </attribute>
    <attribute name="VERSIONNO">
      <value>1</value>
    </attribute>
  </row>


Is there any way I can pass the attribute as "ADMINISTRATIONROLEID" and i will get back the value as "1002".......
0
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 30656844
yes xpath can do the search, then you can value from the returned element
0
 
LVL 61

Expert Comment

by:Kevin Cross
ID: 36427097
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month11 days, 11 hours left to enroll

640 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question