Java - XML Parsing -


this question has answer here:

i parsing xml in java check attribute. attribute searching erg. code not working.

import java.io.file; import javax.xml.parsers.documentbuilderfactory; import javax.xml.parsers.documentbuilder; import org.w3c.dom.document; import org.w3c.dom.nodelist; import org.w3c.dom.node; import org.w3c.dom.element;  public class parser {  public static void main(string[] args){    try {       file inputfile = new file("parser.txt");      documentbuilderfactory dbfactory          = documentbuilderfactory.newinstance();      documentbuilder dbuilder = dbfactory.newdocumentbuilder();      document doc = dbuilder.parse(inputfile);      doc.getdocumentelement().normalize();      system.out.println("root element :"          + doc.getdocumentelement().getnodename());      nodelist nlist = doc.getelementsbytagname("database");      (int temp = 0; temp < nlist.getlength(); temp++) {         node nnode = nlist.item(temp);         if (nnode.getnodetype() == node.element_node) {            element eelement = (element) nnode;            string nodeattribute = eelement.getattribute("key");            if(nodeattribute == "erg"){                system.out.println("yes");            }         }      }   } catch (exception e) {      e.printstacktrace();      }    } } 

here parser.txt

<?xml version="1.0"?> <databases> <database key='bc'    driver='com.micro'    url.local='456@we'    login='ses2adm'    password='12dr'     /> <database key='sgb'    driver='com.micro'    url.local='477@jj'    login='ses2adm'    password='12yh'   />   <database key='erg'    driver='com.micro13'    url.local='2726@kk'    login='sdm123'    password='ghjhj'   />   <database key='mesh'    driver='com.micro'    url.local='hgh'    login='adgvnh33'    password='hghsj'   />   <database key='mesj_eom'    driver='com.micro'    url.local='jujj'    login='124hh'    password='sunn'   />   <database key='bsuj'    driver='com.micro'    url.local='dkl12'    login='eh23'    password='fukll'   />   <database key='bsyh_eom'    driver='com.micro'    url.local='skk'    login='3556'    password='eyii'   /> </databases> 

you comparing references string.

compare content of node string using string.equals() method.

"erg".equals(nodeattribute) 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -