• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Merging siblings with same tag name in xml documents

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
You will always be treated with dignity. Now, strip naked, get on the probulator and hold this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic