Java parsing string-type XML

Source: Internet
Author: User

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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.