XML基础教程2版第6章的代码.docx

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

XML基础教程2版第6章的代码.docx

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

XML基础教程2版第6章的代码.docx

XML基础教程2版第6章的代码

第6章XPath语言

6.1XPath简介

例子1

example6_1.xml

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

>

<学生列表>

<学生>

<姓名>张三

<性别>男

<成绩>80

<学生>

<姓名>李四

<性别>女

<成绩>50

<学生>

<姓名>孙伍

<性别>男

<成绩>80

例子2

XPathTwo.java

importjavax.xml.xpath.*;

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

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

xmlversion="1.0"?

>

xml-stylesheethref="show.css"type="text/css"?

>

<雇员列表>

<姓名>张三

<姓名>李四

<民族>傣族

6.3XPath路径表达式的结构

例子3

example6_3.xml

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

>

p1="Liaoning"sex="男">

2010111

1992-01-01

张三

1992-01-01

611

2010222

1992-01-01

李翠花

1992-02-02

522

2010333

1992-01-01

孙五

1992-03-03

433

例子4

XPathFour.java

importjavax.xml.xpath.*;

importorg.xml.sax.*;

importorg.w3c.dom.*;

importjava.io.*;

importjava.util.*;

publicclassXPathFour{

publicstaticvoidmain(Stringargs[]){

try{XPathFactoryxPathFactory=XPathFactory.newInstance();

XPathxPath=xPathFactory.newXPath();

Scannerreader=newScanner(System.in);

StringfileName="example6_3.xml";

InputSourcesource=newInputSource(fileName);

System.out.print("输入XPath表达式:

");

Stringpath=reader.nextLine();

NodeListnodelist=

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

intsize=nodelist.getLength();

System.out.println("节点集中的节点个数:

"+size);

System.out.println("节点的名字依次为:

");

for(intk=0;k

Nodenode=nodelist.item(k);

Stringname=node.getNodeName();

System.out.println("第"+(k+1)+"个节点的名字:

"+name);

}

}

catch(Exceptionexp){

System.out.println(exp);

}

}

}

例子5

example6_5.xml

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

>

<列车时刻表>

<列车类别="特快"车厢数目="20节车厢">

<列车号码>152次

<始发时间>09:

12

<到达时间>19:

23

<始发站>北京

<终到站>上海

<列车类别="普快"车厢数目="12节车厢">

<列车号码>168次

<始发时间>10:

12

<到达时间>21:

36

<始发站>沈阳

<终到站>南京

例子6

Example6_6.java

importjavax.xml.xpath.*;

importorg.xml.sax.*;

importorg.w3c.dom.*;

importjava.awt.*;

importjava.awt.event.*;

importjavax.swing.*;

publicclassEample6_6{

publicstaticvoidmain(Stringargs[]){

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);

}

catch(Exceptionexp){

System.out.println(exp);

}

}

publicvoidactionPerformed(ActionEvente){

showResult.setText(null);

Stringpath=inputXPath.getText();

try{

NodeListnodelist=

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

intsize=nodelist.getLength();

showResult.append("节点集中的节点个数:

"+size+"\n");

showResult.append("节点的名字以及节点的值依次为:

\n");

for(intk=0;k

Nodenode=nodelist.item(k);

Stringname=node.getNodeName();

showResult.append("第"+(k+1)+"个节点的名字:

"+name+",");

Stringvalue=node.getNodeValue();

showResult.append("第"+(k+1)+"个节点的值:

"+value+"\n");

}

}

catch(Exceptionexp){

showResult.setText(null);

showResult.append("异常:

"+exp);

}

}

}

6.7JavaXPathAPI

例子7

XPathSeven.java

importjavax.xml.xpath.*;

importorg.xml.sax.*;

importorg.w3c.dom.*;

publicclassXPathSeven{

publicstaticvoidmain(Stringargs[]){

try{XPathFactoryxPathFactory=XPathFactory.newInstance();

XPathxPath=xPathFactory.newXPath();

InputSourcesource=newInputSource("example6_5.xml");

Stringpath="/列车时刻表/列车";

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

Stringname=node.getNodeName();

Stringcontent=node.getTextContent();

System.out.print(name);

System.out.println(":

"+content);

}

catch(Exceptionexp){

System.out.println(exp);

}

}

}

例子8

XPathEight.java

importjavax.xml.xpath.*;

importorg.xml.sax.*;

importorg.w3c.dom.*;

publicclassXPathEight{

publicstaticvoidmain(Stringargs[]){

try{XPathFactoryxPathFactory=XPathFactory.newInstance();

XPathxPath=xPathFactory.newXPath();

InputSourcesource=newInputSource("example6_5.xml");

Stringpath="/列车时刻表/列车/始发站";

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

System.out.println("始发站标记包含的文本:

");

System.out.print(stateName);

}

catch(Exceptionexp){

System.out.println(exp);

}

}

}

例子9

example8_9.xml

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

>

<成绩表>

<学生>

<姓名>张三

<成绩>78

<学生>

<姓名>李四

<成绩>80

XPathNine.java

importjavax.xml.xpath.*;

importjavax.xml.xpath.*;

importorg.xml.sax.*;

importorg.w3c.dom.*;

publicclassXPathNine{

publicstaticvoidmain(Stringargs[]){

try{XPathFactoryxPathFactory=XPathFactory.newInstance();

XPathxPath=xPathFactory.newXPath();

InputSourcesource=newInputSource("example8_9.xml");

doublesum=0;

Stringpath="成绩表/学生";

NodeListnodelist=

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

intsize=nodelist.getLength();

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;

System.out.println("学生的平均成绩"+aver);

}

catch(Exceptionexp){

System.out.println(exp);

}

}

}

6.8节点集与函数

例子10

XPathTen.java

importjavax.xml.xpath.*;

importorg.xml.sax.*;

importorg.w3c.dom.*;

publicclassXPathTen{

publicstaticvoidmain(Stringargs[]){

try{XPathFactoryxPathFactory=XPathFactory.newInstance();

XPathxPath=xPathFactory.newXPath();

InputSourcesource=newInputSource("example8_9.xml");

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();

System.out.println("总成绩:

"+total);

doubleaver=total/n;

System.out.println("平均成绩:

"+aver);

}

catch(Exceptionexp){

System.out.println(exp);

}

}

}

6.9图书查询

例子11

book.xml

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

>

<图书列表>

<图书ISBN='72349876'>

<名称>美丽的假日

<作者>张三

<价格>29

<出版时间>2009.05

<出版社>阳光出版社

<图书ISBN='12345678'>

<名称>冬天的阳光

<作者>李四

<价格>18

<出版时间>2010.05

<出版社>冬冬出版社

<图书ISBN='87654321'>

<名称>春天的记忆

<作者>张小民

<价格>25

<出版时间>2010.10

<出版社>春日出版社

Application.java

publicclassApplication{

publicstaticvoidmain(Stringargs[]){

newBookConditionWindow();

}

}

BookConditionWindow.java

importjava.awt.*;

importjavax.swing.*;

publicclassBookConditionWindowextendsJFrame{

JTextFieldinputBookName,inputBookAuthor,inputBookISBN,inputPublish;

JTextAreashowResult;

JButtonbutton;

BoxbaseBox,boxV1,boxV2;

BookConditionWindow(){

inputBookName=newJTextField(10);

inputBookAuthor=newJTextField(10);

inputBookISBN=newJTextField(10);

inputPublish=newJTextField(10);

boxV1=Box.createVerticalBox();

boxV1.add(newLabel("图书名称中包含:

"));

boxV1.add(newLabel("作者姓名中包含:

"));

boxV1.add(newLabel("图书ISBN中包含"));

boxV1.add(newLabel("出版社名称中包含:

"));

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);

button=newJButton("确定");

west.add(button);

add(west,BorderLayout.WEST);

showResult=newJTextArea(10,10);

showResult.setFont(newFont("宋体",Font.PLAIN,12));

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

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

findBook=

newFindBookByXPath(inputBookName,inputBookAuthor,inputBookISBN,inputPublish,

showResult,"book.xml");

button.addActionListener(findBook);

setBounds(10,10,900,300);

setVisible(true);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}

FindBookByXPath.java

importjavax.xml.xpath.*;

importorg.w3c.dom.*;

import

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

当前位置:首页 > 高等教育 > 院校资料

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

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