⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmlutil.java

📁 一个“对象--XML 映射”(Object-Xml Mapping) 的类库。 它的目的是帮助开发者方便、快速的从XML 文件构建出Java 对象
💻 JAVA
字号:
/**
 * @author 沈东良 <a href="mailto:shendl_s@hotmail.com">shendl_s@hotmail.com</a>
 * 2007-1-17 下午11:26:46
 */
package net.sf.oxmled.xml.util.dom4j;



import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.dom4j.tree.DefaultElement;

import net.sf.oxmled.common.util.ClassLoaderUtil;
import net.sf.oxmled.model.INode;
import net.sf.oxmled.model.Node;
import net.sf.oxmled.xml.util.dom4j.override.Dom4jXMLWriter;



/**
 * @author 沈东良 <a href="mailto:shendl_s@hotmail.com">shendl_s@hotmail.com</a>
 *         2007-1-17 下午11:26:46
 * 
 * 1,各个具体的xml产生类,应该自己根据格式和model,构建装配好的Element,然后调用createDocument,得到完整的Document对象
 * 
 * 2,修饰xml文档格式
 * 
 * 3,序列化
 * 
 * 
 */
public class XmlUtil implements IXmlUtil {
	private  Log log = LogFactory.getLog(XmlUtil.class);

	/**
	 * @return log
	 */
	public  Log getLog() {
		return log;
	}

	/**
	 * @param log
	 *            要设置的 log
	 */
	public  void setLog(Log log) {
		this.log = log;
	}

	/* (non-Javadoc)
	 * @see com.parkmanage.oxml.util.IXmlUtil#parse(java.net.URL)
	 */
	public  Document parse(URL url) throws DocumentException {
		SAXReader reader = new SAXReader();
		// 生成DOM(Document对象)
		Document document = reader.read(url);
		return document;
	}

	/* (non-Javadoc)
	 * @see com.parkmanage.oxml.util.IXmlUtil#write(org.dom4j.Document, java.lang.String)
	 */
	public  void write(Document document, String fileName)
			throws IOException {

		// lets write to a file
		XMLWriter writer = new Dom4jXMLWriter(new FileWriter(fileName));
		writer.write(document);
		writer.close();

	}

	/* (non-Javadoc)
	 * @see com.parkmanage.oxml.util.IXmlUtil#writeToPrettyPrint(org.dom4j.Document, java.lang.String)
	 */
	public  void writeToPrettyPrint(Document document, String fileName)
			throws IOException, URISyntaxException {
		OutputFormat format = OutputFormat.createPrettyPrint();
		// 得到文档的编码格式,并设置。
		format.setEncoding(document.getXMLEncoding());
		// lets write to a file

		XMLWriter writer = new Dom4jXMLWriter(new FileOutputStream(new File(
				ClassLoaderUtil.getResource(fileName).toURI())), format);

		// document.setXMLEncoding(document.getXMLEncoding());

		writer.write(document);
		writer.close();

	}

	/**
	 * 可以不用
	 * 
	 * 返回美观格式的xml文本 这个方法是用JDOm
	 * 
	 * @param xml
	 * @return
	 * @throws IOException
	 * @throws JDOMException
	 * 
	 * public  String getPrettyFormatXml(String xml) throws IOException,
	 * JDOMException{ /**
	 * 
	 * 
	 *  /* XMLWriter for viewing SAX events.
	 *  // XMLWriter echo = new XMLWriter();
	 *  /* Build document from xml file.
	 * 
	 * SAXBuilder builder = new SAXBuilder(); // builder.setXMLFilter(echo);
	 * org.jdom.Document document = builder.build(new StringReader(xml));
	 * 
	 * XMLOutputter out=new XMLOutputter(Format.getPrettyFormat()); return
	 * out.outputString(document);
	 * 
	 *  }
	 */

	/* (non-Javadoc)
	 * @see com.parkmanage.oxml.util.IXmlUtil#writeToPrettyPrint(org.dom4j.Document)
	 */
	public  String writeToPrettyPrint(Document document)
			throws IOException {
		OutputFormat format = OutputFormat.createPrettyPrint();
		format.setEncoding(document.getXMLEncoding());

		try {
			StringWriter out = new StringWriter(1024);
			XMLWriter writer = new Dom4jXMLWriter(out, format);
			writer.write(document);
			/**
			 * StringWriter只能用flush();不能用close()。 String无法关闭!
			 */
			writer.flush();

			return out.toString();
		} catch (IOException e) {
			throw new RuntimeException("IOException while generating textual "
					+ "representation: " + e.getMessage());
		}

	}

	// CompactFormat
	/* (non-Javadoc)
	 * @see com.parkmanage.oxml.util.IXmlUtil#writeToCompactFormat(org.dom4j.Document, java.lang.String)
	 */
	public  void writeToCompactFormat(Document document, String fileName)
			throws IOException {
		OutputFormat format = OutputFormat.createCompactFormat();
		format.setEncoding(document.getXMLEncoding());
		// lets write to a file
		XMLWriter writer = new Dom4jXMLWriter(new FileWriter(fileName), format);

		writer.write(document);
		writer.close();

	}

	/**
	 * 把document对象写到指定的位置,生成xml文件。
	 * 
	 * @param document
	 * @param uri
	 *            使用getResource可以得到URL,URI,String可以互相转换
	 * @throws IOException
	 * @throws URISyntaxException
	 * 
	 * public  void write(Document document,URL url) throws IOException,
	 * URISyntaxException{ XmlUtil.getLog().info("url="+url);
	 * XmlUtil.getLog().info("uri="+url.toURI()); // lets write to a file
	 * 
	 * //File file=new File(url.toURI()); //file.mkdirs();
	 * //file.createNewFile(); FileOutputStream fout=new FileOutputStream(new
	 * File(url.toURI())); XMLWriter writer = new Dom4jXMLWriter(fout);
	 * 
	 * writer.write( document ); // fout.flush();
	 * 
	 * writer.close();
	 * 
	 *  }
	 */
	/* (non-Javadoc)
	 * @see com.parkmanage.oxml.util.IXmlUtil#writeToCompactFormat(org.dom4j.Document, java.net.URI)
	 */
	public  void writeToCompactFormat(Document document, URI uri)
			throws IOException {
		OutputFormat format = OutputFormat.createCompactFormat();
		format.setEncoding(document.getXMLEncoding());
		// lets write to a file
		XMLWriter writer = new Dom4jXMLWriter(new FileOutputStream(
				new File(uri)), format);
		writer.write(document);
		writer.close();

	}

	/* (non-Javadoc)
	 * @see com.parkmanage.oxml.util.IXmlUtil#writeToPrettyPrint(org.dom4j.Document, java.net.URI)
	 */
	public  void writeToPrettyPrint(Document document, URI uri)
			throws IOException {
		OutputFormat format = OutputFormat.createPrettyPrint();
		format.setEncoding(document.getXMLEncoding());
		// lets write to a file
		File file = new File(uri);
		// file.mkdirs();
		XMLWriter writer = new Dom4jXMLWriter(new FileWriter(file), format);
		writer.write(document);
		writer.close();

	}

	/**
	 * @param args
	 */
	public  static void main(String[] args) {
		// TODO 自动生成方法存根

	}

	/* (non-Javadoc)
	 * @see com.parkmanage.oxml.util.IXmlUtil#createDocument(org.dom4j.Element)
	 */
	public  Document createDocument(Element assembledElement) {
		Document document = DocumentHelper.createDocument();
		document.add(assembledElement);

		return document;
	}

	/* (non-Javadoc)
	 * @see com.parkmanage.oxml.util.IXmlUtil#createDocument(org.dom4j.Element, java.lang.String)
	 */
	public  Document createDocument(Element assembledElement,
			String encoding) {
		Document document = DocumentHelper.createDocument();
		document.add(assembledElement);
		document.setXMLEncoding(encoding);

		return document;
	}

	@Override
	public Element convertToAssembledRootElement(INode node) {
		/*
		*
		*/
		Element rootElement=this.convertToElement(node);
		 
		/**
		 * PUBLIC Element convertLoopElement
		 * 
		 */
		this.loopElement(rootElement, node);
		
		return rootElement;
	}

	@Override
	public Element convertToElement(INode node) {
		/*
		*
		*/
		Element element=new DefaultElement(node.getName());

	    element.addText(node.getText());
	    /**
	     * 读取所有属性
	     */
	
	 Iterator<Map.Entry<String,String>> it=  node.getAttributes().entrySet().iterator();
	//Iterator itValue=this.getAttributes().values().iterator();
	 while(it.hasNext()){
		 Map.Entry<String,String> tmp=it.next();
		 element.addAttribute(tmp.getKey(), tmp.getValue());
	 }

	    return element;

	}

	@Override
	public Document getDocument(INode node) {
		/*
		*
		*/
		Element rootElement=this.convertToAssembledRootElement(node);
		
		Document document=DocumentHelper.createDocument(); 
		document.setRootElement(rootElement);
		document.setXMLEncoding(node.getEncoding());
		return document;
	}

	@Override
	public INode getINode(Document document) {
		/*
		*根据Document,得到完整的节点数
		*/
		INode node=this.convertToAssembledRootINode(document.getRootElement());
		node.setRoot(true);
		node.setEncoding(document.getXMLEncoding());
		
		return node;
	}

	@Override
	public Element loopElement(Element parentElement, INode parentNode) {
		/*
		*
		*/
		/**
		 * 对父亲节点的儿子节点,加到父亲元素上
		 * 
		 * 
		 */
		Iterator it=parentNode.getChildNodes().iterator();
		while(it.hasNext()){
			INode tmpChildNode=(INode)it.next();
			Element tmpChildElement=this.convertToElement(tmpChildNode);
			parentElement.add(tmpChildElement);
			/**
			 * 递归,加到用户父亲元素上。
			 */
			this.loopElement(tmpChildElement, tmpChildNode);
		}
		
		
		return parentElement;
	}

	@Override
	public INode convertToINode(Element element) {
		/*
		*把元素转成INode
		*/
		INode node =new Node();
		node.setName(element.getName());
		node.setText(element.getTextTrim());
		Iterator<Attribute> it=element.attributeIterator();
		while(it.hasNext()){
			Attribute tmp=it.next();
			String key=tmp.getName();
			String value=tmp.getValue();
			node.getAttributes().put(key, value);
		}
		
		return node;
	}

	@Override
	public INode convertToAssembledRootINode(Element element) {
		/*
		*转换成INode树
		*1,根节点
		*2,逐层取出element下面的东西,直到全部取出
		*   1,取出元素的儿子元素,转换成Node后插到福节点上。
		*
		*3,装配
		*
		*/
		INode rootNode=this.convertToINode(element);
		rootNode=this.loopINode(element, rootNode);
		
		return rootNode;
	}
	@Override
	public INode loopINode(Element parentElement, INode parentNode) {
		/*
		*1,如果有,取出元素的子元素,  转换后,加到Node上
		*
		*/
		Iterator<Element> it=parentElement.elementIterator();
		while(it.hasNext()){
			Element tmpElement=it.next();
			INode  tmpNode=this.convertToINode(tmpElement);
			parentNode.getChildNodes().add(tmpNode);
			this.loopINode(tmpElement, tmpNode);
		}
		
		
		return parentNode;
	}

	@Override
	public INode loadXmlFile(URL url) throws Exception {
		/*
		*
		*/
		Document document=this.parse(url);
		return this.getINode(document);
	}

	@Override
	public void write(INode node, URL url) throws IOException, URISyntaxException {
		/*
		*
		*/
		Document document=this.getDocument(node);
		
		// lets write to a file
		XMLWriter writer = new Dom4jXMLWriter(new FileWriter(new File(url.toURI())));
		writer.write(document);
		writer.close();
		
	}

	@Override
	public void write(INode node, String fileName) throws IOException, URISyntaxException {
		/*
		*
		*/
      Document document=this.getDocument(node);
      
		
		// lets write to a file
		XMLWriter writer = new Dom4jXMLWriter(new FileWriter(new File(ClassLoaderUtil.getResource(fileName).toURI())));
		writer.write(document);
		writer.close();
		
	}

	@Override
	public void writeToCompactFormat(INode node, String fileName) throws IOException {
		/*
		*
		*/
		Document document=this.getDocument(node);
		this.writeToCompactFormat(document, fileName);
	}

	@Override
	public void writeToCompactFormat(INode node, URL url) throws IOException, URISyntaxException {
		/*
		*
		*/
		Document document=this.getDocument(node);
		this.writeToCompactFormat(document, url.toURI());
		
	}

	@Override
	public void writeToPrettyPrint(INode node, URL url) throws IOException, URISyntaxException {
		/*
		*
		*/
		Document document=this.getDocument(node);
		this.writeToPrettyPrint(document, url.toURI());
		
	}

	@Override
	public void writeToPrettyPrint(INode node, String fileName) throws IOException, URISyntaxException {
		/*
		*
		*/
		Document document=this.getDocument(node);
		this.writeToPrettyPrint(document, fileName);
		
	}

	

	

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -