java 导出xml文件的四种方式

时间:2019-12-11
本文章向大家介绍java 导出xml文件的四种方式,主要包括java 导出xml文件的四种方式使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
public class CreateXML {
 
	//DOM方式创建XML文件
	public void DOMcreateXML() {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = null;
			try {
			db = factory.newDocumentBuilder(); 
			Document document = db.newDocument();
			document.setXmlStandalone(true);
			Element bookstore = document.createElement("bookstore");
			Element book = document.createElement("book");
			Element name = document.createElement("name");
			name.setTextContent("<c++>");
			book.setAttribute("id", "three");
			book.appendChild(name);
			bookstore.appendChild(book);			
			document.appendChild(bookstore);
			TransformerFactory tff = TransformerFactory.newInstance();
			Transformer tf = tff.newTransformer();
			tf.setOutputProperty(OutputKeys.INDENT, "yes");
			tf.transform(new DOMSource(document), new StreamResult(new File("res/books2.xml")));
			}catch (ParserConfigurationException e1) {
				e1.printStackTrace();
			}catch (TransformerConfigurationException e) {
				e.printStackTrace();
			} catch (TransformerException e) {
				e.printStackTrace();
			}	
	}
	//SAX生成XML文件
	public void SAXCreateXML() {
		
		SAXTransformerFactory tff = (SAXTransformerFactory)SAXTransformerFactory.newInstance();
		try {
			TransformerHandler handler = tff.newTransformerHandler();
			Transformer tf = handler.getTransformer();
			tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
			tf.setOutputProperty(OutputKeys.INDENT, "yes");
			File f = new File("res/newbooks.xml");
			if(!f.exists()) {
				f.createNewFile();
			}
			Result result = new StreamResult(new FileOutputStream(f));
			handler.setResult(result);
			handler.startDocument();
			AttributesImpl attr = new AttributesImpl();
			handler.startElement("", "", "bookstore",attr);
			attr.clear();
			attr.addAttribute("", "", "id", "", "one");
			handler.startElement("", "", "book", attr);
			
			attr.clear();
			handler.startElement("", "", "name", attr);
			handler.characters(new char[]{'<','c','>'}, 0, "<c>".length());
			handler.endElement("", "", "name");
			handler.endElement("", "", "book");	
			handler.endElement("", "", "bookstroe");
			
			handler.endDocument();
		} catch (TransformerConfigurationException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}catch (IOException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		}
		
	}
	
	//DOM4J创建XML
	public void DOM4JCreateXML() {
		org.dom4j.Document document = DocumentHelper.createDocument();
		org.dom4j.Element rss = document.addElement("rss");
		rss.addAttribute("version", "2.0");
		org.dom4j.Element channel = rss.addElement("channel");
		org.dom4j.Element title = channel.addElement("title");
		title.setText("国内新闻");
		OutputFormat format  = OutputFormat.createPrettyPrint();
		//生成文件
		File file = new File("res/rssbooks.xml");
		XMLWriter writer;
		try {
			writer = new XMLWriter(new FileOutputStream(file),format);
			writer.setEscapeText(false);
			writer.write(document);
			writer.close();
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	//JDOM创建XML
	public void JDOMCreateXML() {
		
		org.jdom2.Element rss = new org.jdom2.Element("rss");
		rss.setAttribute("version", "2.0");
		
		
		//生成一个document对象
		org.jdom2.Document document = new org.jdom2.Document(rss);
		
		org.jdom2.Element channel = new org.jdom2.Element("channel");
		rss.addContent(channel);
		org.jdom2.Element title = new org.jdom2.Element("title");
		title.setText("国内新闻");
		channel.addContent(title);
		//创建XMLoutputer对象
		Format format = Format.getCompactFormat();
		format.setIndent("");
		format.setEncoding("utf-8");
		XMLOutputter outputer = new XMLOutputter(format);
		//利用outputer将document对象转换成xml文档
		try {
			outputer.output(document, new FileOutputStream(new File("res/jdom.xml")));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}

  

原文地址:https://www.cnblogs.com/Koaler/p/12021034.html