Java: How i pass the string in xml and get diffrent attribute -
i have xml-string and, want result
-elements messageid
-,designation
- , status
-members it. how can that?
"null <?xml version="1.0" encoding="utf-8"?> <results> <result> <status>-13</status> <messageid></messageid> <destination>null</destination> </result> <result> <status>-3</status> <messageid></messageid> <destination>911234567898</destination> </result> <result> <status>0</status> <messageid>146092209473920945</messageid> <destination>917827767338</destination> </result> <result> <status>0</status> <messageid>116092209473924510</messageid> <destination>918527593928</destination> </result> <result> <status>-3</status> <messageid></messageid> <destination>912345234567</destination> </result> </results>"
please me solve problem.
first of need rid of null
part @ start of string
. inputstream
can created rest of string
(see how turn string inputstreamreader in java?). parse stream dom parser , extract messageid
s xpath:
string s = "null<?xml version=\"1.0\" encoding=\"utf-8\"?><results><result><status>-13</status><messageid></messageid><destination>null</destination></result><result><status>-3</status><messageid></messageid><destination>911234567898</destination></result><result><status>0</status><messageid>146092209473920945</messageid><destination>917827767338</destination></result><result><status>0</status><messageid>116092209473924510</messageid><destination>918527593928</destination></result><result><status>-3</status><messageid></messageid><destination>912345234567</destination></result></results>"; inputstream = new bytearrayinputstream(s.substring(s.indexof("<?xml")).getbytes(standardcharsets.utf_8)); documentbuilder builder = documentbuilderfactory.newinstance().newdocumentbuilder(); document document = builder.parse(is); xpath path = xpathfactory.newinstance().newxpath(); string expression = "//messageid"; nodelist list = (nodelist) path.evaluate(expression, document, xpathconstants.nodeset); list<string> messageids = new arraylist<>(list.getlength()); (int = 0, l = list.getlength(); < l; i++) { messageids.add(list.item(i).gettextcontent()); } system.out.println(messageids);
Comments
Post a Comment