XML课程报告文档格式.docx
《XML课程报告文档格式.docx》由会员分享,可在线阅读,更多相关《XML课程报告文档格式.docx(14页珍藏版)》请在冰豆网上搜索。
ELEMENT书名(#PCDATA)>
ELEMENT作者(#PCDATA)>
ELEMENTISBN(#PCDATA)>
ELEMENT出版社(#PCDATA)>
ELEMENT出版时间(#PCDATA)>
ELEMENT单价(#PCDATA)>
ELEMENT图书图片(#PCDATA)>
ELEMENT类别(#PCDATA)>
ELEMENT图书简介(#PCDATA)>
ATTLIST书
idCDATA#REQUIRED
>
]>
书籍>
书id="
1"
书名>
XML基础教程与实验指导<
/书名>
作者>
孙晓非<
/作者>
出版社>
清华大学出版社<
/出版社>
ISBN>
978-7-302-18262-7<
/ISBN>
出版时间>
2008.11<
/出版时间>
价格>
38元<
/价格>
图书图片>
src="
8.jpg"
/图书图片>
类别>
教科书<
/类别>
图书简介>
本书由浅入深,全面地介绍了XML开发和应用知识<
/图书简介>
/书>
/书籍>
2、程序运行结果显示
2、使用SAX方式解析
从XML文档books.xml中获得“书名”、“书号”、“作者”、“出版社”、“出版日期”这5个记录项的信息,并将其格式化输出到一个文件books.txt中。
1、程序源代码:
packagecom.src.sax.xml;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.IOException;
importjava.io.InputStream;
importjava.util.ArrayList;
importjava.util.List;
importjavax.xml.parsers.ParserConfigurationException;
importjavax.xml.parsers.SAXParser;
importjavax.xml.parsers.SAXParserFactory;
importorg.xml.sax.Attributes;
importorg.xml.sax.SAXException;
importorg.xml.sax.helpers.DefaultHandler;
publicclassBooks_SaxReadXmlextendsDefaultHandler{
privateList<
Books>
list;
privateBooksbooks;
privateStringtagName;
publicList<
getList(){
returnlist;
}
publicvoidsetList(List<
list){
this.list=list;
publicBooksgetBooks(){
returnbooks;
}
publicvoidsetBooks(Booksbooks){
this.books=books;
publicStringgetTagName(){
returntagName;
publicvoidsetTagName(StringtagName){
this.tagName=tagName;
publicvoidstartDocument()throwsSAXException{
list=newArrayList<
();
publicvoidstartElement(Stringuri,StringlocalName,StringqName,
Attributesattributes)throwsSAXException{
if(qName.equals("
书"
)){
books=newBooks();
books.setBooktype(Integer.parseInt(attributes.getValue(0)));
this.tagName=qName;
publicvoidendElement(Stringuri,StringlocalName,
StringqName)throwsSAXException{
this.list.add(this.books);
this.tagName=null;
publicvoidendDocument()throwsSAXException{
}
publicvoidcharacters(char[]ch,intstart,
intlength)throwsSAXException{
if(this.tagName!
=null){
Stringdate=newString(ch,start,length);
if(this.tagName.equals("
书名"
this.books.setName(date);
this.books.setTypename(date);
}elseif(this.tagName.equals("
图书简介"
this.books.setDescription(date);
图书图片"
this.books.setPicture(date);
ISBN"
this.books.setIsbn(date);
}elseif(this.tagName.equals("
出版时间"
this.books.setTime(date);
出版社"
this.books.setPublisher(date);
作者"
this.books.setAuthor(date);
价格"
this.books.setPrice(date);
publicstaticvoidmain(String[]args){
SAXParserparser=null;
try{
parser=SAXParserFactory.newInstance().newSAXParser();
Books_SaxReadXmlparseXml=newBooks_SaxReadXml();
InputStreamstream=newFileInputStream(newFile("
D:
\\books.xml"
));
parser.parse(stream,parseXml);
List<
list=parseXml.getList();
System.out.println("
书名\t\t\t书号\t\t\t作者\t\t出版社\t\t出版时间"
);
---------------------------------------------------------------------------------------------"
for(Booksbooks:
System.out.println(books.getName()+"
\t"
+
books.getIsbn()+"
books.getAuthor()+"
\t\t"
+books.getPublisher()+"
+books.getTime());
}catch(ParserConfigurationExceptione){
e.printStackTrace();
}catch(SAXExceptione){
}catch(IOExceptione){
2、解析后结果显示
3、生成books.txt
3、使用DOM方式解析
从XML文档books.xml中获取“书名”、“书号”、“作者”、“出版社”、“出版日期”,并将它们封装到一个javabean类中,然后利用该类中的数据,通过DOM构造一棵DOM文档树,并将其序列化到一个XML文件中simplebooks.xml。
packagecom.src.dom.xml;
importjavax.xml.parsers.*;
importorg.w3c.dom.*;
importjava.io.*;
importorg.apache.crimson.tree.XmlDocument;
importorg.w3c.dom.NodeList;
publicclassDom_xml{
privateBookbook=newBook();
Book>
publicBookgetBook(){
returnbook;
publicvoidsetBook(Bookbook){
this.book=book;
publicvoidqueryXml(){
try{
DocumentBuilderFactorydbFactory=DocumentBuilderFactory
.newInstance();
DocumentBuilderdbBuilder=dbFactory.newDocumentBuilder();
Documentdoc=dbBuilder.parse("
System.out.println("
处理该文档的DomImplementation对象="
+doc.getImplementation());
NodeListnList1=doc.getElementsByTagName("
书籍"
//遍历该集合,显示结合中的元素及其子元素的名字
for(inti=0;
i<
nList1.getLength();
i++){
//Elementnode=(Element)nList.item(i);
Elementelement=(Element)nList1.item(i);
StringTypeName=element.getElementsByTagName("
类别"
)
.item(0).getFirstChild().getNodeValue();
System.out.println(TypeName);
book.setTypename(TypeName);
StringDescription=element
.getElementsByTagName("
).item(0)
.getFirstChild().getNodeValue();
book.setDescription(Description);
StringPicture=element.getElementsByTagName("
book.setPicture(Picture);
StringISBN=element.getElementsByTagName("
book.setIsbn(ISBN);
StringName=element.getElementsByTagName("
book.setName(Name);
StringTime=element.getElementsByTagName("
book.setTime(Time);
StringPublisher=element.getElementsByTagName("
book.setPublisher(Publisher);
StringAuthor=element.getElementsByTagName("
book.setAuthor(Author);
StringPrice=element.getElementsByTagName("
book.setPrice(Price);
System.out.println(Description);
list.add(book);
}
}catch(Exceptione){
e.printStackTrace();
}
publicvoidinsertXml(){
Elementbooks=null;
Elementbook1=null;
Elementname=null;
Elementauthor=null;
Elementisbn=null;
Elementpublisher=null;
Elementtime=null;
try{
NodeListnList=doc.getElementsByTagName("
books=(Element)nList.item(0);
book1=doc.createElement("
book1.setAttribute("
id"
"
name=doc.createElement("
name.appendChild(doc.createTextNode(book.getName()));
book1.appendChild(name);
/**
*下面的元素依次加入即可
**/
author=doc.createElement("
author.appendChild(doc.createTextNode(book.getAuthor()));
book1.appendChild(author);
isbn=doc.createElement("
书号"
isbn.appendChild(doc.createTextNode(book.getIsbn()));
book1.appendChild(isbn);
time=doc.createElement("
time.appendChild(doc.createTextNode(book.getTime()));
book1.appendChild(time);
publisher=doc.createElement("
publisher.appendChild(doc.createTextNode(book.getPublisher()));
book1.appendChild(publisher);
books.appendChild(book1);
((XmlDocument)doc).write(newFileOutputStream("
\\1.xml"
success"
/**
*创建xml文档
**/
publicvoidcreateDom(){
Documentdoc;
doc=dbBuilder.newDocument();
if(doc!
=null){
books=doc.createElement("
book1=doc.createElement("
book1.setAttribute("
name=doc.createElement("
name.appendChild(doc.createTextNode(book.getName()));
book1.appendChild(name);
author=doc.createElement("
author.appendChild(doc.createTextNode(book.getAuthor()));
book1.appendChild(author);
isbn=doc.createElement("
isbn.appendChild(doc.createTextNode(book.getIsbn()));
book1.appendChild(isbn);
time=doc.createElement("
time.appendChild(doc.createTextNode(book.getTime()));
book1.appendChild(time);
publisher=doc.createElement("
publisher.appendChild(doc.createTextNode(book.getPublisher()));
book1.appendChild(publisher);
books.appendChild(book1);
doc.appendChild(books);
((XmlDocument)doc).write(newFileOutputStream(
"
\\simplebooks.xml"
System.out.println("
}ca