How to get text of an element in xml with two attributes in java -



How to get text of an element in xml with two attributes in java -

i text within element has 2 attributes, sample xml below

<?xml version="1.0" encoding="utf-8"?> <queries> <query pagename="master" param="default"> select * test; </query> <query pagename="uftl" param="default"> select uftl, lop dwells lop='a' </query> </queries>

input: 2 attributes, output: query. i.e, on giving input 'master','default' query element, in case 'select * test;"

oh. write dom parser while waiting answer

private string parse(document document) { element root = document.getdocumentelement(); nodelist queries = root.getelementsbytagname("queries"); int querieslength = queries.getlength(); (int = 0; < querieslength; i++) { element currentquery = (element) queries.item(i); if (currentquery.getnodetype() == element.element_node) { string pagename = currentquery.getattributes() .getnameditem("pagename").gettextcontent(); string param = currentcategory.getattributes() .getnameditem("param").gettextcontent(); if(param.equals(paramvalue) && pagename.equals(pagename)){ string query = currnetnode.item(0).gettextcontent(); homecoming query; } homecoming null; } } }

sax parser:

public class parser implements contenthandler { boolean check = false; arraylist<string> queries = new arraylist<>(); @override public void startelement(string uri, string localname, string qname, attributes atts) throws saxexception { switch (localname) { case "query": string param = atts.getvalue("param"); string pagename = atts.getvalue("pagename"); check = true; break; default: return; } } @override public void endelement(string uri, string localname, string qname) throws saxexception { check = false; } @override public void characters(char[] ch, int start, int length) throws saxexception { string tagcontent = new string(ch, start, length).trim(); if(check){ if(!tagcontent.isempty()){ queries.add(tagcontent); } } }

i delete sum overriden method because empty , unneccesary here. must implement them , leave empty

update:

class main:

public class main { public static void main(string[] args) throws ioexception, saxexception { arraylist<string> queries = new parser().getqueries("test.xml"); (string query : queries){ system.out.println(query); } } }

parser class:

public class parser implements contenthandler { boolean check = false; arraylist<string> queries = new arraylist<>(); public arraylist<string> getqueries(string filename) throws saxexception, ioexception { xmlreader xmlreader = xmlreaderfactory.createxmlreader(); xmlreader.setcontenthandler(this); xmlreader.parse(filename); homecoming queries; } @override public void setdocumentlocator(locator locator) { //to alter body of implemented methods utilize file | settings | file templates. } @override public void startdocument() throws saxexception { //to alter body of implemented methods utilize file | settings | file templates. } @override public void enddocument() throws saxexception { //to alter body of implemented methods utilize file | settings | file templates. } @override public void startprefixmapping(string prefix, string uri) throws saxexception { //to alter body of implemented methods utilize file | settings | file templates. } @override public void endprefixmapping(string prefix) throws saxexception { //to alter body of implemented methods utilize file | settings | file templates. } @override public void startelement(string uri, string localname, string qname, attributes atts) throws saxexception { switch (localname) { case "query": string param = atts.getvalue("param"); string pagename = atts.getvalue("pagename"); if(!param.isempty() && !pagename.isempty()) check = true; break; default: return; } } @override public void endelement(string uri, string localname, string qname) throws saxexception { check = false; } @override public void characters(char[] ch, int start, int length) throws saxexception { string tagcontent = new string(ch, start, length).trim(); if(check){ if(!tagcontent.isempty()){ queries.add(tagcontent); } } } @override public void ignorablewhitespace(char[] ch, int start, int length) throws saxexception { //to alter body of implemented methods utilize file | settings | file templates. } @override public void processinginstruction(string target, string data) throws saxexception { //to alter body of implemented methods utilize file | settings | file templates. } @override public void skippedentity(string name) throws saxexception { //to alter body of implemented methods utilize file | settings | file templates. } }

also add together xml file in root of project te name test.xml

my output this:

select * test; select uftl, lop dwells lop='a'

java xml

Comments

Popular posts from this blog

javascript - mongodb won't find my schema method in nested container -

Hibernate criteria by a list of natural ids -

ios - Lagging ScrollView with UIWebview inside -