XML 解析和 HashMap 存储:消息在撇号后被剪切

发布于 2024-11-18 15:37:53 字数 5864 浏览 4 评论 0原文

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

前任。消息:“嘿阿马尔!怎么了?”。像这样切的。

  • 消息=(3577):嘿阿马尔!什么
  • 消息 =(3577): '
  • 消息 =(3577): 起来了?

最终输出是:message = "s up?"

那么如何解决这个问题呢?

HashMap 用于支撑 XML 数据:

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:详细/消息=(3577):嘿阿马尔!什么 07-04 13:38:37.934: 详细/消息 =(3577): ' 07-04 13:38:37.934: 详细/消息 =(3577): 起来了吗?

When I parse following XML and store it in HashMap the message is cut after (') this symbol.

Ex. Message: "Hey amar! What's up?". it cut like this.

  • message =(3577): Hey amar! What
  • message =(3577): '
  • message =(3577): s up?

The final out put is : message = "s up?"

So how to solve this problem ?

HashMap's for Shoring XML DATA :

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>

code:

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;
    }
}

Problem :
Log:
07-04 13:38:37.924: VERBOSE/message =(3577): Hey amar! What
07-04 13:38:37.934: VERBOSE/message =(3577): '
07-04 13:38:37.934: VERBOSE/message =(3577): s up?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

埋情葬爱 2024-11-25 15:37:54

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

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

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

The characters method can be called multiple times by the parser, supplying only part of the characters inside a given tag on each call.

This code does not allow for that happening.

What you have to do is setup a string buffer in the startElement method, collect characters into it in the characters and extract the string result in the endElement method.

最偏执的依靠 2024-11-25 15:37:54

正如唐·罗比(Don Roby)回答的那样......

采用一个字符串变量并在 endElement() 方法中添加到您的映射中,其中您的 in_message 变量变为 false。
例如这样做...

String str = "";

//在characters()方法中
否则如果(消息中)
{
str += 字符;
}
//在endElement()方法中

else if(localName.equalsIgnoreCase("message"))
{
in_message = false;
map.put("消息", str);
Log.i("地图"+i,"message = "+str);
字符串=“”;
}
`

As Don Roby Answered..

Take a String variable and add in your map at endElement() method where your in_message variable goes false.
For Example do this...

String str = "";

//In characters() Method
else if(in_message)
{
str += chars;
}
//In endElement() Method

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文