aspose file tools*
The moose likes XML and Related Technologies and the fly likes is there any other tech to replace XPATH+DOM? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "is there any other tech to replace XPATH+DOM?" Watch "is there any other tech to replace XPATH+DOM?" New topic
Author

is there any other tech to replace XPATH+DOM?

raminaa niilian
Ranch Hand

Joined: Jul 14, 2005
Posts: 551
Hi
Thank you for reading my post
For some times we used Xpath and dom parser to find node and attribute (values) in our XML documents, now we fin out that it become a hot spot in our application and this use of XPATH and DOM is using fair amount of cpu times.

is there any other tech to replace this two with them?
we just need to read and pars xml, find node and attributes (values) we do not create XML .

thanks
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42918
    
  68
Have you tried using a SAX parser instead? It may be faster, even though it may possibly involve more code to write, depending on how complex the logic to access particular nodes is.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
Although XPath is convenient, it is certainly not fast as I found out preparing this article.

Directly coding access using the DOM methods will be a LOT faster since you know exactly what you want.

If all you want is the attributes from a known Element and don't need details about the XML document hierarchy, SAX will be even faster as Ulf suggested.

Bill
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
There are situations where you look for a particular value (may be an attribute or text in a element).The XML is very big , in that case using SAX is better , but Stax is better suited for this situation.

Consider the following situation.Where are you looking for value of a particular element and that element may appear anywhere in the XML document.

The main disadvantage of using SAX in this case would be , even if you find the element in the beginning of the XML , then also the complete file would be parsed as the parser is in control and would only return when parsing is complete.

In such cases Stax would be more useful because as soon as you find the required node you can stop the parsing process.So it would not parse the entire document.


Rahul Bhattacharjee
LinkedIn - Blog
raminaa niilian
Ranch Hand

Joined: Jul 14, 2005
Posts: 551
Hi
Thank you for reading my post
as some examples of our usage i can mention:
-find all node in the documents that have attribute a='some value'
-find the attribute value which its name is.
-find all nodes, with the name of attribute.

we usually work with small sized xml file (smaller than 10K), does stax or perform better for such condition?

thanks
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Originally posted by raminaa niilian:

we usually work with small sized xml file (smaller than 10K), does stax or perform better for such condition?


DOM would be better suited for this situation.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
The main disadvantage of using SAX in this case would be , even if you find the element in the beginning of the XML , then also the complete file would be parsed as the parser is in control and would only return when parsing is complete.


Not true - all you have to do is throw an exception after the data has been found. Of course you will have to provide for catching the exception properly, but that is no big deal.

Bill
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Originally posted by William Brogden:
Not true

I thought its bad design to control the flow of the program using exceptions.
Well there is nothing wrong in using exception for this purpose.

Is there is disadvantage of using STAX for this issue ?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
Is there is disadvantage of using STAX for this issue ?


Actually StAX should be perfect for this application since it avoids the problem of the separate parsing Thread. However, as I understand it, StAX is not yet part of the standard Java library.

This site summarizes the current state of StAX for Java. There is a download for the current version.

Bill
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42918
    
  68
Originally posted by William Brogden:
However, as I understand it, StAX is not yet part of the standard Java library.


As of JAXP 1.4 -which is part of Java 6- it is.
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Thanks for your response William.

It has been included as part of JAXP 1.4 , which is included in JDK 1.6.
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
Originally posted by raminaa niilian:
Hi
Thank you for reading my post
as some examples of our usage i can mention:
-find all node in the documents that have attribute a='some value'
-find the attribute value which its name is.
-find all nodes, with the name of attribute.

we usually work with small sized xml file (smaller than 10K), does stax or perform better for such condition?

thanks


I guess these are perfect situations by using XSLT. Simple, easy, can be controlled/changed from external without code changes.

However, I don't know the performance comparing to other approaches.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: is there any other tech to replace XPATH+DOM?