Xml操作接口

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

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

public class XmlUtil {

	private String xmlpath = "";
	private Document doc = null;
	private String contentnode = "//result";

	public XmlUtil() {
		//
	}

	public XmlUtil(String xmlpath) {
		this.xmlpath = xmlpath;
		initial();
	}

	private void initial() {
		SAXBuilder reader = new SAXBuilder();
		try {
			doc = reader.build(new File(xmlpath));
		} catch (JDOMException e) {
			System.out.println(e.getMessage());
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}
	}

	public Element getRootElement() {
		if (doc == null)
			return null;
		return doc.getRootElement();
	}

	public void AddElement(Element parEle, Element childEle) {
		parEle.addContent(childEle);
	}

	public void writeDoc() {
		XMLOutputter out = new XMLOutputter();
		Format f = Format.getPrettyFormat();
		f.setEncoding("UTF-8");
		out.setFormat(f);
		out.getFormat().setEncoding("UTF-8");
		try {
			FileWriter xmlWrite = new FileWriter(xmlpath);
			out.output(doc, xmlWrite);
			xmlWrite.flush();
			xmlWrite.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@SuppressWarnings("rawtypes")
	public List getXmlEleList() {
		XPath path = null;
		try {
			path = XPath.newInstance(contentnode);
		} catch (JDOMException e1) {
			System.out.println(e1.getMessage());
		}
		List list = null;
		try {
			list = path.selectNodes(doc);
		} catch (JDOMException e1) {
			System.out.println(e1.getMessage());
		}
		return list;
	}

	@SuppressWarnings("rawtypes")
	public List getXmlEleList(String xpathtype) {
		XPath path = null;
		try {
			path = XPath.newInstance(xpathtype);
		} catch (JDOMException e1) {
			System.out.println(e1.getMessage());
		}
		List list = null;
		try {
			list = path.selectNodes(doc);
		} catch (JDOMException e1) {
			System.out.println(e1.getMessage());
		}
		return list;
	}

	/** 当前节点向上找指定名称的父节点 **/
	public Element findInParEle(Element nowNode, String nodename) {
		Element parEle = nowNode.getParentElement();
		if (parEle == null)
			return null;
		if (parEle.getName().equals(nodename))
			return parEle;
		else
			return findInParEle(parEle, nodename);
	}

	public void createXmlFile(String filepath, String root_name) {

		// 生成解析结果的模板文档
		Element rootNode = new Element(root_name);
		Document parsersultdoc = new Document(rootNode);

		XMLOutputter fmt = new XMLOutputter();
		try {
			// 生成解析模板文档
			FileWriter writer = new FileWriter(filepath);
			Format f = Format.getPrettyFormat();
			f.setEncoding("UTF-8");
			fmt.setFormat(f);

			fmt.output(parsersultdoc, writer);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		XmlUtil xu = new XmlUtil();
		xu.createXmlFile("C:/a.xml", "testroot");
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值