我正在尝试设置一个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>
但是,我遇到了一些问题。我的代码只会在MO中获得OPEN和CLOSE的值,而不是在一周中的任何其他日子。任何人都可以在我的逻辑中找到一个缺陷吗?这是我的处理程序代码:
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);
}
}
}
}
谢谢!
答案 0 :(得分:2)
On Line 116你设置inBusiness = false - 将在星期一打开/关闭后设置。下次绕过它会失败第44行的if(inBusiness)。
不使用inBusiness布尔值,而是使用int指示符,如下所示:
private int curElement = 0;
public void startElement(String uri,String localName,String qName, 属性属性)抛出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) 抛出SAXException {
if (curElement == 1) {
....
//Remove inBusiness= false;
} else if (curElement == 2) {
...
//Remove inLastCollection = false;
}
}