2010-11-26 93 views
0

我要分析此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; 
     } 


} 



} 

任何更改需要

+0

你的标签可能是错误的。 iPhone类不是nsxmlparser吗?我会为此推荐“sax”和“xmlparser”。 – 2010-11-26 09:08:16

+0

@Tim它的xmlparser sry ...标签是正确的。 – xydev 2010-11-26 09:11:35

回答

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