XML基础教程2版第6章的代码.docx
- 文档编号:5362445
- 上传时间:2022-12-15
- 格式:DOCX
- 页数:16
- 大小:18.90KB
XML基础教程2版第6章的代码.docx
《XML基础教程2版第6章的代码.docx》由会员分享,可在线阅读,更多相关《XML基础教程2版第6章的代码.docx(16页珍藏版)》请在冰豆网上搜索。
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="男"> 例子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
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- XML 基础教程 代码