java - How can I parse String containing XML tags , so that I can get value of all the sub tags -


i have parse string containing xml tags 1 hard coded below can values of tags separately. here when using

nodelist node = doc.getelementsbytagname("event"); 

it returning value "ajain1ankitjain24-04-199223:09.08"

i want retrieve value each tag , store separately in different variables.

like eg in scenario want store value :

string uid = ajain1 string firstname = ankit string lastname = jain date date = "24-04-1992 23:09.08" 

here sample code working on.

package test;  import java.io.ioexception; import java.io.stringreader;  import javax.xml.parsers.documentbuilder; import javax.xml.parsers.documentbuilderfactory; import javax.xml.parsers.parserconfigurationexception;  import org.w3c.dom.document; import org.w3c.dom.element; import org.w3c.dom.nodelist; import org.xml.sax.inputsource; import org.xml.sax.saxexception;  public class demo {      public static void main(string[] args) {          string xmldata = "<event><class></class><data><uid><![cdata[ajain1]]></uid><firstname><![cdata[ankit]]></firstname><lastname><![cdata[jain]]></lastname><date><![cdata[24-04-1992]]></date><time><![cdata[23:09.08]]></time></data></event>";          documentbuilderfactory dbf = documentbuilderfactory.newinstance();         documentbuilder db = null;         try {             db = dbf.newdocumentbuilder();             inputsource = new inputsource();             is.setcharacterstream(new stringreader(xmldata));             try {                 document doc = db.parse(is);                 //string message = doc.getdocumentelement().gettextcontent();                 //system.out.println(message);                  nodelist node = doc.getelementsbytagname("event");              } catch (saxexception e) {                 // handle saxexception             } catch (ioexception e) {                 // handle ioexception             }         } catch (parserconfigurationexception e1) {             // handle parserconfigurationexception         }         // todo auto-generated method stub     } } 

thanks , let me know if require more information.

a nodelist list containing requested nodes, have admit, find implementation highly questionable. it's node containing requested nodes children. implementation has nothing in common other list implementations - doesn't implement list interface. don't know how handle [!cdata], loop through event tags you'd have this:

nodelist eventlist = doc.getelementsbytagname("event"); for(int = 0; < eventlist.getlength(); i++) {   element eventelement = (element) eventlist.item(i);   // stuff } 

from element, can use getelementsbytagname information needed first name , on. , yes, it's end many nested loops...


Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -