阅读标签嵌套使用SAX解析器

问题描述:

我试图阅读下列标签的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

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

我怀疑这是什么错误:

new String(ch,start,length); 

在这里,你传递一个char[]String构造,但是构造应该采取byte[]。最终的结果是你得到一个损坏的字符串。

我建议您改为让str领域StringBuilder,而不是一个String,然后用这个:

builder.append(ch,start,length); 

然后你需要清除StringBuilder每次startElement()被调用。