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.
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?
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 = ""; } `