XML基础教程2版第6章的代码Word文档格式.docx

上传人:b****5 文档编号:18344287 上传时间:2022-12-15 格式:DOCX 页数:16 大小:18.90KB
下载 相关 举报
XML基础教程2版第6章的代码Word文档格式.docx_第1页
第1页 / 共16页
XML基础教程2版第6章的代码Word文档格式.docx_第2页
第2页 / 共16页
XML基础教程2版第6章的代码Word文档格式.docx_第3页
第3页 / 共16页
XML基础教程2版第6章的代码Word文档格式.docx_第4页
第4页 / 共16页
XML基础教程2版第6章的代码Word文档格式.docx_第5页
第5页 / 共16页
点击查看更多>>
下载资源
资源描述

XML基础教程2版第6章的代码Word文档格式.docx

《XML基础教程2版第6章的代码Word文档格式.docx》由会员分享,可在线阅读,更多相关《XML基础教程2版第6章的代码Word文档格式.docx(16页珍藏版)》请在冰豆网上搜索。

XML基础教程2版第6章的代码Word文档格式.docx

importorg.xml.sax.*;

importorg.w3c.dom.*;

publicclassXPathTwo{

publicstaticvoidmain(Stringargs[]){

try{XPathFactoryxPathFactory=XPathFactory.newInstance();

XPathxPath=xPathFactory.newXPath();

InputSourcesource=newInputSource("

example6_1.xml"

);

Stringpath="

/学生列表/学生[成绩>

60]/姓名"

;

NodeListnodelist=

(NodeList)xPath.evaluate(path,source,XPathConstants.NODESET);

intsize=nodelist.getLength();

for(intk=0;

k<

size;

k++){

Nodenode=nodelist.item(k);

Stringname=node.getNodeName();

Stringcontent=node.getTextContent();

System.out.print(name);

System.out.println("

:

"

+content);

}

catch(Exceptionexp){

System.out.println(exp);

}

6.2Node节点

employee.xml

xml-stylesheethref="

show.css"

type="

text/css"

雇员列表>

李四

民族>

傣族<

/民族>

/雇员列表>

6.3XPath路径表达式的结构

例子3

example6_3.xml

studentList>

studentxmlns:

p1="

Liaoning"

sex="

男"

number>

2010111

inputTime>

1992-01-01<

/inputTime>

/number>

name>

张三<

/name>

birthDay>

/birthDay>

score>

611<

/score>

/student>

studentsex="

女"

2010222

李翠花<

1992-02-02<

522<

2010333

孙五<

1992-03-03<

433<

/studentList>

例子4

XPathFour.java

importjava.io.*;

importjava.util.*;

publicclassXPathFour{

Scannerreader=newScanner(System.in);

StringfileName="

example6_3.xml"

InputSourcesource=newInputSource(fileName);

System.out.print("

输入XPath表达式:

Stringpath=reader.nextLine();

节点集中的节点个数:

+size);

节点的名字依次为:

第"

+(k+1)+"

个节点的名字:

+name);

例子5

example6_5.xml

列车时刻表>

列车类别="

特快"

车厢数目="

20节车厢"

列车号码>

152次<

/列车号码>

始发时间>

09:

12<

/始发时间>

到达时间>

19:

23<

/到达时间>

始发站>

北京<

/始发站>

终到站>

上海<

/终到站>

/列车>

普快"

12节车厢"

168次<

10:

21:

36<

沈阳<

南京<

/列车时刻表>

例子6

Example6_6.java

importjava.awt.*;

importjava.awt.event.*;

importjavax.swing.*;

publicclassEample6_6{

XPathWindowwin=newXPathWindow("

a.xml"

classXPathWindowextendsJFrameimplementsActionListener{

XPathFactoryxPathFactory;

XPathxPath;

InputSourcesource;

JTextFieldinputXPath;

JTextAreashowResult;

JButtonbutton;

publicXPathWindow(StringfileName){

try{xPathFactory=XPathFactory.newInstance();

xPath=xPathFactory.newXPath();

source=newInputSource(fileName);

inputXPath=newJTextField(25);

showResult=newJTextArea();

button=newJButton("

确定"

button.addActionListener(this);

inputXPath.addActionListener(this);

JPanelnorth=newJPanel();

north.add(newLabel("

XPath表达式:

));

north.add(inputXPath);

north.add(button);

add(north,BorderLayout.NORTH);

add(newJScrollPane(showResult),BorderLayout.CENTER);

setBounds(10,10,900,300);

setVisible(true);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

publicvoidactionPerformed(ActionEvente){

showResult.setText(null);

Stringpath=inputXPath.getText();

try{

showResult.append("

+size+"

\n"

节点的名字以及节点的值依次为:

+name+"

"

Stringvalue=node.getNodeValue();

个节点的值:

+value+"

异常:

+exp);

}

6.7JavaXPathAPI

例子7

XPathSeven.java

publicclassXPathSeven{

example6_5.xml"

/列车时刻表/列车"

Nodenode=(Node)xPath.evaluate(path,source,XPathConstants.NODE);

例子8

XPathEight.java

publicclassXPathEight{

/列车时刻表/列车/始发站"

StringstateName=(String)xPath.evaluate(path,source,XPathConstants.STRING);

System.out.println("

始发站标记包含的文本:

System.out.print(stateName);

例子9

example8_9.xml

成绩表>

78<

李四<

80<

/成绩表>

XPathNine.java

publicclassXPathNine{

example8_9.xml"

doublesum=0;

成绩表/学生"

for(inti=1;

i<

=size;

i++){

path="

成绩表/学生["

+i+"

]/成绩"

Doublenumber=(Double)xPath.evaluate(path,source,XPathConstants.NUMBER);

doublescore=number.doubleValue();

sum=sum+score;

doubleaver=sum/size;

学生的平均成绩"

+aver);

6.8节点集与函数

例子10

XPathTen.java

publicclassXPathTen{

StringcountPath="

count(/成绩表/学生/成绩)"

Doublenumber=(Double)xPath.evaluate(countPath,source,XPathConstants.NUMBER);

doublen=number.doubleValue();

StringsumPath="

sum(/成绩表/学生/成绩)"

Doublesum=(Double)xPath.evaluate(sumPath,source,XPathConstants.NUMBER);

doubletotal=sum.doubleValue();

总成绩:

+total);

doubleaver=total/n;

平均成绩:

6.9图书查询

例子11

book.xml

图书列表>

图书ISBN='

72349876'

名称>

美丽的假日<

/名称>

作者>

/作者>

价格>

29<

/价格>

出版时间>

2009.05<

/出版时间>

出版社>

阳光出版社<

/出版社>

/图书>

12345678'

冬天的阳光<

18<

2010.05<

冬冬出版社<

87654321'

春天的记忆<

张小民<

25<

2010.10<

春日出版社<

/图书列表>

Application.java

publicclassApplication{

newBookConditionWindow();

BookConditionWindow.java

publicclassBookConditionWindowextendsJFrame{

JTextFieldinputBookName,inputBookAuthor,inputBookISBN,inputPublish;

BoxbaseBox,boxV1,boxV2;

BookConditionWindow(){

inputBookName=newJTextField(10);

inputBookAuthor=newJTextField(10);

inputBookISBN=newJTextField(10);

inputPublish=newJTextField(10);

boxV1=Box.createVerticalBox();

boxV1.add(newLabel("

图书名称中包含:

作者姓名中包含:

图书ISBN中包含"

出版社名称中包含:

boxV2=Box.createVerticalBox();

boxV2.add(inputBookName);

boxV2.add(inputBookAuthor);

boxV2.add(inputBookISBN);

boxV2.add(inputPublish);

baseBox=Box.createHorizontalBox();

baseBox.add(boxV1);

baseBox.add(boxV2);

JPanelwest=newJPanel();

west.add(baseBox);

west.add(button);

add(west,BorderLayout.WEST);

showResult=newJTextArea(10,10);

showResult.setFont(newFont("

宋体"

Font.PLAIN,12));

FindBookByXPathfindBook;

//负责使用XPath查询图书的对象

findBook=

newFindBookByXPath(inputBookName,inputBookAuthor,inputBookISBN,inputPublish,

showResult,"

book.xml"

button.addActionListener(findBook);

FindBookByXPath.java

import

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

当前位置:首页 > 初中教育 > 科学

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

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