2010-11-26 98 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; 
    } 
} 
}