2010-12-20 6 views
1

ここでStackoverflowに関するアドバイスをいただき、私の解析方法をSAXParserに変更しました。AndroidでのSAX XML解析のためにこの状況にコードを調整する方法

さまざまなチュートリアルのおかげで、私はそれを動作させることができました。そして、それはより速く動作すると言わなければなりません(これは私のアプリケーションにとって非常に重要です)。

しかし、問題は、私のXMLファイルが、私が見たチュートリアルのXMLの例よりも深くなってしまうことです。

この私のXMLファイルのサンプル:

<Message> 

    <Service>servicename</Service> 

    <Insurances> 

    <BreakdownInsurance> 

     <Name>Insurance name</Name> 

     <InsuranceNR/> 

     <LicenseNr/> 

    </BreakdownInsurance> 

    <CarDamageInsurance> 

     <Name>Insurance name 2</Name> 

     <InsuranceNR></InsuranceNR> 

    </CarDamageInsurance> 
    </Insurances> 

    <Personal> 
    <Name>my name</Name> 
    </Personal> 
</Message> 

私は名前などの個人情報を得ることができますが、私のコードは、保険で動作するようには思えません。私はこれがもっと一つのノードだからだと思う。

これは私が私のHandlerクラスで使用しているコードです:

@Override 
public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { 
    currentElement = true; 
    if (localName.equals("Message")) { 
     geg = new GegevensXML(); 
    } 
} 

@Override 
public void endElement(String namespaceURI, String localName, String qName) throws SAXException { 
    currentElement = false; 

    /********** Autopech **********/ 
    if (localName.equals("Name")) { 
     geg.setAutopechMaatschappij(currentValue); 
    } 
    else if (localName.equals("InsuranceNR")){ 
     geg.setAutopechPolis(currentValue); 
    } 
    else if (localName.equals("LicenseNr")){ 
     geg.setAutopechKenteken(currentValue); 
    } 

@Override 
public void characters(char ch[], int start, int length) { 
    if (currentElement) { 
     currentValue = new String(ch, start, length); 
     currentElement = false; 
    } 

は、どのように私はそれを調整する必要がありますか?

答えて

1
@Override 
public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { 
    currentElement = true; 
    if (localName.equals("Message")) { 
     geg = new GegevensXML(); 
    } 
    if(localName.equals("BreakdownInsurance")) 
    { 
     BreakdownInsurance = true; 
    } 
} 

@Override 
public void endElement(String namespaceURI, String localName, String qName) throws SAXException { 
    currentElement = false; 

    /********** Autopech **********/ 
    if (localName.equals("Name")) 
    { 
    if(BreakdownInsurance) 
    { 
     geg.setBreakdownInsuranceName(currentValue); 

     BreakdownInsurance = false; 
    } 
    else 
    { 
     geg.setAutopechMaatschappij(currentValue); 
     } 

    } 
    else if (localName.equals("InsuranceNR")){ 
     geg.setAutopechPolis(currentValue); 
    } 
    else if (localName.equals("LicenseNr")){ 
     geg.setAutopechKenteken(currentValue); 
    } 


「BreakdownInsurance」は同様にブール値です。フラグとして使用してください...

+0

Thnxは私のために働いた! – Galip

1

ちょうどするendElement()を修正...

は、現在の名前は、あなたが<BreakdownInsurance> <Personal>の両方の下で来て名前を持っているので、保存する場所を示すためにフラグを追加します。

+0

Flagsを追加することはどういう意味ですか? – Galip

1

深さは問題ではありません。私はより多くのレベルがあり、正確なコードは私のためにうまく動作します。あなたは同じ名前のいくつかのノードを持つことができますか? 「名前」は個人、「名前」はその保険契約のノードです。

+0

はい。私は4つの保険すべてにおいて "氏名"と "InsuranceNR"を持っています。 – Galip

+0

はい、わかっています。つまり、それが問題になる可能性があります。 SAXパーサーの仕組みについての簡単な紹介、http://en.wikipedia.org/wiki/Simple_API_for_XMLイベントを検出するので、個人名と保険の名前の違いはわかりません。 – springrolls

+0

Hmm。だから私はこれを修正すべきだとあなたはどのように提案しますか? – Galip

関連する問題