book.xml
<?xml version="1.0" encoding="UTF-8"?>
<书籍列表>
<武侠小说 isbn = "1234" hot="true" big = "very">
<书名><<天龙八部>></书名>
<作者>金庸</作者>
<mobile>12345678</mobile>
<价格>50</价格>
<册数>6</册数>
<简介>一本好书</简介>
</武侠小说>
<武侠小说>
<书名><<笑傲江湖>></书名>
<作者>金庸</作者>
<mobile>12345678</mobile>
<价格>60</价格>
<册数>4</册数>
<简介>一本好书</简介>
</武侠小说>
<爱情小说>
<书名><<倾城绝恋>></书名>
<作者>张爱玲</作者>
<mobile>12345678</mobile>
<价格>60</价格>
<册数>4</册数>
<简介>一本好书</简介>
</爱情小说>
<爱情小说>
<书名><<京华烟云>></书名>
<作者>张爱玲</作者>
<mobile>12345678</mobile>
<价格>60</价格>
<册数>4</册数>
<简介>一本好书</简介>
</爱情小说>
</书籍列表>
readXmlDemo:
package day0712;
import java.io.File;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import com.sun.faces.renderkit.Attribute;
public class ReadXmlDemo {
public static void main(String[] args) {
readBook("src/book.xml");
}
public static void readBook(String filename) {
SAXReader reader = new SAXReader();
File file = new File(filename);
try {
Document doc = reader.read(file);
Element rootElmt = doc.getRootElement();
List list = (List) rootElmt.elements("武侠小说");
parseNovel(list);
} catch (DocumentException e) {
e.printStackTrace();
}
}
public static void parseNovel(List list) {
Iterator it = list.iterator();
while(it.hasNext()){
Element novelElmt = (Element) it.next();
System.out.println("书名:" + novelElmt.elementText("书名"));
System.out.println("作者:" + novelElmt.elementText("作者"));
System.out.println("价格:" + novelElmt.elementText("价格"));
System.out.println("册数:" + novelElmt.elementText("册数"));
System.out.println("简介:" + novelElmt.elementText("简介"));
System.out.println("########################");
List attrList = novelElmt.attributes();
Iterator attrIt = attrList.iterator();
while(attrIt.hasNext()){
org.dom4j.Attribute attr = (org.dom4j.Attribute) attrIt.next();
System.out.println(attr.getName() + "=" + attr.getValue());
}
}
}
}
解析结果:
书名:<<天龙八部>>
作者:金庸
价格:50
册数:6
简介:一本好书
########################
isbn=1234
hot=true
big=very
书名:<<笑傲江湖>>
作者:金庸
价格:60
册数:4
简介:一本好书
########################
readxmldemo01:
package day0712;
import java.util.Iterator;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class ReadXmlDemo01 {
public static void main(String[] args) throws DocumentException {
readXML();
}
public static void readXML() throws DocumentException {
SAXReader sr = new SAXReader();
Document doc = sr.read("src/book.xml");
Element el_root = doc.getRootElement();
Iterator it = el_root.elementIterator();
while(it.hasNext()){
Object o = it.next();
Element el_row = (Element)o;
String s= el_row.getText();
Iterator it_row = el_row.elementIterator();
while(it_row.hasNext()){
Element el_ename = (Element) it_row.next();
System.out.println(el_ename.getText());
}
}
}
}
解析结果:
<<天龙八部>>
金庸
12345678
50
6
一本好书
<<笑傲江湖>>
金庸
12345678
60
4
一本好书
<<倾城绝恋>>
张爱玲
12345678
60
4
一本好书
<<京华烟云>>
张爱玲
12345678
60
4
一本好书
test01.xml
<?xml version="1.0" encoding="UTF-8" ?><!-- XML声明 -->
<package name="struts2" extends="jfreechart-default">
<action name="addVote" class="com.sanqing.action.AddVoteAction">
<result name="success">addVote.jsp</result>
</action>
<action name="showVote" class="com.sanqing.action.ShowVoteAction">
<result name="success">showVote.jsp</result>
</action>
<action name="deleteVote" class="com.sanqing.action.DeleteVoteAction">
<result name="success" type="chain">showVote</result>
</action>
<action name="showVoteByChannel" class="com.sanqing.action.ShowVoteByChannelAction">
<result name="success">index.jsp</result>
<result name="input">index.jsp</result>
</action>
<action name="voteResult" class="com.sanqing.action.VoteResultAction">
<result name="success" type="chart">
<param name="width">400</param>
<param name="height">300</param>
</result>
</action>
<action name="doVote" class="com.sanqing.action.DoVoteAction">
<result name="success" type="chain">voteResult</result>
<result name="input" type="chain">showVoteByChannel</result>
</action>
</package>
readxmldemo01:
package day0712;
import java.util.Iterator;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class ReadXmlDemo01 {
public static void main(String[] args) throws DocumentException {
readXML();
}
public static void readXML() throws DocumentException {
SAXReader sr = new SAXReader();
Document doc = sr.read("src/test01.xml");
Element el_root = doc.getRootElement();
Iterator it = el_root.elementIterator();
while(it.hasNext()){
Object o = it.next();
Element el_row = (Element)o;
String s= el_row.getText();
Iterator it_row = el_row.elementIterator();
while(it_row.hasNext()){
Element el_ename = (Element) it_row.next();
System.out.println(el_ename.getText());
}
}
}
}
解析结果:
addVote.jsp
showVote.jsp
showVote
index.jsp
index.jsp
voteResult
showVoteByChannel
readxmldemo:
package day0712;
import java.io.File;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import com.sun.faces.renderkit.Attribute;
public class ReadXmlDemo {
public static void main(String[] args) {
readBook("src/test01.xml");
}
public static void readBook(String filename) {
SAXReader reader = new SAXReader();
File file = new File(filename);
try {
Document doc = reader.read(file);
Element rootElmt = doc.getRootElement();
List list = (List) rootElmt.elements("action");
parseNovel(list);
} catch (DocumentException e) {
e.printStackTrace();
}
}
public static void parseNovel(List list) {
Iterator it = list.iterator();
while(it.hasNext()){
Element novelElmt = (Element) it.next();
System.out.println("书名:" + novelElmt.elementText("书名"));
System.out.println("作者:" + novelElmt.elementText("作者"));
System.out.println("价格:" + novelElmt.elementText("价格"));
System.out.println("册数:" + novelElmt.elementText("册数"));
System.out.println("简介:" + novelElmt.elementText("简介"));
System.out.println("########################");
List attrList = novelElmt.attributes();
Iterator attrIt = attrList.iterator();
while(attrIt.hasNext()){
org.dom4j.Attribute attr = (org.dom4j.Attribute) attrIt.next();
System.out.println(attr.getName() + "=" + attr.getValue());
}
}
}
}
解析结果:
书名:null
作者:null
价格:null
册数:null
简介:null
########################
name=addVote
class=com.sanqing.action.AddVoteAction
书名:null
作者:null
价格:null
册数:null
简介:null
########################
name=showVote
class=com.sanqing.action.ShowVoteAction
书名:null
作者:null
价格:null
册数:null
简介:null
########################
name=deleteVote
class=com.sanqing.action.DeleteVoteAction
书名:null
作者:null
价格:null
册数:null
简介:null
########################
name=showVoteByChannel
class=com.sanqing.action.ShowVoteByChannelAction
书名:null
作者:null
价格:null
册数:null
简介:null
########################
name=voteResult
class=com.sanqing.action.VoteResultAction
书名:null
作者:null
价格:null
册数:null
简介:null
########################
name=doVote
class=com.sanqing.action.DoVoteAction