📄 xpathtest.java
字号:
// -------------------------------------------------------------------------
// Copyright (c) 2000-2004 Ufinity. All Rights Reserved.
//
// This software is the confidential and proprietary information of
// Ufinity
//
// Original author:Administrator
//
// -------------------------------------------------------------------------
// UFINITY MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
// THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE, OR NON-INFRINGEMENT. UFINITY SHALL NOT BE
// LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
// MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
//
// THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
// CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
// PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
// NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
// SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
// SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
// PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES"). UFINITY
// SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
// HIGH RISK ACTIVITIES.
// -------------------------------------------------------------------------
package com.ufinity.jdom;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.xpath.XPath;
/**
* @author WenQiang Wu
*
* @time 9:48:33 AM Nov 10, 2008
*/
public class XPathTest {
private static XPathTest xpathTest = null;
//public static final String XPATH_CONFIG = "xpath-config.xml";
public static final String XPATH_CONFIG = "xpath-test.xml";
private Element rootElt;
// single model
public static XPathTest getInstance() {
if (null == xpathTest) {
xpathTest = new XPathTest();
}
return xpathTest;
}
//construct function
private XPathTest() {
readXPathConfig();
initXPathXML();
}
// read xpath config's xml file
public void readXPathConfig() {
SAXBuilder saxBuilder = new SAXBuilder();
try {
// get read file's all element
Document document = saxBuilder.build(Thread.currentThread()
.getContextClassLoader().getResourceAsStream(XPATH_CONFIG));
// get this first element for read file's element
this.rootElt = document.getRootElement();
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* The <cod>initXPathXML()<cod> method is operation xml for read
*/
private void initXPathXML() {
// String xPathString = "//CCC | //BBB";
// String xPathString = "/AAA";
// String xPathString = "/descendant::*";
// String xPathString = "/bookstore/book[last()-1]";
// String xPathString = "/bookstore/book[price>35.00]/title";
// String xPathString = "//book/title | //book/price";
//String xPathString = "//attribute::*";
//String xPathString = "//ancestor-or-self::book";
//String xPathString = "//child::node()";
//String xPathString = "/bookstore/book[position()<2]";
//String xPathString = "//book/* | //student/*";
//String xPathString = "/bookstore/book[price>35]/title[attribute::*]"; //<IE5 0 else 1
//String xPathString = "//*[string-length(name()) = 2]";
String xPathString = "../price";
try {
List<?> lists = XPath.selectNodes(rootElt, xPathString);
System.out.println("size : " + lists.size()
+ "\n-------------------------");
for (Iterator<?> iterator = lists.iterator(); iterator.hasNext();) {
Object object = iterator.next();
if (object instanceof Attribute) {
Attribute attribute = (Attribute) object;
System.out.println(attribute.getName() + " - "
+ attribute.getValue());
} else if (object instanceof Element) {
Element beanElt = (Element) object;
String eltName = beanElt.getName();
String eltText = beanElt.getTextTrim();
System.out.println(eltName + " : " + eltText);
}
}
} catch (JDOMException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
XPathTest.getInstance();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -