XML课程报告.docx
- 文档编号:4595192
- 上传时间:2022-12-07
- 格式:DOCX
- 页数:14
- 大小:79.60KB
XML课程报告.docx
《XML课程报告.docx》由会员分享,可在线阅读,更多相关《XML课程报告.docx(14页珍藏版)》请在冰豆网上搜索。
XML课程报告
《XML语言》
结
业
报
告
学号:
____________________
专业:
____________________
姓名:
____________________
任课教师:
________________
日期:
____________________
1、定义XML文件
所有数据存放在XML文件books.xml中,同时要求对XML文档必须有严格的DTD定义。
XML文档数据可参考以下内容(也可自已定义,但数据信息下必须包括图片、类别和简介信息)
图书信息:
书名、作者、出版社、ISBN、出版时间、单价、图书图片、类别、图书简介。
1、XML源代码:
xmlversion="1.0"encoding="gb2312"?
>
DOCTYPE书籍[
ELEMENT书籍(书+)>
ELEMENT书(书名,作者,出版社,ISBN,出版时间,单价,图书图片,类别,图书简介)>
ELEMENT书名(#PCDATA)>
ELEMENT作者(#PCDATA)>
ELEMENTISBN(#PCDATA)>
ELEMENT出版社(#PCDATA)>
ELEMENT出版时间(#PCDATA)>
ELEMENT单价(#PCDATA)>
ELEMENT图书图片(#PCDATA)>
ELEMENT类别(#PCDATA)>
ELEMENT图书简介(#PCDATA)>
ATTLIST书
idCDATA#REQUIRED
>
]>
<书籍>
<书id="1">
<书名>XML基础教程与实验指导书名>
<作者>孙晓非作者>
<出版社>清华大学出版社出版社>
<出版时间>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
privateBooksbooks;
privateStringtagName;
publicList
returnlist;
}
publicvoidsetList(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{
if(qName.equals("书")){
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);
}
if(this.tagName.equals("书")){
this.books.setTypename(date);
}elseif(this.tagName.equals("图书简介")){
this.books.setDescription(date);
}elseif(this.tagName.equals("图书图片")){
this.books.setPicture(date);
}elseif(this.tagName.equals("ISBN")){
this.books.setIsbn(date);
}elseif(this.tagName.equals("书名")){
this.books.setName(date);
}elseif(this.tagName.equals("出版时间")){
this.books.setTime(date);
}elseif(this.tagName.equals("出版社")){
this.books.setPublisher(date);
}elseif(this.tagName.equals("作者")){
this.books.setAuthor(date);
}elseif(this.tagName.equals("价格")){
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
System.out.println("书名\t\t\t书号\t\t\t作者\t\t出版社\t\t出版时间");
System.out.println("---------------------------------------------------------------------------------------------");
for(Booksbooks:
list){
System.out.println(books.getName()+"\t"+
books.getIsbn()+"\t"+
books.getAuthor()+"\t\t"+books.getPublisher()+"\t"+books.getTime());
}
}catch(ParserConfigurationExceptione){
e.printStackTrace();
}catch(SAXExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}
}
}
2、解析后结果显示
3、生成books.txt
3、使用DOM方式解析
从XML文档books.xml中获取“书名”、“书号”、“作者”、“出版社”、“出版日期”,并将它们封装到一个javabean类中,然后利用该类中的数据,通过DOM构造一棵DOM文档树,并将其序列化到一个XML文件中simplebooks.xml。
1、程序源代码:
packagecom.src.dom.xml;
importjavax.xml.parsers.*;
importorg.w3c.dom.*;
importjava.io.*;
importjava.util.ArrayList;
importjava.util.List;
importorg.apache.crimson.tree.XmlDocument;
importorg.w3c.dom.NodeList;
publicclassDom_xml{
privateBookbook=newBook();
privateList
publicBookgetBook(){
returnbook;
}
publicvoidsetBook(Bookbook){
this.book=book;
}
publicList
returnlist;
}
publicvoidsetList(List
this.list=list;
}
publicvoidqueryXml(){
try{
DocumentBuilderFactorydbFactory=DocumentBuilderFactory
.newInstance();
DocumentBuilderdbBuilder=dbFactory.newDocumentBuilder();
Documentdoc=dbBuilder.parse("D:
\\books.xml");
System.out.println("处理该文档的DomImplementation对象="
+doc.getImplementation());
NodeListnList1=doc.getElementsByTagName("书籍");
//遍历该集合,显示结合中的元素及其子元素的名字
for(inti=0;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("图书图片") .item(0).getFirstChild().getNodeValue(); book.setPicture(Picture); StringISBN=element.getElementsByTagName("ISBN").item(0) .getFirstChild().getNodeValue(); book.setIsbn(ISBN); StringName=element.getElementsByTagName("书名").item(0) .getFirstChild().getNodeValue(); book.setName(Name); StringTime=element.getElementsByTagName("出版时间").item(0) .getFirstChild().getNodeValue(); book.setTime(Time); StringPublisher=element.getElementsByTagName("出版社") .item(0).getFirstChild().getNodeValue(); book.setPublisher(Publisher); StringAuthor=element.getElementsByTagName("作者").item(0) .getFirstChild().getNodeValue(); book.setAuthor(Author); StringPrice=element.getElementsByTagName("价格").item(0) .getFirstChild().getNodeValue(); 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{ DocumentBuilderFactorydbFactory=DocumentBuilderFactory .newInstance(); DocumentBuilderdbBuilder=dbFactory.newDocumentBuilder(); Documentdoc=dbBuilder.parse("D: \\books.xml"); NodeListnList=doc.getElementsByTagName("书籍"); books=(Element)nList.item(0); book1=doc.createElement("书"); book1.setAttribute("id","1"); 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("D: \\1.xml")); System.out.println("success"); }catch(Exceptione){ e.printStackTrace(); } } /** *创建xml文档 **/ publicvoidcreateDom(){ Documentdoc; Elementbooks=null; Elementbook1=null; Elementname=null; Elementauthor=null; Elementisbn=null; Elementpublisher=null; Elementtime=null; try{ DocumentBuilderFactorydbFactory=DocumentBuilderFactory .newInstance(); DocumentBuilderdbBuilder=dbFactory.newDocumentBuilder(); doc=dbBuilder.newDocument(); if(doc! =null){ books=doc.createElement("书籍"); book1=doc.createElement("书"); book1.setAttribute("id","1"); 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"); 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( "D: \\simplebooks.xml")); System.out.println("success"); } }ca
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- XML 课程 报告