Reputation: 2291
I have xml on which I run queries as stated below. Which parser (sax or DOM or xpath) is best to retrieve query node by id?
Can you please explain me this with some sample code so that this will help me a lot?
I want to write a generic class which reads queries by id and passes parameters to it. What is the best way to return values after we executing queries since every query may return a different set of values?
<?xml version="1.0" encoding="UTF-8"?>
<queries>
<query id="getUserByName">
select * from users where name=?
</query>
<query id="getUserByEmail">
select * from users where email=?
</query>
</queries>
Upvotes: 0
Views: 6267
Reputation: 49095
If your really putting SQL into XML and then executing I would use MyBatis.
If you don't want to use MyBatis you should use JAXB and load the entire XML as a List of query objects because you don't want to reparse XML for every query. Let me show you how easy it is with JAXB:
@XmlRootElement(name = "queries")
public class Queries {
@XmlElement(name = "query")
public List<Query> queries;
}
public Query {
@XmlAttribute
public String id;
@XmlValue
public String sql;
}
Queries queries = JAXB.unmarshal(file, Queries.class);
The above is not tested but it should give you an idea.
Upvotes: 3
Reputation: 4746
Sax parser will be ideal for your case.I have written a blog post about Sax parsing
and another blog post on how to choose a xml parser
hope these will help you.
Upvotes: 0
Reputation: 163587
Better in order of usability and programmer productivity: SAX is worst, XPath is best.
Better in terms of saving machine cycles: SAX is best, XPath is worst.
So it depends whether your programmers cost more than your computers. Usually they do.
Upvotes: 3
Reputation: 9705
Technically, XPath is not a query, but a query language for selecting nodes from an XML document.
The main difference between SaX and DOM is that DOM will load all of your document into a memory structure. For small XML documents, you won't run into much trouble, but for a large XML document, memory usage may become an issue. SaX on the other hand will process your XML document in a streaming fashion, so in the end there is no object model representing the whole of your document. Instead you will need to keep track of the data you encountered in your document yourself.
For this case, my choice would be to use SaX and keep track of all queries in the document, for example in a Map<String, String>
(where the key is the query ID and the value is the query itself).
Upvotes: 2
Reputation: 10139
The DOM specification defines a tree-based approach to navigating an XML document The SAX specification defines an event-based approach whereby parsers scan through XML data, calling handler functions whenever certain parts of the document The strength of the SAX specification is that it can scan and parse gigabytes worth of XML documents without hitting resource limits
Upvotes: 1
Reputation: 8261
It mainly depends on use case.
If generally talking, SAX Parser
is good for parsing large file and dom parser
for alternates.
The reason is dom parser
ended up taking enough memory while parsing large file/input stream.
In my opinion, it needs to have some extent of experience to best utilize Sax Parser. Comparatively dom parsing is easier to learn and use.
Alternatively Apaches commons-digester can be alternative suggestion, if use case permits which is easier compare to sax and uses sax internally.
And xpath
is not a parser.
For your case, you do not need a parser. I think you are looking for xpath to retrieve sql queries using xpath
.
If you need to generate a new xml for your simple xml
you can do that manually. I can't suggest any better alternative right now.
Upvotes: 5