aspose file tools*
The moose likes XML and Related Technologies and the fly likes Merging siblings with same tag name in xml documents Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Merging siblings with same tag name in xml documents" Watch "Merging siblings with same tag name in xml documents" New topic
Author

Merging siblings with same tag name in xml documents

Swethar Kalki
Greenhorn

Joined: Mar 26, 2012
Posts: 4
I'm trying to create concise summaries for xml documents. The idea is as described below. The input xml file i'm using is.

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

<Employee>
<Emp_Id> E-001 </Emp_Id>
<Emp_Name> Vinod </Emp_Name>
<Emp_E-mail> Vinod1@yahoo.com </Emp_E-mail>
</Employee>

<Employee>
<Emp_Id> E-002 </Emp_Id>
<Emp_Name> Amit </Emp_Name>
<Emp_E-mail> Amit2@yahoo.com </Emp_E-mail>
</Employee>

<Employee>
<Emp_Id> E-003 </Emp_Id>
<Emp_Name> Deepak </Emp_Name>
<Emp_E-mail> Deepak3@yahoo.com </Emp_E-mail>
</Employee>

</Employee-Detail>


I'm trying to write code in java that converts above file into something like the following.
<?xml version = "1.0" encoding="UTF-8"?>
<Employee-Detail>

<Employee>
<Emp_Id> E-001,E-002,E-003 </Emp_Id>
<Emp_Name> Vinod,Amit,Deepak </Emp_Name>
<Emp_E-mail> Vinod1@yahoo.com,Amit2@yahoo.com,Deepak3@yahoo.com </Emp_E-mail>
</Employee>

</Employee-Detail>


That is, I'm trying to merge siblings with same name by keeping only one such node and adding the children of the other siblings with same name to it.
I'm using the DOM API. How do I keep track of all the elements names and their respective children?
Jeremy James Brown
Greenhorn

Joined: Mar 24, 2011
Posts: 27
This would be easier with jaxb becasue you can control the schema. You could unmarshal your xml's to POJO's, then make a new POJO with a List in the Emp_E-mail filed and then marshal to the new schema you require.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18989
    
    8

Or it might be easier in XSLT, assuming you were dealing with a specific set of elements which you wanted to aggregate. But it sounds like a bad idea to me: you're going to end up with a badly-designed XML document. What do you plan to do with that document once you produce it?
Swethar Kalki
Greenhorn

Joined: Mar 26, 2012
Posts: 4
Jeremy James Brown wrote:This would be easier with jaxb becasue you can control the schema. You could unmarshal your xml's to POJO's, then make a new POJO with a List in the Emp_E-mail filed and then marshal to the new schema you require.

Hi Jeremy.Thanks for the reply. I will try with jaxb also.
Swethar Kalki
Greenhorn

Joined: Mar 26, 2012
Posts: 4
Paul Clapham wrote:Or it might be easier in XSLT, assuming you were dealing with a specific set of elements which you wanted to aggregate. But it sounds like a bad idea to me: you're going to end up with a badly-designed XML document. What do you plan to do with that document once you produce it?

Hi Paul. Thanks for the reply. Actually, the main idea is to generate structural summaries for XML documents. I'm doing this as the first step of that process.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18989
    
    8

My point was that having a text node containing a comma-separated list of data items is bad XML design. That's why I asked why you were doing that. Telling me that "it's the first step in a process" is not an answer to the question.

Hopefully you don't plan to try and un-separate the items in that list in a later step.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Merging siblings with same tag name in xml documents