您的位置:首页 > 编程语言 > Java开发

Java获取xml格式字段内容

2016-05-17 14:23 441 查看
   

有些接口传输的数据格式是xml的

例如:

<xml>
<InfoType>aaa</InfoType>
<Ticket>abc</Ticket>
</xml>


 public String processXml(HttpServletRequest request) throws Exception{  

           StringBuilder sb = new StringBuilder();  

    //从request里面取出传送过来的xml,并拼成String
           BufferedReader in = request.getReader();  
           String line;  
           while ((line = in.readLine()) != null) {  
               sb.append(line);  
           }  
           String xml = sb.toString();  

//转换String类型的xml转换为doc

       Document doc;  
       try {  
           doc = DocumentHelper.parseText(xml);  
           Element rootElt = doc.getRootElement(); 
           String infotype = rootElt.elementText("InfoType");//获取xml的<InfoType>标签的value
    String ticket = rootElt.elementText("Ticket");  
//   获取xml的<Ticket>标签的value         

 

    System.out.println("=============ticket=============="+ticket);
   return ticket;
       } catch (DocumentException e) {  
           e.printStackTrace();  
       } 
       return "could not get ticket";
   }  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: