I want to I want to merge 2 xml files.

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?
9900166556Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

9900166556Author Commented:
Hi,
The links you mentioned only do simple merging.
But I want the duplicate attributes to be removed.
0
objectsCommented:
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
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

9900166556Author Commented:
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
objectsCommented:
why can't write it for you sorry. have a crack at it and let us know if you get stuck
0
9900166556Author Commented:
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
objectsCommented:
>                     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
9900166556Author Commented:
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
objectsCommented:
yes I find DOM a lot easier to use.
Did you need any more help?
0
9900166556Author Commented:
Hi,
How to get all the attribute names in DOM?
I want to store ADMINISTRATIONROLEID,USERNAME ...to a list.

0
objectsCommented:
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
9900166556Author Commented:
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
objectsCommented:
yes xpath can do the search, then you can value from the returned element
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kevin CrossChief Technology OfficerCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.