XML基础教程2版第第4章DOM解析器代码.docx

上传人:b****5 文档编号:7282419 上传时间:2023-01-22 格式:DOCX 页数:19 大小:19.04KB
下载 相关 举报
XML基础教程2版第第4章DOM解析器代码.docx_第1页
第1页 / 共19页
XML基础教程2版第第4章DOM解析器代码.docx_第2页
第2页 / 共19页
XML基础教程2版第第4章DOM解析器代码.docx_第3页
第3页 / 共19页
XML基础教程2版第第4章DOM解析器代码.docx_第4页
第4页 / 共19页
XML基础教程2版第第4章DOM解析器代码.docx_第5页
第5页 / 共19页
点击查看更多>>
下载资源
资源描述

XML基础教程2版第第4章DOM解析器代码.docx

《XML基础教程2版第第4章DOM解析器代码.docx》由会员分享,可在线阅读,更多相关《XML基础教程2版第第4章DOM解析器代码.docx(19页珍藏版)》请在冰豆网上搜索。

XML基础教程2版第第4章DOM解析器代码.docx

XML基础教程2版第第4章DOM解析器代码

第4章DOM解析器

4.1DOM解析器

例子1

example4_1.xml

xmlversion="1.0"encoding="UTF-8"?

>

<学生名单>

<姓名>张三

<姓名>

李四

<奖励>一等奖学金

JAXPOne.java

importorg.w3c.dom.*;

importjavax.xml.parsers.*;

importjava.io.*;

publicclassJAXPOne{

publicstaticvoidmain(Stringargs[]){

try{DocumentBuilderFactoryfactory=

DocumentBuilderFactory.newInstance();

DocumentBuilderdomPaser=factory.newDocumentBuilder();

Documentdocument=domPaser.parse(newFile("example4_1.xml"));

Elementroot=document.getDocumentElement();

StringrootName=root.getNodeName();

System.out.println("XML文件根节点的名字:

"+rootName);

NodeListnodelist=root.getElementsByTagName("姓名");

intsize=nodelist.getLength();

for(intk=0;k

Nodenode=nodelist.item(k);

Stringname=node.getNodeName();

Stringcontent=node.getTextContent();

System.out.print(name);

System.out.println(":

"+content);

}

}

catch(Exceptione){

System.out.println(e);

}

}

}

4.2节点的类型

例子2

example4_2.xml

xmlversion="1.0"encoding="UTF-8"?

>

<雇员列表>

<雇员>

<姓名>张三

<年龄>25岁

<工资>3190元/月

<雇员>

<姓名>李四

<年龄>35岁

<工资>4320元/月

JAXPTwo.java

importorg.w3c.dom.*;

importjavax.xml.parsers.*;

importjava.io.*;

publicclassJAXPTwo{

publicstaticvoidmain(Stringargs[]){

try{DocumentBuilderFactoryfactory=

DocumentBuilderFactory.newInstance();

DocumentBuilderdomParser=factory.newDocumentBuilder();

Documentdocument=domParser.parse(newFile("example6_2.xml"));

NodeListnodeList=document.getChildNodes();

output(nodeList);

}

catch(Exceptione){

System.out.println(e);

}

}

publicstaticvoidoutput(NodeListnodeList){//output是一个递归方法

intsize=nodeList.getLength();

for(intk=0;k

Nodenode=nodeList.item(k);

if(node.getNodeType()==Node.TEXT_NODE){

TexttextNode=(Text)node;

Stringcontent=textNode.getWholeText();

System.out.print(content);

}

if(node.getNodeType()==Node.ELEMENT_NODE){

ElementelementNode=(Element)node;

Stringname=elementNode.getNodeName();

System.out.print(name+":

");

NodeListnodes=elementNode.getChildNodes();

output(nodes);//递归调用

}

}

}

}

4.4Element节点

例子3

example4_3.xml

xmlversion="1.0"encoding="UTF-8"?

>

<商品列表>

<商品名称分类="家电">

电视机

<商品名称分类="服装">

雅格尔西装

<商品名称分类="食品">

东北大米

JAXPThree.java

importorg.w3c.dom.*;

importjavax.xml.parsers.*;

importjava.io.*;

publicclassJAXPThree{

publicstaticvoidmain(Stringargs[]){

try{DocumentBuilderFactoryfactory=

DocumentBuilderFactory.newInstance();

DocumentBuilderdomPaser=factory.newDocumentBuilder();

Documentdocument=domPaser.parse(newFile("example6_3.xml"));

Elementroot=document.getDocumentElement();

NodeListnodeList=root.getChildNodes();

intsize=nodeList.getLength();

for(intk=0;k

Nodenode=nodeList.item(k);

if(node.getNodeType()==Node.ELEMENT_NODE){

ElementelementNode=(Element)node;

Stringname=elementNode.getNodeName();

Stringid=elementNode.getAttribute("分类");

Stringcontent=elementNode.getTextContent();

System.out.print(name);

System.out.print("("+id+")");

System.out.println(":

"+content);

}

}

}

catch(Exceptione){}

}

}

4.5Text节点

例子4

example4_4.xml

xmlversion="1.0"encoding="UTF-8"?

>

<同学录>

<姓名>张大山

<居住城市>大连市

<联系电话>0411-123456

<姓名>刘翠花

<居住城市>北京市

<联系电话>010-654321

JAXPFour.java

importorg.w3c.dom.*;

importjavax.xml.parsers.*;

importjava.io.*;

publicclassJAXPFour{

publicstaticvoidmain(Stringargs[]){

GiveDatagive=newGiveData();

try{DocumentBuilderFactoryfactory=

DocumentBuilderFactory.newInstance();

DocumentBuilderdomPaser=factory.newDocumentBuilder();

Documentdocument=domPaser.parse(newFile("example4_4.xml"));

Elementroot=document.getDocumentElement();

NodeListnodeList=root.getChildNodes();

give.output(nodeList);

System.out.println("一共有"+give.m+"个Text节点");

}

catch(Exceptione){}

}

}

classGiveData{

intm=0;

publicvoidoutput(NodeListnodeList){//这是一个递归方法

intsize=nodeList.getLength();

for(intk=0;k

Nodenode=nodeList.item(k);

if(node.getNodeType()==Node.TEXT_NODE){

TexttextNode=(Text)node;

Stringcontent=textNode.getWholeText();

m++;

System.out.print(content);

}

if(node.getNodeType()==Node.ELEMENT_NODE){

ElementelementNode=(Element)node;

Stringname=elementNode.getNodeName();

System.out.print(name+":

");

NodeListnodes=elementNode.getChildNodes();

output(nodes);

}

}

}

}

例子5

example4_5.xml

xmlversion="1.0"encoding="UTF-8"?

>

<电视机列表>

<名称>海尔电视机

<价格单位='元/台'>5238

<名称>星海电视机

<价格单位='元/台'>3660

<名称>长虹电视机

<价格单位='元/台'>5285

JAXPFive.java

importorg.w3c.dom.*;

importjavax.xml.parsers.*;

importjava.io.*;

publicclassJAXPFive{

publicstaticvoidmain(Stringargs[]){

GiveDatagive=newGiveData();

try{DocumentBuilderFactoryfactory=

DocumentBuilderFactory.newInstance();

DocumentBuilderdomPaser=factory.newDocumentBuilder();

Documentdocument=domPaser.parse(newFile("example4_5.xml"));

NodeListnodeList=document.getChildNodes();

give.output(nodeList);

System.out.printf("平均价格:

%5.2f%s",give.average/give.m,give.mess);

}

catch(Exceptione){

System.out.println(e);

}

}

}

classGiveData{

doubleaverage=0,m=0;

Stringmess;

publicvoidoutput(NodeListnodeList){

intsize=nodeList.getLength();

for(intk=0;k

Nodenode=nodeList.item(k);

if(node.getNodeType()==Node.TEXT_NODE){

TexttextNode=(Text)node;

Stringcontent=textNode.getWholeText();

System.out.print(content);

Elementparent=(Element)textNode.getParentNode();

booleanboo=(parent.getNodeName()).equals("价格");

if(boo==true){

content=textNode.getWholeText();

average=average+Double.parseDouble(content.trim());

m++;

}

}

if(node.getNodeType()==Node.ELEMENT_NODE){

ElementelementNode=(Element)node;

Stringname=elementNode.getNodeName();

Stringid=elementNode.getAttribute("单位");

if(id.length()>0){

System.out.print(name+"("+id+"):

");

mess=id;

}

NodeListnodes=elementNode.getChildNodes();

output(nodes);

}

}

}

}

4.6Attr节点

例子6

Example4_6.xml

xmlversion="1.0"encoding="UTF-8"?

>

<应聘者列表>

<姓名学历="研究生">张三

<毕业学校类型="工科">清华大学

<专业>土木工程

<姓名学历="本科">张三

<毕业学校类型="理科">北京大学

<专业>计算数学

JAXPSix.java

importorg.w3c.dom.*;

importjavax.xml.parsers.*;

importjava.io.*;

publicclassJAXPSix{

publicstaticvoidmain(Stringargs[]){

GiveDatagive=newGiveData();

try{DocumentBuilderFactoryfactory=

DocumentBuilderFactory.newInstance();

DocumentBuilderdomPaser=factory.newDocumentBuilder();

Documentdocument=domPaser.parse(newFile("example4_6.xml"));

Elementroot=document.getDocumentElement();

NodeListnodeList=root.getChildNodes();

give.output(nodeList);

}

catch(Exceptione){

System.out.println(e);

}

}

}

classGiveData{

publicvoidoutput(NodeListnodeList){

intsize=nodeList.getLength();

for(intk=0;k

Nodenode=nodeList.item(k);

if(node.getNodeType()==Node.TEXT_NODE){

TexttextNode=(Text)node;

Stringcontent=textNode.getWholeText();

System.out.print(content);

}

if(node.getNodeType()==Node.ELEMENT_NODE){

ElementelementNode=(Element)node;

Stringname=elementNode.getNodeName();

System.out.print(name+":

");

NamedNodeMapmap=elementNode.getAttributes();

for(intm=0;m

AttrattrNode=(Attr)map.item(m);

StringattName=attrNode.getName();

StringattValue=attrNode.getValue();

System.out.print("("+attName+":

"+attValue+")");

}

NodeListnodes=elementNode.getChildNodes();

output(nodes);

}

}

}

}

4.7DocumentType节点

例子7

time.dtd

ELEMENT北京站始发列车时刻表(客车*)>

ELEMENT客车(车次,开车时间)>

ELEMENT车次(#PCDATA)>

ELEMENT开车时间(#PCDATA)>

ATTLIST客车类别CDATA#REQUIRED>

example4_7.xml

xmlversion="1.0"encoding="UTF-8"?

>

DOCTYPE北京站始发列车时刻表PUBLIC"-//ISO985//China//ForXML/Ch""time.dtd">

<北京站始发列车时刻表>

<客车类别="特快">

<车次>T259

<开车时间>18:

38

<客车类别="普快">

<车次>K1257

<开车时间>23:

12

JAXPSeven.java

importorg.w3c.dom.*;

importjavax.xml.parsers.*;

importjava.io.*;

publicclassJAXPSeven{

publicstaticvoidmain(Stringargs[]){

try{DocumentBuilderFactoryfactory=DocumentBuilderFactory.newInstance();

DocumentBuilderdomParser=factory.newDocumentBuilder();

Documentdocument=domParser.parse(newFile("example4_7.xml"));

DocumentTypedoctype=document.getDoctype();

StringDTDName=doctype.getName();

System.out.println("DTD名字:

"+DTDName);

StringpublicId=doctype.getPublicId();

System.out.println("public标识:

"+publicId);

StringsystemId=doctype.getSystemId();

System.out.println("system标识:

"+systemId);

StringinternalDTD=doctype.getInternalSubset();

System.out.println("内部DTD:

"+internalDTD);

}

catch(Exceptione){}

}

}

4.8处理空白

例子8

bookList.dtd

xmlversion="1.0"encoding="UTF-8"?

>

DOCTYPE图书列表SYSTEM"bookList.dtd">

<图书列表>

<书名>Java程序设计

<出版社>清华大学出版社

<书名>高等数学

<出版社>高等教育出版社

example4_8.xml

xmlversion="1.0"encoding="UTF-8"?

>

DOCTYPE图书列表SYSTEM"bookList.dtd">

<图书列表>

<书名>Java程序设计

<出版社>清华大学出版社

<书名>高等数学

<出版社>高等教育出版社

JAXPEight.java

importorg.w3c.dom.*;

importjavax.xml.parsers.*;

importjava.io.*;

publicclassJAXPEight{

publicstaticvoidmain(Stringargs[]){

GiveDatagive=newGiveData();

try{DocumentBuilderFactoryfactory=DocumentBuilderFactory.newInstance();

factory.setIgnoringElementContentWhitespace(true);//忽略缩进

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

当前位置:首页 > 农林牧渔 > 林学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1