DOM解析。求大神调试错误,谢谢
主要是看看我写的DOM解析的地方哪里错了try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse("src/movie.xml");
// 首先获得movie根节点的集合
NodeList list = doc.getElementsByTagName("movie");
// 遍历获得MOVIE的子节点
int num=0;
for (int i = 0; i < list.getLength() && list != null; i++) {
Element timeElement = null;
Element ziElement=null;
String ziElementName = null;
String value = null;
String time[]=new String[4];
Element element = (Element) list.item(i);
NodeList zilist = element.getChildNodes();
for (int j = 0; j < zilist.getLength()&& (zilist != null); j++) {
Node node = zilist.item(j);
if (node.getNodeType() == Node.ELEMENT_NODE) {
ziElement = (Element) node;
ziElementName = ziElement.getNodeName();
value = ziElement.getTextContent();
if (ziElementName.equals("name")) {
movie.setMovieName(value);
} else if (ziElementName.equals("poster")) {
movie.setPoster(value);
} else if (ziElementName.equals("director")) {
movie.setDirector(value);
} else if (ziElementName.equals("actor")) {
movie.setActor(value);
} else if (ziElementName.equals("price")) {
movie.setPrice(value);
} else if (ziElementName.equals("schedule")) {
NodeList timelist = ziElement.getChildNodes();
for (int q = 0; q < timelist.getLength()&& timelist != null; q++) {
Node timeNode = timelist.item(q);
if (timeNode.getNodeType() == Node.ELEMENT_NODE) {
timeElement = (Element) timeNode;
time[q] = timeElement.getTextContent();
num++;
}
}
}
}
}
for(int w=0;w<num;w++){
scheduleItem = new ScheduleItem(movie, time[w]);
items.add(scheduleItem);
}
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
xml:
<?xml version="1.0" encoding="UTF-8"?>
<showList>
<movie>
<name>非常完美</name>
<poster>perfect</poster>
<director>阴萌</director>
<actor>范冰冰</actor>
<type>Romance</type>
<price>60</price>
<schedule>
<item>09:00</item>
<item>13:00</item>
</schedule>
</movie>
<movie>
<name>源代码</name>
<poster>sourcecode</poster>
<director>邓肯琼斯</director>
<actor>杰克</actor>
<type>thriller</type>
<price>60</price>
<schedule>
<item>10:00</item>
<item>14:00</item>
</schedule>
</movie>
</showList>
结果:
1源代码 源代码 邓肯琼斯 杰克 Action null
2源代码 源代码 邓肯琼斯 杰克 Action 09:00
3源代码 源代码 邓肯琼斯 杰克 Action null
4源代码 源代码 邓肯琼斯 杰克 Action 10:00
5源代码 源代码 邓肯琼斯 杰克 Action null
6源代码 源代码 邓肯琼斯 杰克 Action 14:00
--------------------编程问答-------------------- 代码好长,先顶贴再说
补充:Java , Java EE