jdom操作xml Demo

4 篇文章 0 订阅
1 篇文章 0 订阅

感觉学一个东西经常容易忘代码,在此弄一个jdom的操作大全,以此备忘!


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.Text;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import org.jdom.xpath.XPath;

/**
 * 
 * @ClassName: TestJdomDemo 
 * @Description:动态的去创建XML文档
 * 		去解析并修改XML文档
 * 		如何快速的解析XML文档
 */
public class TestJdomDemo {
	public static void main(String[] args) throws Exception {
		//获取项目的路径
		String path = System.getProperty("user.dir") + File.separator + "src" + File.separator + "users.xml";
		//动态的去创建XML文档
		//		createXML(path);
		//解析并修改XML文档
		//		parseAndUpdateXML(path);
		//快速的获取并解析XML文档
		xpathXML(path);
	}

	/**
	 * 快速的获取并解析XML文档
	 * @param path
	 * @throws IOException 
	 * @throws JDOMException 
	 * @throws FileNotFoundException 
	 */
	private static void xpathXML(String filePath) throws FileNotFoundException, JDOMException, IOException {
		//获取XML的根节点
		Element usersElement = new SAXBuilder().build(new FileInputStream(filePath)).getRootElement();
		//进行快速解析
		//		String path = "/users/user/realname/xing";//获取路径对应的节点
		//		String path = "/users/user//xing";//获取路径对应的节点
		//		String path = "/users//xing[@bbc='3'][@abc='2']";//通过属性获取节点
		String path = "/users//xing[text()='张2'][@abc='1']";//通过文本获取节点

		XPath xPath = XPath.newInstance(path);
		//获取指定的节点
		//		Element xingElement = (Element) xPath.selectSingleNode(usersElement);
		//		System.out.println(xingElement.getText());
		//获取所有的符合条件的
		List<Element> elements = xPath.selectNodes(usersElement);
		for (Element element : elements) {
			System.out.println(element.getName() + ":" + element.getText());
		}
	}

	/**
	 * 解析并修改XML文档
	 * @param path
	 * @throws IOException 
	 * @throws JDOMException 
	 */
	private static void parseAndUpdateXML(String path) throws JDOMException, IOException {
		//创建解析器
		SAXBuilder saxBuilder = new SAXBuilder();
		//获取文件的输入流
		InputStream is = new FileInputStream(path);
		//解析并获取文档
		Document document = saxBuilder.build(is);
		//获取根节点
		Element usersElement = document.getRootElement();
		//		new SAXBuilder().build(new FileInputStream(path)).getRootElement();
		/*******************开始对文档进行解析***********************/
		//获取指定的节点
		//		Element userElement = usersElement.getChild("user");
		//获取节点的属性值
		//		System.out.println(userElement.getAttributeValue("type"));
		//获取文本值
		//		System.out.println(usersElement.getChild("user").getChild("uname").getText());
		//		System.out.println(usersElement.getChild("user").getChild("realname").getChildText("xing"));
		//获取指定节点的所有子节点
		//		List<Element> eList = userElement.getChildren();
		//		for (Element element : eList) {
		//			System.out.println(element.getName());
		//		}
		/*******************开始对文档进行修改***********************/
		//操作元素节点(文本值)
		usersElement.getChild("user").getChild("uname").setText("zhangsanfeng");
		//操作元素节点(删除)
		usersElement.getChild("user").removeChild("pwd");
		//操作元素节点(新增)
		Element emailElement = new Element("email").setText("1234567@qq.com");
		usersElement.getChild("user").addContent(2, emailElement);
		//操作属性节点(删除)
		usersElement.getChild("user").removeAttribute("type");
		//操作属性节点(修改)
		usersElement.getChild("user").setAttribute("id", "888");
		//操作属性节点(新增)
		usersElement.getChild("user").setAttribute("aaaa", "1111");
		//继续添加文本内容
		usersElement.addContent("我想做一个好人");
		//继续添加文本到第一行
		usersElement.addContent(1, new Text("我想做一个坏人"));
		/*******************如果需要让你的修改生效,需要重新写出***********************/
		//设置输出的格式
		Format format = Format.getPrettyFormat();//美观的--适合阅读
		//		Format format = Format.getCompactFormat();//紧凑的--适合传输
		format.setEncoding("utf-8");
		//将文档对应的内容写出到硬盘
		XMLOutputter xmlOutputter = new XMLOutputter(format);
		xmlOutputter.output(document, new FileOutputStream(path));
	}

	/**
	 * 动态的去创建XML文档
	 * @param path
	 * @throws IOException 
	 * @throws FileNotFoundException 
	 */
	private static void createXML(String path) throws FileNotFoundException, IOException {
		//创建所需要的节点
		Element usersElement = new Element("users");
		Element userElement = new Element("user");
		Element unameElement = new Element("uname");
		Element pwdElement = new Element("pwd");
		Element realnameElement = new Element("realname");
		Element xingElement = new Element("xing");
		Element mingElement = new Element("ming");
		//给节点添加属性
		Attribute typeAttr = new Attribute("type", "student");
		userElement.setAttribute(typeAttr);
		userElement.setAttribute("id", "999");
		//给节点添加文本内容
		unameElement.setText("zhangsan");
		pwdElement.setText("123456");
		xingElement.setText("张");
		mingElement.setText("三三");
		//将节点有机的进行关联
		usersElement.addContent(userElement);
		userElement.addContent(unameElement);
		userElement.addContent(pwdElement);
		userElement.addContent(realnameElement);
		realnameElement.addContent(xingElement);
		realnameElement.addContent(mingElement);
		//根据根节点创建文档对象模型
		Document document = new Document(usersElement);
		//设置输出的格式
		Format format = Format.getPrettyFormat();//美观的--适合阅读
		//		Format format = Format.getCompactFormat();//紧凑的--适合传输(空格也会占用传输资源)
		format.setEncoding("utf-8");
		//将文档对应的内容写出到硬盘(这一步可以抽取成一个方法)
		XMLOutputter xmlOutputter = new XMLOutputter(format);
		xmlOutputter.output(document, new FileOutputStream(path));
	}
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值