码迷,mamicode.com
首页 > 其他好文 > 详细

xml字符串解析

时间:2014-05-28 15:22:53      阅读:264      评论:0      收藏:0      [点我收藏+]

标签:style   c   class   blog   code   java   

bubuko.com,布布扣
//解析xml字符串
    public void readStringXml(String xml)
{
Document doc=null;
try {
            //将字符转化为XML
            doc=DocumentHelper.parseText(xml);
            //获取根节点
            Element rootElt=doc.getRootElement();
            
            //拿到根节点的名称
            //System.out.println("根节点名称:"+rootElt.getName());
//获取根节点下的子节点的值
            String returnstatus=rootElt.elementText("returnstatus").trim();
            String message=rootElt.elementText("message").trim();
            String payinfo=rootElt.elementText("payinfo").trim();
            String overage=rootElt.elementText("overage").trim();
            String sendTotal=rootElt.elementText("sendTotal").trim();
System.out.println("返回状态为:"+returnstatus);
            System.out.println("返回信息提示:"+message);
            System.out.println("返回支付方式:"+payinfo);
            System.out.println("返回剩余短信条数:"+overage);
            System.out.println("返回总条数:"+sendTotal);
            
        } catch (DocumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
bubuko.com,布布扣

实体类封装解析

bubuko.com,布布扣
//XML字符串解析通用方法
    public xmlEntity readStringXmlCommen(xmlEntity xmlentity,String xml)
    {
        xmlEntity xe=new xmlEntity();
        
        Document doc=null;
        
        try {
            //将字符转化为XML
            doc=DocumentHelper.parseText(xml);
            //获取根节点
            Element rootElt=doc.getRootElement();
            //拿到根节点的名称
            //System.out.println("根节点:" + rootElt.getName()); 
            
            //获取根节点下的子节点的值
            if(xmlentity.getReturnstatus()!=null)
            {
                xe.setReturnstatus(rootElt.elementText(xmlentity.getReturnstatus()).trim());
            }
            if(xmlentity.getMessage()!=null)
            {
                xe.setMessage(rootElt.elementText(xmlentity.getMessage()).trim());
            }
            if(xmlentity.getRemainpoint()!=null)
            {
                xe.setRemainpoint(rootElt.elementText(xmlentity.getRemainpoint()).trim());
            }
            if(xmlentity.getTaskID()!=null)
            {
                xe.setTaskID(rootElt.elementText(xmlentity.getTaskID()).trim());
            }
            if(xmlentity.getSuccessCounts()!=null)
            {
                xe.setSuccessCounts(rootElt.elementText(xmlentity.getSuccessCounts()).trim());
            }
            if(xmlentity.getPayinfo()!=null)
            {
                xe.setPayinfo(rootElt.elementText(xmlentity.getPayinfo()).trim());
            }
            if(xmlentity.getOverage()!=null)
            {
                xe.setOverage(rootElt.elementText(xmlentity.getOverage()).trim());
            }
            if(xmlentity.getSendTotal()!=null)
            {
                xe.setSendTotal(rootElt.elementText(xmlentity.getSendTotal()).trim());
            }
            //接收状态返回的报告
            if(rootElt.hasMixedContent()==false)
            {
                System.out.println("无返回状态!");
            }
            else
            {
                for (int i = 1; i <= rootElt.elements().size(); i++) {
                    if(xmlentity.getStatusbox()!=null)
                    {
                        System.out.println("状态"+i+":");
                        //获取根节点下的子节点statusbox
                         Iterator iter = rootElt.elementIterator(xmlentity.getStatusbox()); 
                        // 遍历statusbox节点 
                         while(iter.hasNext())
                         {
                             Element recordEle = (Element) iter.next();
                             xe.setMobile(recordEle.elementText("mobile").trim());
                             xe.setTaskid(recordEle.elementText("taskid").trim());
                             xe.setStatus(recordEle.elementText("status").trim());
                             xe.setReceivetime(recordEle.elementText("receivetime").trim());
                             System.out.println("对应手机号:"+xe.getMobile());
                             System.out.println("同一批任务ID:"+xe.getTaskid());
                             System.out.println("状态报告----10:发送成功,20:发送失败:"+xe.getStatus());
                             System.out.println("接收时间:"+xe.getReceivetime());
                         }     
                     }
                    
                }

            }
bubuko.com,布布扣

 

xml字符串解析,布布扣,bubuko.com

xml字符串解析

标签:style   c   class   blog   code   java   

原文地址:http://www.cnblogs.com/blogFree/p/3754878.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!