1
I’d like some help on a problem I’m having at Java Android.
First I read a String XML to get the values of each tag. When getting the values reads well the tags, only 9 in 9 Strings received reads in parts, if anyone knows how to solve thanks for the help.
Next I present the code I use to read the XML.
SAXParser saxParser = saxParserFactory.newSAXParser();
Parse_xml parseXMLClass = new Parse_xml();
saxParser.parse(new InputSource(new StringReader(message_posted)), parseXMLClass);
The following is class reading tags:
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException
{
if (localName.equalsIgnoreCase("MSG"))
{
if (listMSG == null)
{
listMSG = new ArrayList<MSG>();
}
}
if (localName.equalsIgnoreCase("Delete"))
{
if (listMSG == null)
{
listMSG = new ArrayList<MSG>();
}
}
if (localName.equalsIgnoreCase("Clear"))
{
if (listMSG == null)
{
listMSG = new ArrayList<MSG>();
}
}
if (localName.equalsIgnoreCase("ID"))
{
bID = true;
}
if (localName.equalsIgnoreCase("DESTINATION"))
{
bDestination = true;
}
if (localName.equalsIgnoreCase("SOURCE"))
{
bSource = true;
}
if (localName.equalsIgnoreCase("DATE"))
{
bDate = true;
}
if (localName.equalsIgnoreCase("SUBJECT"))
{
bSubject = true;
}
if (localName.equalsIgnoreCase("BODY"))
{
bMessage = true;
}
if (localName.equalsIgnoreCase("SMS"))
{
bType = true;
}
if (localName.equalsIgnoreCase("TTL"))
{
config = new Configuration();
bTtl = true;
}
if (localName.equalsIgnoreCase("ATTEMPTS"))
{
bAttempet = true;
}
if (localName.equalsIgnoreCase("SMTP"))
{
bSmtp = true;
}
if (localName.equalsIgnoreCase("PORT"))
{
bPort = true;
}
if (localName.equalsIgnoreCase("USERNAME"))
{
bUsername = true;
}
if (localName.equalsIgnoreCase("PASSWORD"))
{
bPass = true;
}
if (localName.equalsIgnoreCase("EMAIL"))
{
bEmail = true;
}
if (localName.equalsIgnoreCase("STATUS"))
{
bStatus = true;
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException
{
if (localName.equalsIgnoreCase("MSG"))
{
listMSG.add(msg);
msg = new MSG();
}
if (localName.equalsIgnoreCase("DELETE"))
{
listMSG.add(msg);
msg = new MSG();
}
if (localName.equalsIgnoreCase("Clear"))
{
listMSG.add(msg);
msg = new MSG();
}
}
@Override
public void characters(char ch[], int start, int length)
throws SAXException
{
*/
if (bID)
{
String id=String.copyValueOf(ch, start, length).trim();
msg.setId(Integer.parseInt(id));
bID = false;
}
if (bDestination)
{
String destinaton=String.copyValueOf(ch, start, length).trim();
msg.setDestination(destinaton);
bDestination = false;
}
if (bSource)
{
String source=String.copyValueOf(ch, start, length).trim();
msg.setSource(source);
bSource = false;
}
if (bDate)
{
String date=String.copyValueOf(ch, start, length).trim();
msg.setDate(date);
bDate = false;
}
if (bSubject)
{
String subject=String.copyValueOf(ch, start, length).trim();;
msg.setSubject(subject);
bSubject = false;
}
if (bMessage)
{
String body=String.copyValueOf(ch, start, length).trim();
msg.setbody(body);
bMessage = false;
}
if (bType)
{
String type=String.copyValueOf(ch, start, length).trim();
msg.setType(Integer.parseInt(type));
bType = false;
}
if (bStatus)
{
String status=String.copyValueOf(ch, start, length).trim();
msg.setStatus(Integer.parseInt(status));
bStatus = false;
}
if (bTtl)
{
String ttl=String.copyValueOf(ch, start, length).trim();
config.setTtl(Integer.parseInt(ttl));
bTtl = false;
}
if (bAttempet)
{
String attempt=String.copyValueOf(ch, start, length).trim();
config.setAttempt(Integer.parseInt(attempt));
bAttempet = false;
}
if (bSmtp)
{
String smtp=String.copyValueOf(ch, start, length).trim();
config.setSmtp(smtp);
bSmtp = false;
}
if (bPort)
{
String porta=String.copyValueOf(ch, start, length).trim();
config.setPorta(Integer.parseInt(porta));
bPort = false;
}
if (bUsername)
{
String username=String.copyValueOf(ch, start, length).trim();
config.setUsername(username);
bUsername = false;
}
if (bPass)
{
String pass=String.copyValueOf(ch, start, length).trim();
config.setPass(pass);
bPass = false;
}
if (bEmail)
{
String email=String.copyValueOf(ch, start, length).trim();
config.setEmail(email);
bEmail = false;
}
}
Could you post an XML demo? No formatting error? I recommend taking a look at Jackson Fasterxml, it does XML and JSON Parsing, mapping in structure
Map
or Object (using Reflection).– Wakim
Thanks for the help but I got it solved. To solve had to in characters always concatenate the values of why add it in endElemnt. because when the string is long it calls the chacteres several times. Here is the answer in [link][1]. [1]: http://stackoverflow.com/a/10766976/3296571
– Ludger
Show! If you can put the solution as a response and mark as accepted, so if someone comes through this post, you will find the correct solution :)
– Wakim