Parsing xml in java -
this part of xml file
<iostatussummary> <iostatus_interval_value> <count>0</count> <iostatusvalue datainterval="2016-01-16 15:45:30">1.0</iostatusvalue> <iostatusvalue datainterval="2016-01-16 19:47:29">0.0</iostatusvalue> <iostatusvalue datainterval="2016-05-16 13:01:07">1.0</iostatusvalue> <iostatusvalue datainterval="2016-05-16 18:51:33">0.0</iostatusvalue> <iostatusvalue datainterval="2016-05-27 16:45:50">1.0</iostatusvalue> <iostatusvalue datainterval="2016-05-27 20:56:00">0.0</iostatusvalue> <iostatusvalue datainterval="2016-06-10 10:21:28">1.0</iostatusvalue> <iostatusvalue datainterval="2016-06-10 20:01:17">0.0</iostatusvalue> </iostatus_interval_value> </iostatussummary>
what trying
documentbuilder db1 = documentbuilder db1 = documentbuilderfactory.newinstance().newdocumentbuilder(); inputsource is1 = new inputsource(); is1.setcharacterstream(new stringreader(xmlresult)); document doc1 = db1.parse(is1); nodelist nlist_interval=doc1.getelementsbytagname("iostatus_interval_value"); list on = new arraylist(); list off = new arraylist(); (int =0; < nlist_interval.getlenght(); ++ ){ node currentitem2= nlist_interval.item(iter); element element2 = (element) currentitem2; string iostatusvalue = element2.getelementsbytagname("iostatusvalue").item(i).gettextcontent(); if(iostatusvalue.equals("1.0")) { on.add(element1.getattribute("datainterval"))); } if(iostatusvalue.equals("0.0")) { off.add("element1.getattribute("datainterval"))"); } }
then printing expecting
on = { 2016-01-16 15:45:30" , 2016-05-16 13:01:07 , 2016-05-27 16:45:50,2016-06-10 10:21:28}
off = { 2016-01-16 19:47:29, 2016-05-16 18:51:33, 2016-05-27 20:56:00, 2016-06-10 20:01:17 }
i getting list empty , above xml not complete xml part of xml.
but not getting expecting think making logical mistake somewhere because new in this.
thank reading question.
try this:
string xml = "<iostatussummary><iostatus_interval_value><count>0</count><iostatusvalue datainterval=\"2016-01-16 15:45:30\">1.0</iostatusvalue><iostatusvalue datainterval=\"2016-01-16 19:47:29\">0.0</iostatusvalue><iostatusvalue datainterval=\"2016-05-16 13:01:07\">1.0</iostatusvalue><iostatusvalue datainterval=\"2016-05-16 18:51:33\">0.0</iostatusvalue><iostatusvalue datainterval=\"2016-05-27 16:45:50\">1.0</iostatusvalue><iostatusvalue datainterval=\"2016-05-27 20:56:00\">0.0</iostatusvalue><iostatusvalue datainterval=\"2016-06-10 10:21:28\">1.0</iostatusvalue><iostatusvalue datainterval=\"2016-06-10 20:01:17\">0.0</iostatusvalue></iostatus_interval_value></iostatussummary>"; bytearrayinputstream bais = new bytearrayinputstream(xml.getbytes()); documentbuilder db1 = documentbuilderfactory.newinstance().newdocumentbuilder(); document doc1 = db1.parse(bais); nodelist nlist_interval = doc1.getelementsbytagname("iostatus_interval_value").item(0).getchildnodes(); list<string> on = new arraylist<string>(); list<string> off = new arraylist<string>(); (int = 0; < nlist_interval.getlength(); i++) { element element = (element) nlist_interval.item(i); if (element.gettagname().equals("iostatusvalue")) { string text = element.gettextcontent(); string interval = element.getattribute("datainterval"); if (text.equals("1.0")) { on.add(interval); } if (text.equals("0.0")) { off.add(interval); } } } system.out.println(on); system.out.println(off);
the main difference in example line:
nodelist nlist_interval = doc1.getelementsbytagname("iostatus_interval_value").item(0).getchildnodes();
what list of child elements of first iostatus_interval_value
element.
Comments
Post a Comment