Sometimes our parameters are passed as XML types in the network, and sometimes the XML is spelled as a string.
Resolved as follows:
Single XML document parsing
XML file
<?xml version= "1.0" encoding= "gb2312"?>
<IfResponse>
<ifresult ></IfResult>
<ifwarning ></IfWarning>
<iferror >zh</IfError>
</IfResponse>
Java parsing
public static Boolean strparsexml (String str) {
documentbuilderfactory factory = Documentbuilderfactory.newinstance ();
documentbuilder builder;
String Ifresult=null;
String ifwarning=null;
try {
builder = factory.newdocumentbuilder ();
Document doc = builder.parse (new bytearrayinputstream (Str.getbytes ()));
if (doc.getelementsbytagname ("Ifresult"). Item (0). Getfirstchild ()!=null) {
ifresult=doc.getelementsbytagname ("Ifresult"). Item (0). Getfirstchild (). Getnodevalue ();
if (ifresult.equals ("0")) {
....
return true;
}else{
if (Doc.getelementsbytagname ("ifwarning"). Item (0). Getfirstchild ()!=null) {
Ifwarning=doc.getelementsbytagname ("ifwarning"). Item (0). Getfirstchild (). Getnodevalue ();
}
if (Doc.getelementsbytagname ("Iferror"). Item (0). Getfirstchild ()!=null) {
Iferror=doc.getelementsbytagname ("Iferror"). Item (0). Getfirstchild (). Getnodevalue ();
}
}
}
}
catch (Parserconfigurationexception e) {
E.printstacktrace ();
catch (Saxexception e) {
E.printstacktrace ();
catch (IOException e) {
E.printstacktrace ();
}
return false;
}
Multiple parameter parsing, in the form of objects in the list
public static list<exteriorimages> Xmlinfoparasemodel (String strinfo) {
List<exteriorimages> list=new arraylist<exteriorimages> ();
Documentbuilderfactory factory = Documentbuilderfactory.newinstance ();
Documentbuilder Builder;
try{
Builder = Factory.newdocumentbuilder ();
Document doc = builder.parse (new Bytearrayinputstream (Strinfo.getbytes ()));
int Nodelength=doc.getelementsbytagname ("message"). GetLength ()//Get all nodes
for (int i=0;i<nodelength;i++) {
Exteriorimages images=new exteriorimages ();
Images.setr_code (Doc.getelementsbytagname ("code"). Item (i). Getfirstchild (). Getnodevalue ());
Images.setbar_state (Doc.getelementsbytagname ("state"). Item (i). Getfirstchild (). Getnodevalue ());
Images.setapp_user (Doc.getelementsbytagname ("user"). Item (i). Getfirstchild (). Getnodevalue ());
List.add (images);
}
return list;
}
catch (Parserconfigurationexception e) {
E.printstacktrace ();
catch (Saxexception e) {
E.printstacktrace ();
catch (IOException e) {
E.printstacktrace ();
}
return null;
}