当前位置:编程学习 > XML/UML >>

Android XML解析器的问题

最近在项目中遇到了一个解析XML的问题,我们是用android自带的DOM解析器来解析XML的,但发现了一个android的问题,那就是在2.3的SDK上面,无法解析像<, >, 等字符串。

尽管我们从服务器端返回的数据中,应该是不能包含< >这样的字符,应该使用转义,但有时候,由于历史原因,导致服务器端不能作这样的修正,所以这样的问只能是在客户端来解决了。下面我就说一说我们是如何解决这种问的。

1,现象
我们的解析代码是:
[java] DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();  
DocumentBuilder builder = factory.newDocumentBuilder();  
Document documnet = builder.parse(in); 
Element root = documnet.getDocumentElement(); 

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document documnet = builder.parse(in);
Element root = documnet.getDocumentElement();其中builder.parse(in)中的in是一个InputStream类型的输入流,例如有如下一段XML:


[html] <?xml version="1.0" ?> 
<data> 
    <success>1</success> 
    <error> 
        <code></code> 
        <message></message> 
    </error> 
    <result> 
        <history_info_list> 
            <row> 
                <purchase_info_id>dnrxmauxecj3z6e4</purchase_info_id> 
                <title_id>134051</title_id> 
                <title>まもって守護月天!再逢<Retrouvailles></title> 
                <volume_number>001</volume_number> 
                <author_name>桜野みねね</author_name> 
                <contents_name>まもって守護月天!再逢<Retrouvailles> 1巻</contents_name> 
                <date_open>2011-12-02</date_open> 
                <purchase_date>2012-02-06 18:39:48</purchase_date> 
                <image_url>/resources/c_media/images/thumb/262/134051_01_1_L.jpg</image_url> 
                <contents> 
                    <story_number>1</story_number> 
                    <contents_id>BT000013405100100101500014</contents_id> 
                    <file_size>34168162</file_size> 
                    <Within_Wifi>0</Within_Wifi> 
                </contents> 
                <text_to_speech_易做图>0</text_to_speech_易做图> 
                <restrict_num>-1</restrict_num> 
                <issue>3</issue> 
                <subscription>0</subscription> 
                <易做图_易做图>0</易做图_易做图> 
            </row> 
        </history_info_list> 
    </result> 
</data> 

<?xml version="1.0" ?>
<data>
    <success>1</success>
    <error>
        <code></code>
        <message></message>
    </error>
    <result>
        <history_info_list>
            <row>
                <purchase_info_id>dnrxmauxecj3z6e4</purchase_info_id>
                <title_id>134051</title_id>
                <title>まもって守護月天!再逢<Retrouvailles></title>
                <volume_number>001</volume_number>
                <author_name>桜野みねね</author_name>
                <contents_name>まもって守護月天!再逢<Retrouvailles> 1巻</contents_name>
                <date_open>2011-12-02</date_open>
                <purchase_date>2012-02-06 18:39:48</purchase_date>
                <image_url>/resources/c_media/images/thumb/262/134051_01_1_L.jpg</image_url>
                <contents>
                    <story_number>1</story_number>
                    <contents_id>BT000013405100100101500014</contents_id>
                    <file_size>34168162</file_size>
                    <Within_Wifi>0</Within_Wifi>
                </contents>
      

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,