简体   繁体   中英

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);
        currentElement = false;
    }

So how must I adjust it?

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

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?

@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 technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM