0

Please could You help me to resole problem I meet during exploring saxparser.

my xml file

<row>
    <PSC>10000</PSC>
    <NAZEV>Praha 10</NAZEV>
    <ADRESA>Černokostelecká 2020/20, Strašnice, 10000, Praha 10</ADRESA>
    <TYP>pošta</TYP>
    <OTEV_DOBY>
        <den name="Pondělí">
            <od_do>
                <od>08:00</od>
                <do>19:30</do>
            </od_do>
        </den>
        <den name="Úterý">
            <od_do>
                <od>08:00</od>
                <do>19:30</do>
            </od_do>
        </den>
        <den name="Středa">
            <od_do>
                <od>08:00</od>
                <do>19:30</do>
            </od_do>
        </den>
        <den name="Čtvrtek">
            <od_do>
                <od>08:00</od>
                <do>19:30</do>
            </od_do>
        </den>
        <den name="Pátek">
            <od_do>
                <od>08:00</od>
                <do>19:30</do>
            </od_do>
        </den>
        <den name="Sobota">
            <od_do>
                <od>09:00</od>
                <do>13:00</do>
            </od_do>
        </den>
        <den name="Neděle"/>
    </OTEV_DOBY>
    <SOUR_X>1044922.91</SOUR_X>
    <SOUR_Y>737904.98</SOUR_Y>
    <OBEC>Praha</OBEC>
    <C_OBCE>Strašnice</C_OBCE>
    <SOUR_X_WGS84>14.492777</SOUR_X_WGS84>
    <SOUR_Y_WGS84>50.076442</SOUR_Y_WGS84>
    <STAV>nová</STAV>
</row>

I try catch opening and closing time, from elements "od" and "do" this is my code in startElement

else if (qName.equalsIgnoreCase("OTEV_DOBY")) {
        otwarte = new Otwarte();
        b_otev_doby = true;
    }
    if (qName.equalsIgnoreCase("den")) {
        den = attributes.getValue("name");
        if (den.equalsIgnoreCase("Pondělí")) {
            if (qName.equalsIgnoreCase("od"))
                b_od_Pn = true;
            else if (qName.equalsIgnoreCase("do"))
                b_do_Pn = true;
        } else if (den.equalsIgnoreCase("Úterý")) {
            if (qName.equalsIgnoreCase("od")) {
                b_od_Wt = true;
            } else if (qName.equalsIgnoreCase("do")) {
                b_do_Wt = true;
            }
        } else if (den.equalsIgnoreCase("Středa")) {
            if (qName.equalsIgnoreCase("od")) {
                b_od_Sr = true;
            } else if (qName.equalsIgnoreCase("do")) {
                b_do_Sr = true;
            }
        } else if (den.equalsIgnoreCase("Čtvrtek")) {
            if (qName.equalsIgnoreCase("od")) {
                b_od_Cz = true;
            } else if (qName.equalsIgnoreCase("do")) {
                b_do_Cz = true;
            }
        } else if (den.equalsIgnoreCase("Pátek")) {
            if (qName.equalsIgnoreCase("od")) {
                b_od_Pt = true;
            } else if (qName.equalsIgnoreCase("do")) {
                b_do_Pt = true;
            }
        } else if (den.equalsIgnoreCase("Sobota")) {
            if (qName.equalsIgnoreCase("od")) {
                b_od_Sob = true;
            } else if (qName.equalsIgnoreCase("do")) {
                b_do_Sob = true;
            }
        } else if (den.equalsIgnoreCase("Neděle")) {
            if (qName.equalsIgnoreCase("od")) {
                b_od_Nd = true;
            } else if (qName.equalsIgnoreCase("do")) {
                b_do_Nd = true;
            }
        }
    }

at the nested if "if (qName.equalsIgnoreCase("od"))" i got always "Condition 'qName.equalsIgnoreCase("od")' is always 'false'" and result _> Otwarte{pn_od='null', pn_do='null'. Any ideas how to resolv this problem.

0

1 Answer 1

0

The example below will use booleans to validate at what point you are in when parsing.

Please see reference: How to get element's value from XML using SAX parser in startElement?

   public class SimpleHandler extends DefaultHandler {


    class DenObject{
        public String od;
        public String do;

       //Getters/Setters
    }

    boolean isDo, isOd;

    DenObject currentDenObj;

    List<DenObject> denObjects = new ArrayList<>();

    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes atts) throws SAXException {

        if(qName.equals("den")){ 
            currentDenObj = new DenObject ();
        }
        if(qName.equals("do")) { isDo = true; }
        if(qName.equals("od"))  { isOd = true;  }
    }

    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {

        if(qName.equals("den")) {
            denObjects .add(currentDenObj);
            currentDenObj = null;
        }
        if(qName.equals("do")) { isDo = false; }
        if(qName.equals("od"))  { isOd = false;  }
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        if (isDo) {
            currentDenObj.setDo(new String(ch, start, length));
        }
        if (isOd) {
            currentDenObj.setOd(new String(ch, start, length));
        }
    }

  @Override
    public void endDocument() throws SAXException {
        for(DenObject denObject : denObjects ) {

                System.out.println(denObject);
        }
    }

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.