2010-12-20 10 views
2

SAXパーサーとタグが、SAXパーサーは、ここで読書ネストされた私は、次のタグを使用してXMLファイルを読み込むしようとしています

<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は、タグ

の内容としてのみ「<」を読んでタグを境に私は

01を読み込むべきデータ全体に必要な助けてください
+0

は、あなたが期待する出力のサンプルを投稿してください。

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

サンプルの実行には、次の出力を生成します。サンプルにはプレーンテキストのデータは含まれていませんが、属性を含むネストされたタグのみが含まれているため、おそらく起こりそうもないものがあると思っていますが、 –

答えて

3

XMLファイルとパーサーのタグが一致しません。私はあなたが属性名とタグを混ぜていると思います。ここでは正しくサンプルXMLを解析するコードは次のとおりです。

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

+1:おそらく、彼が何をしているかに近いでしょう。 –

0

私は、これは間違っている何が起こっているのである疑いがある:

ここ
new String(ch,start,length); 

、あなたはStringコンストラクタにchar[]を渡しているが、コンストラクタがbyte[]を取ることになっています。最終的には、あなたは文字列を取得します。

は、私はあなたがstrフィールドStringBuilder、ないStringを作ることを代わりに示唆した後、これを使用する:

builder.append(ch,start,length); 

あなたはその後、呼び出されたStringBuilderたびstartElement()をクリアする必要があります。

関連する問題