package com.tudou.ajax.t1; import java.io.File; import java.io.IOException; import java.util.Iterator; import java.util.List; import org.jdom.Content; import org.jdom.Document; import org.jdom.Element; import org.jdom.JDOMException; import org.jdom.filter.ElementFilter; import org.jdom.input.SAXBuilder; import org.jdom.output.XMLOutputter; import org.jdom.xpath.XPath; public class XpathByJDom { public static void main(String[] args) { XpathByJDom xj = new XpathByJDom(); // xj.readerJDom(); xj.calByXpathExpression(); // xj.calByDescendantElementFilter(); } @SuppressWarnings("unchecked") public void getDescendantsDemo() { try { SAXBuilder builder = new SAXBuilder(); File f = new File("WebRoot/xmls/employee.xml"); Document doc = builder.build(f); System.out.println("All content:"); Iterator itr = doc.getDescendants(); while (itr.hasNext()) { Content c = (Content) itr.next(); System.out.println(c); } System.out.println(); System.out.println("Only elements:"); itr = doc.getDescendants(new ElementFilter()); while (itr.hasNext()) { Content c = (Content) itr.next(); System.out.println(c); } System.out.println(); System.out.println("Everything that's not an element:"); itr = doc.getDescendants(new ElementFilter().negate()); while (itr.hasNext()) { Content c = (Content) itr.next(); System.out.println(c); } System.out.println(); System.out.println("Only elements with localname of employee:"); itr = doc.getDescendants(new ElementFilter("employee")); while (itr.hasNext()) { Content c = (Content) itr.next(); System.out.println(c); } System.out.println(); System.out.println("Only elements with localname of name or dept:"); itr = doc.getDescendants(new ElementFilter("name") .or(new ElementFilter("dept"))); while (itr.hasNext()) { Content c = (Content) itr.next(); System.out.println(c); } System.out.println(); System.out.println("Remove elements with localname of servlet:"); itr = doc.getDescendants(new ElementFilter("servlet")); while (itr.hasNext()) { itr.next(); itr.remove(); } XMLOutputter outp = new XMLOutputter(); outp.output(doc, System.out); } catch (JDOMException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 通过Xpath表达式计算员工平均工资 */ @SuppressWarnings("unchecked") public void calByXpathExpression() { try { SAXBuilder builder = new SAXBuilder(); File f = new File("WebRoot/xmls/employee.xml"); Document doc = builder.build(f); Element root = doc.getRootElement(); List<Element> employees = XPath.selectNodes(root, "//employees/employee"); System.out.println("共有员工:" + employees.size()); for (Element e : employees) { System.out.print("员工:" + e.getChild("name").getText()); System.out.print(",工资:" + e.getChild("salary").getText()); System.out.println("\n----------------------"); } Object avg = XPath.selectSingleNode(root, "sum(//employee/salary) div count(descendant::employee)"); System.out.println("平均工资:" + avg); System.out.println("----------------------"); int num = 100000; List<Element> sales = XPath.selectNodes(root, "//sales[sale>" + num + "]"); for (Element e : sales) { Element e1 = (Element) e.getParent().getContent().get(1); System.out.println("销售额大于" + num + "的员工有:" + e1.getText() + ",销售额为:" + e.getChild("sale").getText()); } } catch (JDOMException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 通过过滤器计算员工平均工资 */ @SuppressWarnings("unchecked") public void calByGescendantElementFilter() { try { SAXBuilder builder = new SAXBuilder(); File f = new File("WebRoot/xmls/employee.xml"); Document doc = builder.build(f); System.out.println("Only elements with localname of sale:"); Iterator itr = doc.getDescendants(new ElementFilter("sale")); double count = 0; int i = 0; while (itr.hasNext()) { Content c = (Content) itr.next(); count += Integer.parseInt(c.getValue()); i++; } System.out.println("共有员工" + i + "名,平均工资:" + count / i); } catch (JDOMException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 通过JDom读取xml */ @SuppressWarnings("unchecked") public void readerJDom() { try { SAXBuilder builder = new SAXBuilder(); File f = new File("WebRoot/xmls/employee.xml"); Document doc = builder.build(f); XPath servletPath = XPath.newInstance("//employee"); List<Element> employees = servletPath.selectNodes(doc); System.out.println("This project has " + employees.size() + " employees:"); Iterator<Element> i = employees.iterator(); while (i.hasNext()) { Element employee = (Element) i.next(); System.out.print("\t" + employee.getChild("name").getTextTrim() + " : " + employee.getChild("sex").getTextTrim()); Element dept = (Element) employee.getChildren("dept").get(0); System.out.println(" (it owns to" + dept.getText() + " 部门)"); } } catch (JDOMException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
<?xml version="1.0" encoding="UTF-8"?> <employees> <employee id="scce001"> <name>Lan</name> <dept>G3T05</dept> <sex>famale</sex> <birthday> <year>1990</year> <month>01</month> <day>21</day> </birthday> <sales> <year>2011</year> <month>01</month> <sale>70000</sale> </sales> <salary>3000</salary> </employee> <employee id="scce002"> <name>TuDou</name> <dept>G3T05</dept> <sex>male</sex> <birthday> <year>1989</year> <month>01</month> <day>24</day> </birthday> <sales> <year>2011</year> <month>01</month> <sale>394000</sale> </sales> <salary>10000</salary> </employee> <employee id="scce003"> <name>Lee</name> <dept>G3T05</dept> <sex>male</sex> <birthday> <year>1989</year> <month>09</month> <day>03</day> </birthday> <sales> <year>2011</year> <month>01</month> <sale>188880</sale> </sales> <salary>6000</salary> </employee> </employees>
<?xml version="1.0" encoding="UTF-8"?> <employees> <employee id="scce001"> <name>Lan</name> <dept>G3T05</dept> <sex>famale</sex> <birthday> <year>1990</year> <month>01</month> <day>21</day> </birthday> <sales> <year>2011</year> <month>01</month> <sale>70000</sale> </sales> <salary>3000</salary> </employee> <employee id="scce002"> <name>TuDou</name> <dept>G3T05</dept> <sex>male</sex> <birthday> <year>1989</year> <month>01</month> <day>24</day> </birthday> <sales> <year>2011</year> <month>01</month> <sale>394000</sale> </sales> <salary>10000</salary> </employee> <employee id="scce003"> <name>Lee</name> <dept>G3T05</dept> <sex>male</sex> <birthday> <year>1989</year> <month>09</month> <day>03</day> </birthday> <sales> <year>2011</year> <month>01</month> <sale>188880</sale> </sales> <salary>6000</salary> </employee> </employees>