解析器出错

时间:2010-11-26 08:58:47

标签: android xml-parsing

我想解析这个xml并在标签之间得到结果...但我不能得到我的xml的结果

<?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> 处理程序

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


}



}

需要进行任何更改

1 个答案:

答案 0 :(得分:2)

当您找到要查找的开始标记时,“字符”会被调用一次或多次。您必须收集数据而不是覆盖它。变化

if (currentNode.equals("result")){
        result = value;
        return;
     }

if (currentNode.equals("result")){
        result += value;
        return;
     }

或者使用StringBuilder来完成它。此外,你应该删除它,它似乎覆盖你的结果字符串:

result = localName;

修改

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