JAVA 解析XML文件

   String head="<?xml version=\"1.0\" encoding=\"GB2312\"?> \n;

   String content="<alipay>

                        <success>T</success>

                        <response>

                        <order>

                        <batch_status>P</batch_status> <btn_num>ff</btn_num>

                        </order> 

                        </response>  

                        </alipay> ";

try{

        XML xml=XML.readFrom(head+content);

        Element e1=xml.getRoot();//获取的是alipay

        Element e2=e1.getChild("response");

        Element e3=e2.getChild("order");

        if (e3.getChild("batch_status") !=null && !"".equals(e3.getChild(batch_status)){

             Element batch_status=e3.getChild("batch_status");

                         String str=batch_status.getText();

        }

     //每一个Element 需要用if判断  否则会报空指针异常

 }catch(Exception e){


}finally{


}

猜你喜欢

转载自blog.csdn.net/jessecary/article/details/78800772