how do I parse one node at a time from XML file through Java code? -


this xml file:

<applications> <apps> <appname>myapp</appname> <server>qwe</server> <port>1234</port> <uname>system</uname> <passwd>security</passwd> </apps>  <apps> <appname>tyas</appname> <server>qwewe</server> <port>1235</port> <uname>asd</uname> <passwd>wetry</passwd> </apps> </applications> 

this java code:

package trial; import java.io.file;  import javax.xml.parsers.documentbuilder; import javax.xml.parsers.documentbuilderfactory;  import org.w3c.dom.document; import org.w3c.dom.element; import org.w3c.dom.node; import org.w3c.dom.nodelist; public class parsing {  public static void main(string args[])  {     try {  file applications = new file("appdetails.xml"); documentbuilderfactory dbfactory = documentbuilderfactory.newinstance(); documentbuilder dbuilder = dbfactory.newdocumentbuilder(); document doc = dbuilder.parse(applications); doc.getdocumentelement().normalize();  system.out.println("application details:"); nodelist nodes = doc.getelementsbytagname("apps"); system.out.println("===================");  (int = 0; < nodes.getlength(); i++)  {     node node = nodes.item(i);  if (node.getnodetype() == node.element_node)  {     element element = (element) node;     system.out.println("app name: " + getvalue("appname", element));     string uname = getvalue("uname",element);     string host = getvalue("server",element);     string passwd = getvalue("passwd",element);     string clu = getvalue("clustername",element);     string path = getvalue("path",element);     string cmd = getvalue("cmd" ,element);     string port = getvalue("port" ,element);     string user= getvalue("user",element);     system.out.println(user);     string pswd= getvalue("pswd" ,element);     system.out.println(pswd);    }}} catch (exception ex) {ex.printstacktrace();     } } private static string getvalue(string tag, element element)  { nodelist nodes = element.getelementsbytagname(tag).item(0).getchildnodes(); node node = (node) nodes.item(0); return node.getnodevalue(); } } 

the output iterates through both nodes. how modify code display details of node select? if select myapp should display details of myapp.

change

if (node.getnodetype() == node.element_node)  

to

if (node.getnodetype() == node.element_node && "myapp".equals(getvalue("appname", element)))  

Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -