aspose file tools*
The moose likes XML and Related Technologies and the fly likes Splitting an XML file 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 "Splitting an XML file" Watch "Splitting an XML file" New topic
Author

Splitting an XML file

Prashant Mathur
Greenhorn

Joined: Jul 19, 2011
Posts: 22
I need to sort xml file using dom parser on some key attribute of it. How can I do that is there any built in function for that in java ??
Please tell asap. I have done exhaustive search on that but couldn't find a proper solution for that.

eg XML file



Suppose in the above xml file I need to sort it on the basis of "startTime" field, so how could I proceed..??
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
If this was my problem, I'd read the data into a suitable collection of objects that can be sorted via Collections.sort.

Or what, exactly, do you mean by "sort an XML file"?
Prashant Mathur
Greenhorn

Joined: Jul 19, 2011
Posts: 22
as I have shown the above a piece of code of my xml so as I am reading that xml on html table thru some java code....ok so the thing is that on runtime I am modifying the data means insert new event, update existing event or deleting event and accordingy date wise I am displaying it on browser now as logically events should be displayed according to time in a increasing order so I want to perform that .

So I thought to sort xml on every change in xml file. So that the displayed data remains sorted everytime..!!
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
Using a special Java DOM API it may be possible to reorder the nodes in memory without resorting to custom-made objects, but I kinda doubt that ordering according to one specific attribute is supported. But you can check those APIs out yourself: have a look at JDOM, dom4j and XOM in particular.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
Just keep the EventManagement Element references in a collection that can be sorted and write your Comparator to look at the time element contents.

Really very simple.

Bill
Prashant Mathur
Greenhorn

Joined: Jul 19, 2011
Posts: 22
If I add my Events in the vector means whole of

<EventManagement>
<userId></userId>
<eventId></eventId>
<startTime></startTime>
<endTime></endTime>
<details></details>
</EventManagement>




If this whole is added as it is in vector ...means this whole is one element of vector...how could I sort vector according to one attribute of this event. like <startTime>.......Collections n Comparator class can be used to sort vector in general...but here in this case if sorting a vector on basis on some tag then how ?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
You are writing your own Comparator, right?

That means you just need compare( obj1, obj2) and equals( obj1, obj2 ) methods, right?

It will be getting references to two org.w3c.dom.Elements, right?

These will be XML DOM Elements -soooooo you can use ANY XML DOM methods to inspect the contents of those elements to any desired depth to find the data to be used in the comparison, right?

Bill
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

How did "using DOM parser" get into your list of requirements? If this was my problem I would write a simple XSL transformation which used <xsl:sort> to do the required sorting.
Prashant Mathur
Greenhorn

Joined: Jul 19, 2011
Posts: 22
Friends its been quite a exhaustive search of mine over Internet. Since past so many days I am so tensed for this sorting problem. I have consulted you guys also about this all, but still not reached till the solution,
can any one of you exactly help me out that what those statements would be of Java that would suffice the solution of sorting a XML file on basis of some key attribute of it.

eg..
suppose the node is of the form

<EventManagement>
<userId></userId>
<eventId></eventId>
<startTime></startTime>
<endTime></endTime>
<details></details>
</EventManagement>

I have to sort it on the basis of <startTime> so please please please please friends tell me what those golden lines would be that I must include on my java code which would do the needful.
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
How did writing a Comparator not work? How did using <xsl:sort> not work? You did try those, right?
Oguzhan Acargil
Greenhorn

Joined: Apr 16, 2010
Posts: 6

Below xsl will sort your xml document by eventId; i agree with above who thinks xsl is better solution instead of DOM parsing.

Prashant Mathur
Greenhorn

Joined: Jul 19, 2011
Posts: 22
I am using Apache Velocity(Turbine Framwork)

Java n Mysql

velocity is for frontend designing where the data context.put() done from java is called on template and values are printed on a html table.

now as you are saying to use XSL ..........how I will place this file and use with these things...I am having confusion on this because xsl syntax doesn't works with vm files(velocity) ...n if I am using XSL which in itself contains html syntax how I will be able use my template file. ???

THIS IS A BIG PROBLEM for me ???
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
I'm confused. You said you wanted to transform XML. Now you want to transform VM files? Obviously you can't use XML approaches for non-XML files.

XML processing is not frontend stuff (the "V" in MVC), so you wouldn't be doing it in the VM templates, in case that's what you're asking. You'd do it in the Java controller code.
Prashant Mathur
Greenhorn

Joined: Jul 19, 2011
Posts: 22
No I think you aren't able to get what I am trying to say. Let me explain the whole thing again.

I am using velocity template language as fronted and java and mysql as backend coding...right !!

Now the thing is XML is kept on some location within the project folder..fine.

We have style sheets on one location all together for whole project and javascript(js) files on one location and so on.

Now my problem is that I HAVE TO SORT XML FILE ONLY as I said before but the thing is that the XSL you told would be saved with .xsl ...fine.....and now you tell that how n where should I keep that XSL file and use it ??
Should I call it in XML file ..If yes then how ??
Should I call it through java code..If yes then how ??
Should I call it through VM(thats a similar code like html with few differences in syntax and few more functionality)


I think now you must have got my problem that how n where should I use this xsl code for sorting of my xml file with this kind of PROJECT STRUCTURE.
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
You'd use an XSL API for XSL transformations; there's one built into the class libraries - check out the javax.xml.transform package. I suggest to get that working outside a web app first; it'll be easier to debug. If you haven't used XSL before there'll be a bit of a learning curve ahead of you. Once it's ready to be added to the web app, you'd do that in the Java controller code (maybe a servlet), not the VM templates.
 
 
subject: Splitting an XML file