After The End

2007年12月11日 #

Java的xpath示例程序

代码示例来源于:http://www.ibm.com/developerworks/xml/library/x-javaxpathapi.html
books.xml
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <inventory>
 3     <book year="2000">
 4         <title>Snow Crash</title>
 5         <author>Neal Stephenson</author>
 6         <publisher>Spectra</publisher>
 7         <isbn>0553380958</isbn>
 8         <price>14.95</price>
 9     </book>
10  
11     <book year="2005">
12         <title>Burning Tower</title>
13         <author>Larry Niven</author>
14         <author>Jerry Pournelle</author>
15         <publisher>Pocket</publisher>
16         <isbn>0743416910</isbn>
17         <price>5.99</price>
18     </book>
19  
20     <book year="1995">
21         <title>Zodiac</title>
22         <author>Neal Stephenson</author>
23         <publisher>Spectra</publisher>
24         <isbn>0553573862</isbn>
25         <price>7.50</price>
26     </book>
27 
28     <!-- more books -->
29  
30 </inventory>

 1 package com.zhanjh.xpath;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.xml.parsers.*;
 6 import javax.xml.xpath.*;
 7 
 8 import org.w3c.dom.*;
 9 import org.xml.sax.SAXException;
10 
11 class XpathSample {
12     public static void main(String[] args) throws ParserConfigurationException,
13             SAXException, IOException, XPathExpressionException {
14         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
15         factory.setNamespaceAware(true);
16         DocumentBuilder builder = factory.newDocumentBuilder();
17         Document doc = builder.parse("XmlFile/books.xml");
18         XPathFactory xFactory = XPathFactory.newInstance();
19         XPath xpath = xFactory.newXPath();
20         XPathExpression expr = xpath
21                 .compile("//book[author='Neal Stephenson']/price/text()");
22         Object result = expr.evaluate(doc, XPathConstants.NODESET);
23         NodeList nodes = (NodeList) result;
24         for (int i = 0; i < nodes.getLength(); i++) {
25             System.out.println(nodes.item(i).getNodeValue());
26         }
27     }
28 
29 }
30 
文件结构:
    XpahtSample/
       src/
          com.zhanjh.xpath
    XmlFile/
       books.xml


posted @ 2007-12-11 16:06 zhanjh 阅读(2871) 评论(1) 推荐(0)

2007年11月30日 #

DirectoryInfo:文件搜索;XmlSerializer:xml串行化读入和存储;IComparer:Array排序

摘要: 一、文件搜索: 1 DirectoryInfodi = new DirectoryInfo(dataPath); 2 FileInfo[]xmlFiles = di.GetFiles( " *.xml " ); 二、xml串行化 1、读入 [XmlRoot( " MapItems " ,... 阅读全文

posted @ 2007-11-30 17:52 zhanjh 阅读(525) 评论(1) 推荐(0)

2007年7月13日 #

窗体控件的多线程操作

摘要: 下面代码不多,但是是我上个月花了很长时间才写出来,不现在回过头再研究研究,其实也不难。在其他的线程里,不能直接操作主窗体上的控件,必须通过IAsyncResult接口来调用(invoke)主窗口上的方法,来操作窗口上的控件。 public System.Threading.ThreadT_Hcp... 阅读全文

posted @ 2007-07-13 10:44 zhanjh 阅读(291) 评论(0) 推荐(0)

2007年7月2日 #

控件操作小结

摘要: 1.控件的拖动:以System.Windows.Forms.Panel控件为例:声明变量: Point mouse_offset; MouseDown事件: 1 private void panel1_MouseDown( object sender,MouseEventArgse) 2 { ... 阅读全文

posted @ 2007-07-02 12:17 zhanjh 阅读(341) 评论(0) 推荐(0)

2007年5月18日 #

通过Dataset操作xml数据(添加,删除,修改)

摘要: 1)cameralist.xml文件的主要内容: No.1 street No.2 street 篇幅原因只设置了一个站点。 2)cameralist.xsd文件内容: 用... 阅读全文

posted @ 2007-05-18 20:04 zhanjh 阅读(1071) 评论(0) 推荐(0)

导航

统计信息

点击右上角即可分享
微信分享提示