java中的xmlWord文档格式.docx

上传人:b****2 文档编号:1090042 上传时间:2023-04-30 格式:DOCX 页数:8 大小:19.66KB
下载 相关 举报
java中的xmlWord文档格式.docx_第1页
第1页 / 共8页
java中的xmlWord文档格式.docx_第2页
第2页 / 共8页
java中的xmlWord文档格式.docx_第3页
第3页 / 共8页
java中的xmlWord文档格式.docx_第4页
第4页 / 共8页
java中的xmlWord文档格式.docx_第5页
第5页 / 共8页
java中的xmlWord文档格式.docx_第6页
第6页 / 共8页
java中的xmlWord文档格式.docx_第7页
第7页 / 共8页
java中的xmlWord文档格式.docx_第8页
第8页 / 共8页
亲,该文档总共8页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

java中的xmlWord文档格式.docx

《java中的xmlWord文档格式.docx》由会员分享,可在线阅读,更多相关《java中的xmlWord文档格式.docx(8页珍藏版)》请在冰点文库上搜索。

java中的xmlWord文档格式.docx

?

xmlversion="

1.0"

encoding="

ISO-8859-1"

>

第一行是XML声明。

它定义XML的版本(1.0)和所使用的编码(ISO-8859-1=Latin-1/西欧字符集)。

下一行描述文档的根元素(像在说:

“本文档是一个便签”):

接下来4行描述根的4个子元素(to,from,heading以及body):

最后一行定义根元素的结尾:

从本例可以设想,该XML文档包含了John给George的一张便签。

XML文档形成一种树结构

XML文档必须包含根元素。

该元素是所有其他元素的父元素。

XML文档中的元素形成了一棵文档树。

这棵树从根部开始,并扩展到树的最底端。

所有元素均可拥有子元素:

root>

<

child>

subchild>

.....<

/subchild>

/child>

/root>

父、子以及同胞等术语用于描述元素之间的关系。

父元素拥有子元素。

相同层级上的子元素成为同胞(兄弟或姐妹)。

XML属性

属性通常提供不属于数据组成部分的信息。

在下面的例子中,文件类型与数据无关,但是对需要处理这个元素的软件来说却很重要:

filetype="

gif"

computer.gif<

/file>

 

实例

上图表示下面的XML中的一本书:

bookstore>

bookcategory="

COOKING"

titlelang="

en"

EverydayItalian<

/title>

author>

GiadaDeLaurentiis<

/author>

year>

2005<

/year>

price>

30.00<

/price>

/book>

CHILDREN"

HarryPotter<

JK.Rowling<

29.99<

WEB"

LearningXML<

ErikT.Ray<

2003<

39.95<

/bookstore>

例子中的根元素是<

文档中的所有<

book>

元素都被包含在<

中。

元素有4个子元素:

title>

、<

author>

3.Xml的特点。

XML的属性值须加引号

与HTML类似,XML也可拥有属性(名称/值的对)。

在XML中,XML的属性值须加引号。

请研究下面的两个XML文档。

第一个是错误的,第二个是正确的:

notedate=08/08/2008>

notedate="

08/08/2008"

在第一个文档中的错误是,note元素中的date属性没有加引号。

XML文档必须有根元素

XML文档必须有一个元素是所有其他元素的父元素。

该元素称为根元素。

XML标签对大小写敏感

XML元素使用XML标签进行定义。

XML标签对大小写敏感。

在XML中,标签<

Letter>

与标签<

letter>

是不同的。

必须使用相同的大小写来编写打开标签和关闭标签:

Message>

这是错误的。

/message>

message>

这是正确的。

XML中的注释

在XML中编写注释的语法与HTML的语法很相似:

!

--Thisisacomment-->

XML元素vs.属性

请看这些例子:

personsex="

female"

firstname>

Anna<

/firstname>

lastname>

Smith<

/lastname>

/person>

person>

sex>

female<

/sex>

在第一个例子中,sex是一个属性。

在第二个例子中,sex则是一个子元素。

两个例子均可提供相同的信息。

4.如何解析xml文件

UTF-8"

employees>

employee>

name>

ddviplinux<

/name>

m<

age>

30<

/age>

/employee>

/employees>

packagecom.alisoft.facepay.framework.bean;

importjava.io.FileInputStream;

importjava.io.FileNotFoundException;

importjava.io.FileOutputStream;

importjava.io.IOException;

importjava.io.InputStream;

importjava.io.PrintWriter;

importjavax.xml.parsers.DocumentBuilder;

importjavax.xml.parsers.DocumentBuilderFactory;

importjavax.xml.parsers.ParserConfigurationException;

importjavax.xml.transform.OutputKeys;

importjavax.xml.transform.Transformer;

importjavax.xml.transform.TransformerConfigurationException;

importjavax.xml.transform.TransformerException;

importjavax.xml.transform.TransformerFactory;

importjavax.xml.transform.dom.DOMSource;

importjavax.xml.transform.stream.StreamResult;

importorg.w3c.dom.Document;

importorg.w3c.dom.Element;

importorg.w3c.dom.Node;

importorg.w3c.dom.NodeList;

importorg.xml.sax.SAXException;

/**

*

*@authorhongliang.dinghl

*DOM生成与解析XML文档

*/

publicclassDomDemoimplementsXmlDocument{

privateDocumentdocument;

privateStringfileName;

publicvoidinit(){

try{

DocumentBuilderFactoryfactory=DocumentBuilderFactory

.newInstance();

DocumentBuilderbuilder=factory.newDocumentBuilder();

this.document=builder.newDocument();

}catch(ParserConfigurationExceptione){

System.out.println(e.getMessage());

}

publicvoidcreateXml(StringfileName){

Elementroot=this.document.createElement("

employees"

);

this.document.appendChild(root);

Elementemployee=this.document.createElement("

employee"

Elementname=this.document.createElement("

name"

name.appendChild(this.document.createTextNode("

丁宏亮"

));

employee.appendChild(name);

Elementsex=this.document.createElement("

sex"

sex.appendChild(this.document.createTextNode("

m"

employee.appendChild(sex);

Elementage=this.document.createElement("

age"

age.appendChild(this.document.createTextNode("

30"

employee.appendChild(age);

root.appendChild(employee);

TransformerFactorytf=TransformerFactory.newInstance();

Transformertransformer=tf.newTransformer();

DOMSourcesource=newDOMSource(document);

transformer.setOutputProperty(OutputKeys.ENCODING,"

gb2312"

transformer.setOutputProperty(OutputKeys.INDENT,"

yes"

PrintWriterpw=newPrintWriter(newFileOutputStream(fileName));

StreamResultresult=newStreamResult(pw);

transformer.transform(source,result);

System.out.println("

生成XML文件成功!

"

}catch(TransformerConfigurationExceptione){

}catch(IllegalArgumentExceptione){

}catch(FileNotFoundExceptione){

}catch(TransformerExceptione){

publicvoidparserXml(StringfileName){//解析文档

DocumentBuilderFactorydbf=DocumentBuilderFactory.newInstance();

DocumentBuilderdb=dbf.newDocumentBuilder();

Documentdocument=db.parse(fileName);

NodeListemployees=document.getChildNodes();

for(inti=0;

i<

employees.getLength();

i++){

Nodeemployee=employees.item(i);

NodeListemployeeInfo=employee.getChildNodes();

for(intj=0;

j<

employeeInfo.getLength();

j++){

Nodenode=employeeInfo.item(j);

NodeListemployeeMeta=node.getChildNodes();

for(intk=0;

k<

employeeMeta.getLength();

k++){

System.out.println(employeeMeta.item(k).getNodeName()

+"

:

+employeeMeta.item(k).getTextContent());

解析完毕"

}catch(SAXExceptione){

}catch(IOExceptione){

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > PPT模板 > 动物植物

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2