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>
It mainly depends on use case.
If generally talking,
SAX Parseris good for parsing large file anddom parserfor alternates.The reason is
dom parserended 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
xpathis 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
xmlyou can do that manually. I can’t suggest any better alternative right now.