2010-12-20 47 views
2

我試圖閱讀下列標籤的XML文件,但SAX解析器無法讀取嵌套的標籤,如閱讀標籤嵌套使用SAX解析器

<active-prod-ownership> 
    <ActiveProdOwnership> 
    <Product code="3N3" component="TRI_SCORE" orderNumber="1-77305469" /> 
    </ActiveProdOwnership> 
</active-prod-ownership> 

這裏是我使用

public class LoginConsumerResponseParser extends DefaultHandler { 
// =========================================================== 
// Fields 
// =========================================================== 
static String str="default"; 
private boolean in_errorCode=false; 
private boolean in_Ack=false; 
private boolean in_activeProdOwnership= false; 
private boolean in_consumerId= false; 
private boolean in_consumerAccToken=false; 


    public void startDocument() throws SAXException { 
    Log.e("i am ","in start document"); 
} 


public void endDocument() throws SAXException { 
    // Nothing to do 
    Log.e("doc read", " ends here"); 
} 

/** Gets be called on opening tags like: 
    * <tag> 
    * Can provide attribute(s), when xml was like: 
    * <tag attribute="attributeValue">*/ 

public void startElement(String namespaceURI, String localName, 
    String qName, Attributes atts) throws SAXException { 
    if(localName.equals("ack")){ 
    in_Ack=true; 
    } 
    if(localName.equals("error-code")){ 
    in_errorCode=true; 
    } 
    if(localName.equals("active-prod-ownership")){ 
    Log.e("in", "active product ownership"); 
    in_activeProdOwnership=true; 
    } 
    if(localName.equals("consumer-id")){ 
    in_consumerId= true; 
    } 
    if(localName.equals("consumer-access-token")) 
    { 
    in_consumerAccToken= true; 
    } 
} 

/** Gets be called on closing tags like: 
    * </tag> */ 

public void endElement(String namespaceURI, String localName, String qName) 
throws SAXException { 
    if(localName.equals("ack")){ 
    in_Ack=false; 
    } 
    if(localName.equals("error-code")){ 
    in_errorCode=false; 
    } 
    if(localName.equals("active-prod-ownership")){ 
    in_activeProdOwnership=false; 
    } 
    if(localName.equals("consumer-id")){ 
    in_consumerId= false; 
    } 
    if(localName.equals("consumer-access-token")) 
    { 
    in_consumerAccToken= false; 
    } 
} 

/** Gets be called on the following structure: 
    * <tag>characters</tag> */ 

public void characters(char ch[], int start, int length) { 

    if(in_Ack){ 
    str= new String(ch,start,length); 
    } 
    if(str.equalsIgnoreCase("success")){ 
    if(in_consumerId){ 

    } 
    if(in_consumerAccToken){ 

    } 
    if(in_activeProdOwnership){ 
    str= new String(ch,start,length); 
    Log.e("active prod",str); 
    } 
    } 
} 
} 
代碼

,但是在達到標籤in_activeProdOwnersip只讀「<」爲標籤的內容

請幫助我需要整個數據被讀取

+0

請上傳您所期望的輸出的一個樣本。由於您的示例不包含純文本數據,而只包含具有屬性的嵌套標記,所以我認爲您可能期望一些永遠不會發生的事情,但需要確保某些上下文。 – 2010-12-20 12:29:17

回答

3

XML文件和解析器中的標記不匹配。我認爲你是混合標籤屬性名稱。下面是正確分析示例XML代碼:

public class LoginConsumerResponseParser extends DefaultHandler { 
    public void startDocument() throws SAXException { 
     System.out.println("startDocument()"); 
    } 

    public void endDocument() throws SAXException { 
     System.out.println("endDocument()"); 
    } 

    public void startElement(String namespaceURI, String localName, 
          String qName, Attributes attrs) 
     throws SAXException { 

     if (qName.equals("ActiveProdOwnership")) { 
      inActiveProdOwnership = true; 
     } else if (qName.equals("Product")) { 
      if (!inActiveProdOwnership) { 
       throw new SAXException("Product tag not expected here."); 
      } 
      int length = attrs.getLength(); 
      for (int i=0; i<length; i++) { 
       String name = attrs.getQName(i); 
       System.out.print(name + ": "); 
       String value = attrs.getValue(i); 
       System.out.println(value);    
      } 
     }    
    } 

    public void endElement(String namespaceURI, String localName, String qName) 
     throws SAXException { 

     if (localName.equals("ActiveProdOwnership")) 
      inActiveProdOwnership = false; 
    } 

    public void characters(char ch[], int start, int length) { 
    } 

    public static void main(String args[]) throws Exception { 
     String xmlFile = args[0]; 
     File file = new File(xmlFile); 
     if (file.exists()) { 
      SAXParserFactory factory = SAXParserFactory.newInstance(); 
      SAXParser parser = factory.newSAXParser(); 
      DefaultHandler handler = new Test(); 
      parser.parse(xmlFile, handler); 
     } 
     else { 
      System.out.println("File not found!"); 
     } 
    } 

    private boolean inActiveProdOwnership = false; 
} 

樣品運行將產生以下的輸出:

startDocument() 
code: 3N3 
component: TRI_SCORE 
orderNumber: 1-77305469 
endDocument() 
+1

+1:可能接近他之後的東西。 – 2010-12-20 12:48:12

0

我懷疑這是什麼錯誤:

new String(ch,start,length); 

在這裏,你傳遞一個char[]String構造,但是構造應該採取byte[]。最終的結果是你得到一個損壞的字符串。

我建議您改爲讓str領域StringBuilder,而不是一個String,然後用這個:

builder.append(ch,start,length); 

然後你需要清除StringBuilder每次startElement()被調用。