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