XmlPullParser解析xml文件

xml:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://webservice.datasource.gosun.com/">
    <soapenv:Header></soapenv:Header>
    <soapenv:Body>
        <AddRecordRes>
            <code>14</code>
            <message>成功</message>
        </AddRecordRes>
    </soapenv:Body>
</soapenv:Envelope>
public String createXml() {    
    StringBuilder sb = new StringBuilder("<soapenv:Envelope " + 
        "xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" " +            
        "xmlns:web=\"http://webservice.datasource.gosun.com/\">");     
    sb.append("<soapenv:Header></soapenv:Header>" + "<soapenv:Body>" +            
        "<AddRecordRes>" + "<code>14</code>" + "<message>成功</message>" +            
        "</AddRecordRes>" + "</soapenv:Body>" + "</soapenv:Envelope>"    );    
    return sb.toString();
}

解析函数 getCommXml(String xml)

public static String getCommXml(String xml) {    
    String commXml = null;    
    if (xml == null) {        
        return null;    
    }    
    Log.i("dido", xml);    
    InputStream inputStream = new ByteArrayInputStream(xml.getBytes());   
    try {        
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();        
        XmlPullParser parser = factory.newPullParser();        
        parser.setInput(inputStream,"UTF-8"); // 设置数据源编码        
        int eventType = parser.getEventType(); //获取事件类型       
        while (eventType != XmlPullParser.END_DOCUMENT) {     
            switch (eventType) {                
                case XmlPullParser.START_TAG: // 开始读取某个标签                    
                    Log.i("Start tag ", parser.getName());                    
                    break;                
                case XmlPullParser.TEXT:                    
                    commXml = parser.getText();                    
                    Log.i("Text ", parser.getText());                    
                    break;                
                case XmlPullParser.END_TAG:                    
                    Log.i("End tag ", parser.getName());                    
                    break;            
            }            
            eventType = parser.next();        
        }        
        inputStream.close();    
    } catch (Exception e) {        
        e.printStackTrace();    
    }    
    return commXml;
}

输出:

I/dido丫: <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://webservice.datasource.gosun.com/"><soapenv:Header></soapenv:Header><soapenv:Body><AddRecordRes><code>14</code><message>成功</message></AddRecordRes></soapenv:Body></soapenv:Envelope>
I/Start tag: soapenv:Envelope
I/Start tag: soapenv:Header
I/End tag: soapenv:Header
I/Start tag: soapenv:Body
I/Start tag: AddRecordRes
I/Start tag: code
I/Text: 14
I/End tag: code
I/Start tag: message
I/Text: 成功
I/End tag: message
I/End tag: AddRecordRes
I/End tag: soapenv:Body
I/End tag: soapenv:Envelope
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,767评论 18 399
  • PHP常用函数大全 usleep() 函数延迟代码执行若干微秒。 unpack() 函数从二进制字符串对数据进行解...
    上街买菜丶迷倒老太阅读 1,384评论 0 20
  • php usleep() 函数延迟代码执行若干微秒。 unpack() 函数从二进制字符串对数据进行解包。 uni...
    思梦PHP阅读 2,009评论 1 24
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,969评论 19 139
  • 甲午年九日春初雪至。晨起寥寥,随后纷纷。不时起略寸,万径景象异,犹不见远山;过盏…炊烟起,新路出。谁家新媳不畏寒,...
    剪烛弄影a却话西窗阅读 266评论 4 3