开发者

Java Help with Logic in XML SAX Handler

I am trying to set up a SAX Handler to parse XML with the following structure:

<Hours>
  <Set name="BUSINESS">
        <MO>
              <Open>09:00:00</Open>
              <Close>17:00:00</Close>
        </MO>
        <TU>
              <Open>09:00:00</Open>
              <Close>17:00:00</Close>
        </TU>
        <WE>
              <Open>09:00:00</Open>
              <Close>17:00:00</Close>
        </WE>
        <TH>
              <Open>09:00:00</Open>
              <Close>17:00:00</Close>
        </TH>
        <FR>
              <Open>09:00:00</Open>
              <Close>17:00:00</Close>
        </FR>
        <SA/>
        <SU/>
  </Set>
  <Set name="LASTCOLLECTION">
        <MO>
              <Close>17:00:00</Close>
        </MO>
        <TU>
              <Close>17:00:00</Close>
        </TU>
        <WE>
              <Close>17:00:00</Close>
        </WE>
        <TH>
              <Close>17:00:00</Close>
        </TH>
        <FR>
              <Close>17:00:00</Close>
        </FR>
        <SA/>
        <SU/>
  </Set>
 </Hours>

I am running into some problems, however. My code will only really get the value of OPEN and CLOSE in MO, not in any of the other days of the week. Can anyone find a flaw in my logic? Here is my handler code:

public void startElement(String uri, String localName, String qName,
        Attributes attributes) throws SAXException {

    if (localName.equalsIgnoreCase("Set")
            && attributes.getValue("name").equalsIgnoreCase(
                    "BUSINESS")) {
        inBusiness = true;
    } else if (localName.equalsIgnoreCase("Set")
            && attributes.getValue("name").equalsIgnoreCase(
                    "LASTCOLLECTION")) {
        inLastCollection = true;
    } else if (localName.equalsIgnoreCase("MO")) {
        inMonday = true;
    } else if (localName.equalsIgnoreCase("TU")) {
        inTuesday = true;
    } else if (localName.equalsIgnoreCase("WE")) {
        inWednesday = true;
    } else if (localName.equalsIgnoreCase("TH")) {
        inThursday = true;
    } else if (localName.equalsIgnoreCase("FR")) {
        inFriday = true;
    } else if (localName.equalsIgnoreCase("SA")) {
        inSaturday = true;
    } else if (localName.equalsIgnoreCase("SU")) {
        inSunday = true;
    } else if (localName.equalsIgnoreCase("Open")) {
        inOpen = true;
    } else if (localName.equalsIgnoreCase("Close")) {
        inClose = true;
    }

}

public void characters(char ch[], int start, int length)
        throws SAXException {

    value = value + new String(ch, start, length).trim();

}

public void endElement(String uri, String name, String qName)
        throws SAXException {

    if (inBusiness) {

        bHours = new BusinessHours();

        if (inMonday) {

            if (inOpen) {
                bHours.setMondayOpen(value);
                inOpen = false;
            } else if (inClose) {
                bHours.setMondayClose(value);
                inClose = false;
            }
            inMonday = false;

        } else if (inTuesday) {
            if (inOpen) {
                bHours.setTuesdayOpen(value);
                inOpen = false;
            } else if (inClose) {
                bHours.setTuesdayClose(value);
                inClose = false;
            }
            inTuesday = false;
        } else if (inWednesday) {
            if (inOpen) {
                bHours.setWednesdayOpen(value);
                inOpen = false;
            } else if (inClose) {
                bHours.setWednesdayClose(value);
                inClose = false;
            }
            inWednesday = false;
        } else if (inThursday) {
            if (inOpen) {
                bHours.setThursdayOpen(value);
                inOpen = false;
            } else if (inClose) {
                bHours.setThursdayClose(value);
                inClose = false;
            }
            inThursday = false;
        } else if (inFriday) {
            if (inOpen) {
                bHours.setFridayOpen(value);
                inOpen = false;
            } else if (inClose) {
                bHours.setFridayClose(value);
                inClose = false;
            }
            inFriday = false;
        } else if (inSaturday) {
            if (inOpen) {
                bHours.setSaturdayOpen(value);
                inOpen = false;
            } else if (inClose) {
                bHours.setSaturdayClose(value);
                inClose = false;
            }
            inSaturday = false;
        } else if (inSunday) {
            if (inOpen) {
                bHours.setSundayOpen(value);
                inOpen = false;
            } else if (inClose) {
                bHours.setSundayClose(value);
                inClose = false;
            }
            inSunday = false;

        }
        myLoc.setBusinessHours(bHours);
        inBusiness = false;
    } else if (inLastCollection) {

        cHours = new LastCollectionHours();

        if (inMonday) {

            if (inOpen) {
                cHours.setMondayOpen(value);
                inOpen = false;
            } else if (inClose) {
                cHours.setMondayClose(value);
                inClose = false;
            }
            inMonday = false;

        } else if (inTuesday) {
            if (inOpen) {
                cHours.setTuesdayOpen(value);
                inOpen = false;
            } else if (inClose) {
                cHours.setTuesdayClose(value);
                inClose = false;
            }
            inTuesday = false;
        } else if (inWednesday) {
            if (inOpen) {
                cHours.setWednesdayOpen(value);
                inOpen = false;
            } else if (inClose) {
                cHours.setWednesdayClose(value);
                inClose = false;
            }
            inWednesday = false;
        } else if (inThursday) {
            if (inOpen) {
                cHours.setThursdayOpen(value);
                inOpen = false;
            } else if (inClose) {
                cHours.setThursdayClose(value);
                inClose = false;
            }
            inThursday = false;
        } else if (inFriday) {
            if (inOpen) {
                cHours.setFridayOpen(value);
                inOpen = false;
            } else if (inClose) {
                cHours.setFridayClose(value);
                inClose = false;
            }
            inFriday = false;
        } else if (inSaturday) {
            if (inOpen) {
                cHours.setSaturdayOpen(value);
                inOpen = false;
            } else if (inClose) {
                cHours.setSaturdayClose(value);
                inClose = false;
            }
            inSaturday = false;
        } else if (inSunday) {
            if (inOpen) {
                cHours.setSundayOpen(value);
                inOpen = false;
            } else if (inClose) {
                cHours.setSundayClose(value);
                inClose = false;
            }
            inSunday = false;

        }
        myLoc.setLastCollectionHours(cHours);
        inLastCollection = false;
    }

}

UPDATED CODE:

public void startElement(String uri, String localName, String qName,
        Attributes attributes) throws SAXException {

    value = new String();

    if (localName.equalsIgnoreCase("Set")
            && attributes.getValue("name").equalsIgnoreCase("BUSINESS")) {
        currentElement = 1;
        bHours = new BusinessHours();

    } else if (localName.equalsIgnoreCase("Set")
            && attributes.getValue("name").equalsIgnoreCase(
                    "LASTCOLLECTION")) {
        currentElement = 2;
        cHours = new LastCollectionHours();
    } else if (localName.equalsIgnoreCase("MO")) {
        day = 1;
    } else if (localName.equalsIgnoreCase("TU")) {
        day = 2;
    } else if (localName.equalsIgnoreCase("WE")) {
        day = 3;
    } else if (localName.equalsIgnoreCase("TH")) {
        day = 4;
    } else if (localName.equalsIgnoreCase("FR")) {
        day = 5;
    } else if (localName.equalsIgnoreCase("SA")) {
        day = 6;
    } else if (localName.equalsIgnoreCase("SU")) {
        day = 7;
    } else if (localName.equalsIgnoreCase("Open")) {
        open = 1;
    } else if (localName.equalsIgnoreCase("Close")) {
        open = 2;
    }

}

public void characters(char ch[], int start, int length)
        throws SAXException {

    value = value + new String(ch, start, length).trim();

} // end Method Characters

public void endElement(String uri, String name, String qName)
        throws SAXException {

    if (name.equalsIgnoreCase("Set") && currentElement == 1) {
        myLoc.setBusinessHours(bHours);
    } else if (name.equalsIgnoreCase("Set") && currentElement == 2) {
        myLoc.setLastCollectionHours(cHours);

    }

    if (currentElement == 1) {

        if (day == 1) {

            if (open == 1) {
                bHours.setMondayOpen(value);
            } else if (open == 2) {
                bHours.setMondayClose(value);
            }

        } else if (day == 2) {

            if (open == 1) {
                bHours.setTuesdayOpen(value);
            } else if (open == 2) {
                bHours.setTuesdayClose(value);
            }
        } else if (day == 3) {

            if (open == 1) {
                bHours.setWednesdayOpen(value);
            } else if (open == 2) {
                bHours.setWednesdayClose(value);
            }
        } else if (day == 4) {

            if (open == 1) {
                bHours.setThursdayOpen(value);
            } else if (open == 2) {
                bHours.setThursdayClose(value);
            }
        } else if (day == 5) {

            if (open == 1) {
                bHours.setFridayOpen(value);
            } else if (open == 2) {
                bHours.setFridayClose(开发者_如何转开发value);
            }
        } else if (day == 6) {

            if (open == 1) {
                bHours.setSaturdayOpen(value);
            } else if (open == 2) {
                bHours.setSaturdayClose(value);
            }
        } else if (day == 7) {

            if (open == 1) {
                bHours.setSundayOpen(value);
            } else if (open == 2) {
                bHours.setSundayClose(value);
            }

        }

    } else if (currentElement == 2) {

        if (day == 1) {

            if (open == 1) {
                cHours.setMondayOpen(value);
            } else if (open == 2) {
                cHours.setMondayClose(value);
            }

        } else if (day == 2) {

            if (open == 1) {
                cHours.setTuesdayOpen(value);
            } else if (open == 2) {
                cHours.setTuesdayClose(value);
            }
        } else if (day == 3) {

            if (open == 1) {
                cHours.setWednesdayOpen(value);
            } else if (open == 2) {
                cHours.setWednesdayClose(value);
            }
        } else if (day == 4) {

            if (open == 1) {
                cHours.setThursdayOpen(value);
            } else if (open == 2) {
                cHours.setThursdayClose(value);
            }
        } else if (day == 5) {

            if (open == 1) {
                cHours.setFridayOpen(value);
            } else if (open == 2) {
                cHours.setFridayClose(value);
            }
        } else if (day == 6) {

            if (open == 1) {
                cHours.setSaturdayOpen(value);
            } else if (open == 2) {
                cHours.setSaturdayClose(value);
            }
        } else if (day == 7) {

            if (open == 1) {
                cHours.setSundayOpen(value);
            } else if (open == 2) {
                cHours.setSundayClose(value);
            }

        }

    }

}

Thanks!


On Line 116 You are setting inBusiness = false - which will get set after monday's open/close. Next time around it fails the if(inBusiness) on line 44.

Instead of using a inBusiness boolean, use a int indicator as follows:

private int curElement = 0;

public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {

if (localName.equalsIgnoreCase("Set")
        && attributes.getValue("name").equalsIgnoreCase(
                "BUSINESS")) {
    curElement = 1;
} else if (localName.equalsIgnoreCase("Set")
        && attributes.getValue("name").equalsIgnoreCase(
                "LASTCOLLECTION")) {
    curElement = 2;
}

......

}

public void endElement(String uri, String name, String qName) throws SAXException {

if (curElement == 1) {

    ....
    //Remove inBusiness= false;
} else if (curElement == 2) {
    ...
    //Remove inLastCollection = false;
}

}

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜