Reputation: 3409
I want to parse this xml and get the result between the tag... but i cant get the result my xml is
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><loginResponse xmlns="https://comm1.get.com/"><loginResult>true</loginResult><result>success</result></loginResponse></soap:Body></soap:Envelope>
The handler
public class MyXmlContentHandler extends DefaultHandler {
String result;
private String currentNode;
private String currentValue = null;
public String getFavicon() {
return result;
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if (localName.equalsIgnoreCase("result")) {
//offerList = new BFOfferList();
this.result = new String();
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (localName.equalsIgnoreCase("result")) {
result = localName;
}
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
String value = new String(ch, start, length);
if (currentNode.equals("result")){
result = value;
return;
}
}
}
Any changes needed
Upvotes: 0
Views: 159
Reputation: 63814
When you found the start tag you are looking for, "characters" is called one or more times. You have to collect the data not overwrite it. Change
if (currentNode.equals("result")){
result = value;
return;
}
to
if (currentNode.equals("result")){
result += value;
return;
}
Or use StringBuilder to do it. Furthermore, you should remove this, it seems to overwrite your result String:
result = localName;
EDIT:
public class MyXmlContentHandler extends DefaultHandler {
private String result = "";
private String currentNode;
public String getFavicon() {
return result;
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
currentNode = localName;
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
currentNode = null;
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
String value = new String(ch, start, length);
if ("result".equals(currentNode)){
result += value;
}
}
}
Upvotes: 2