项目是做一个电影院售票系统,不用管前端,也不用做数据库,只需要把逻辑层写出来就行了,由于刚学不久,有很多代码都不会写,求大神指点。
以下是XML文件:
<?xml version="1.0" encoding="gbk"?>
<ShowList>
<Movies>
<Movie>唐老鸭大战比卡超</Movie>
<Poster>东西方文化战争纪录片</Poster>
<Director>xxx</Director>
<Type>War</Type>
<Price>60</Price>
<Schedule>
<Item>09:30</Item>
<Item>19:00</Item>
</Schedule>
</Movies>
<Movies>
<Movie>兰桂坊的日与夜</Movie>
<Poster>用镜头看遍爱情的真理</Poster>
<Director>SSS</Director>
<Type>Thriller</Type>
<Price>60</Price>
<Schedule>
<Item>10:30</Item>
<Item>15:00</Item>
</Schedule>
</Movies>
</ShowList>
将这个XML文件读取出来,做到for循环时,就不会写了:
以下是我自己 写的代码:
import java.io.File;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class DOM4jMovie {
public static void main(String[] args){
File file=new File("e:/Movie.xml");
SAXReader reader=new SAXReader();
try {
Document doc=reader.read(file);
Element Movies=doc.getRootElement();
List list=Movies.elements("Movies");
for(Object e:list){
for(int i=0;i<=2;i++){
Element a=(Element) e;
System.out.println(a.element("Movie").getText()+"\t"
+a.element("Poster").getText()+"\t"
+a.element("Director").getText()+"\t"
+a.element("Type").getText()+"\t"
+a.element("Price").getText()+"\t"
+a.element("Schedule").getText());
}
}
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
因为在XML里的时间场次是两场,而上面的循环,只是一个循环,而Scheduele的场次就读不出来,如何用嵌套循环将文件读出来,求大神指教
标准库,无需第三方库
import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; /** * */ public class Main { /** * @param args */ public static void main(String[] args) { File file = new File("Movie.xml"); try { DocumentBuilder reader = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc = reader.parse(file); Element root = doc.getDocumentElement(); NodeList movies = root.getElementsByTagName("Movies"); for (int i = 0, size = movies.getLength(); i < size; i++) { Element movie = (Element) movies.item(i) ; // 电影名 String movieName = movie.getElementsByTagName("Movie").item(0).getTextContent(); // String posterName = movie.getElementsByTagName("Poster").item(0).getTextContent(); // 导演 String directorName = movie.getElementsByTagName("Director").item(0).getTextContent(); // String typeName = movie.getElementsByTagName("Type").item(0).getTextContent(); // String price = movie.getElementsByTagName("Price").item(0).getTextContent(); System.out.println("Movie = " + movieName + " ,Poster = " + posterName + " ,Director = " + directorName + " ,Type = " + typeName+ " ,Price = " + price); //获取Schedule Element scheduleEle = (Element) movie.getElementsByTagName("Schedule").item(0); NodeList scheduleItems = scheduleEle.getElementsByTagName("Item"); for(int k=0,leng=scheduleItems.getLength();k<leng;k++) { Node scheduleItem = scheduleItems.item(k); String itemText=scheduleItem.getTextContent(); System.out.println("Item = "+itemText); } } } catch (Exception e) { e.printStackTrace(); } } }
预期输出
Movie = 唐老鸭大战比卡超 ,Poster = 东西方文化战争纪录片 ,Director = xxx ,Type = War ,Price = 60 Item = 09:30 Item = 19:00 Movie = 兰桂坊的日与夜 ,Poster = 用镜头看遍爱情的真理 ,Director = SSS ,Type = Thriller ,Price = 60 Item = 10:30 Item = 15:00
谢谢大神指点,给我打开了路!
百度: java xml复杂类型数据反序列化
注意:JavaBean中结构要和xml中的对应上。比如有实体中包含另外一个实体的集合等情况。
没有听懂楼主什么意思