XpathByJDom

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>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值