开发者

How to adjust my code to this situation for SAX XML parsing in Android

On advice on someone here on Stackoverflow I changed my method of parsing to the SAXParser.

Thanks to different tutorials I'm able to get it to work, and I have to say that it does work faster (which is very important for my app).

The problem, however, is that my XML file goes deeper than the tutorial's example XML's I've seen.

This a sample of my XML file:

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

I can get the personal details like name, but my code doesn't seem to work with the insurances. I think this is because it's one node more.

This is the code I'm using in my Handler class:

@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);
        curr开发者_JAVA百科entElement = false;
    }

So how must I adjust it?


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


Similarly do it for other cases... 'BreakdownInsurance' is a boolean. use it as a Flag...


The depth should not be a problem. I have more levels and the exact code works fine for me. Could it be that you have several nodes with the same name? "Name" in Personal, and "Name" in those insurances nodes?


Just modify the endElement()...

Add Flags to indicate where current Name is to be saved since you are having Name coming under both <BreakdownInsurance> and <Personal>.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜