Справка по Java с логикой в ​​обработчике XML SAX - PullRequest
0 голосов
/ 01 декабря 2010

Я пытаюсь настроить обработчик SAX для анализа XML со следующей структурой:

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

Однако у меня возникают некоторые проблемы.Мой код действительно получит значения ОТКРЫТЬ и ЗАКРЫТЬ в МО, а не в другие дни недели.Кто-нибудь может найти изъян в моей логике?Вот мой код обработчика:

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

}

ОБНОВЛЕННЫЙ КОД:

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

        }

    }

}

Спасибо!

1 Ответ

2 голосов
/ 01 декабря 2010

On Line 116 Вы устанавливаете inBusiness = false - это будет установлено после понедельника открытия / закрытия.В следующий раз он завершится с ошибкой if (inBusiness) в строке 44.

Вместо использования логического значения inBusiness используйте индикатор int следующим образом:

private int curElement = 0;

public void startElement (String uri, String localName, String qName, атрибуты Attributes) выдает SAXException {

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

......

}

publicvoid endElement (String uri, String name, String qName) выдает SAXException {

if (curElement == 1) {

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

}

...