XML Parsing和HashMap存储:在撇号后删除消息

时间:2011-07-04 14:07:43

标签: android hashmap xml-parsing saxparser

当我解析以下XML并将其存储在HashMap中时,消息会在(')此符号后被剪切。

实施例。消息:“嘿阿玛!怎么了?”它像这样切割。

  • message =(3577):嘿阿玛!什么
  • message =(3577):'
  • message =(3577):s up?

最终输出是:message =“s up?”

那么如何解决这个问题?

支持XML DATA的HashMap:

public static HashMap<String,String> message_map1 = new HashMap<String,String>();
public static HashMap<String,String> message_map2 = new HashMap<String,String>();
public static HashMap<String,String> message_map3 = new HashMap<String,String>();

XML:

<statuses type="array">
 <status>
  <messageinfo>
    <messageid>485</messageid>
    <userid>58</userid>
    **<message>Hey amar! What's up?</message>**
  </messageinfo>
 <messageinfo>
    <messageid>486</messageid>
    <userid>58</userid>
    **<message>Hey What's up?</message>**
  </messageinfo>
 <messageinfo>
    <messageid>485</messageid>
    <userid>58</userid>
    **<message>What's up?</message>**
  </messageinfo>
 </status>
</statuses>

代码:

package com.xmldataparser;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import android.util.Log;

public class Message_XHandler extends DefaultHandler 
{
     boolean in_porf_msg_statuses;
     boolean in_prof_msg_status;
     boolean in_prof_msg_messageinfo;
     boolean in_prof_msg_messageid;
     boolean in_prof_msg_userid;
     boolean in_prof_msg_message;

    int i=0;
    public String key = "";
    public String value = "";

    XMLData xml_message;

    @Override
    public void startDocument() throws SAXException
    {
        super.startDocument();
        xml_message = new XMLData();
    }

    @Override
    public void startElement(String uri, String localName, String qName,
        Attributes attributes) throws SAXException 
    {
        super.startElement(uri, localName, qName, attributes);

        if(localName.equalsIgnoreCase("statuses"))
        {
            in_porf_msg_statuses= true;
        }
        else if(localName.equalsIgnoreCase("status"))
        {
            in_prof_msg_status = true;
        }
        else if(localName.equalsIgnoreCase("messageinfo"))
        {
            in_prof_msg_messageinfo = true;
            i++;
        }
        else if(localName.equalsIgnoreCase("messageid"))
        {
            in_prof_msg_messageid = true;
        }
        else if(localName.equalsIgnoreCase("userid"))
        {
            in_prof_msg_userid = true;
        }
        else if(localName.equalsIgnoreCase("message"))
        {
            in_prof_msg_message = true;
        }
    }

    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException 
    {
        super.characters(ch, start, length);
        String chars = new String(ch,start,length);
        chars = chars.trim();

        else if(in_prof_msg_messageid)
        {   
            if(i==1)
                {
                    xml_message.message_map1.put("messageid", chars);
                }
            else if(i==2)
                 {
                xml_message.message_map2.put("messageid", chars);
                 }
            else if(i==3)
             {
                xml_message.message_map3.put("messageid", chars);
             }  
            Log.v("messageid = ", chars);
        } 
        else if(in_prof_msg_userid)
        {   
            if(i==1)
                {
                    xml_message.message_map1.put("userid", chars);
                }
            else if(i==2)
                 {
                xml_message.message_map2.put("userid", chars);
                 }
            else if(i==3)
             {
                xml_message.message_map3.put("userid", chars);
             }  

                Log.v("userid = ", chars);
        } 
        else if(in_prof_msg_message)
        {   
            if(i==1)
                {
                xml_message.message_map1.put("message", chars);
                }
            else if(i==2)
                 {
                    xml_message.message_map2.put("message", chars);
                 }
            else if(i==3)
                 {
                    xml_message.message_map3.put("message", chars);
                 }  

                Log.v("message = ", chars);

        } 


        } 


    }

    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException 
    {
        super.endElement(uri, localName, qName);

        if(localName.equalsIgnoreCase("statuses"))
        {
            in_porf_msg_statuses = false;
        }

        else if(localName.equalsIgnoreCase("messageinfo"))
        {
            in_prof_msg_messageinfo = false;
        }
        else if(localName.equalsIgnoreCase("messageid"))
        {
            in_prof_msg_messageid = false;
        }
        else if(localName.equalsIgnoreCase("userid"))
        {
            in_prof_msg_userid= false;
        }
        else if(localName.equalsIgnoreCase("message"))
        {
            in_prof_msg_message = false;
        }
    }

    @Override
    public void endDocument() throws SAXException
    {
        super.endDocument();
    }

    public XMLData getProfileMessageData()
    {
        Log.v("WHERE","Message_XHandler getProfileMessageData()");
        return xml_message;
    }
}

问题: 日志: 07-04 13:38:37.924:VERBOSE / message =(3577):嘿阿玛!什么 07-04 13:38:37.934:VERBOSE / message =(3577):' 07-04 13:38:37.934:VERBOSE / message =(3577):s up?

2 个答案:

答案 0 :(得分:2)

解析器可以多次调用characters方法,在每次调用时只提供给定标记内的部分字符。

此代码不允许发生这种情况。

您需要做的是在startElement方法中设置字符串缓冲区,在characters中将字符收集到其中,并在endElement方法中提取字符串结果。

答案 1 :(得分:1)

Don Roby回答说。

获取一个String变量并在endElement()方法中添加你的in_message变量为false的地图。
例如,这样做......

String str = "";

//在characters()方法中 否则if(in_message) {  str + = chars; } //在endElement()方法

else if(localName.equalsIgnoreCase(“message”)) {    in_message = false;    map.put(“message”,str);    Log.i(“map”+ i,“message =”+ str);   str =“”; } `