`
annewman
  • 浏览: 30167 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

JAVA 写XML File

    博客分类:
  • File
阅读更多

本人不善言谈,直接贴出代码,希望给有需要的朋友一些帮助。

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class OperationXmlFile {

	public static Document createDocument() {
		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder builder;
			builder = factory.newDocumentBuilder();
			return builder.newDocument();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static Element addChildElement(Element parentElement,
			String elementName, String nodeValue, Document document) {
		Element el = document.createElement(elementName);
		el.appendChild(document.createTextNode(nodeValue));
		if (parentElement != null)
			parentElement.appendChild(el);
		else
			document.appendChild(el);
		return el;
	}

	public static void addAttribute(Element element, String propetyName,
			String propertyValue) {
		element.setAttribute(propetyName, propertyValue);
	}

	public static void toSave(Document document, String filePath) {
		try {
			TransformerFactory tf = TransformerFactory.newInstance();
			Transformer transformer = tf.newTransformer();
			DOMSource source = new DOMSource(document);
			transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
			transformer.setOutputProperty(OutputKeys.INDENT, "yes");
			ByteArrayOutputStream buf = new ByteArrayOutputStream();
			StreamResult result = new StreamResult(buf);
			transformer.transform(source, result);
			FileOutputStream file = new FileOutputStream(filePath);
			file.write(buf.toByteArray());
			transformer.transform(source, result);
		} catch (TransformerException mye) {
			mye.printStackTrace();
		} catch (IOException exp) {
			exp.printStackTrace();
		}
	}

	public static void main(String[] args) {
		Document document = createDocument();
		Element el = OperationXmlFile.addChildElement(null, "hehe", "",
				document);
		Element elchi = OperationXmlFile.addChildElement(el, "haha", "", document);
			OperationXmlFile.addChildElement(elchi, "haha", "ggg", document);
		OperationXmlFile.addChildElement(el, "haha2", "haha2", document);
		OperationXmlFile.toSave(document, "E:/aa.xml");
	}
}

 

分享到:
评论

相关推荐

    使用java读写xml文档

    使用java读写xml文档,方便存取一些记录信息。中文乱码解决方法先写文件File,在写输出流。

    Java解析XML工具类--(java源码)

    public static Document parse( String xmlFile ) throws Exception { // 绑定XML文件,建造DOM树 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf....

    javaxml,xsl,html文件转换.pdf

    Source source = new StreamSource(xmlFile); Result result = new StreamResult(htmlFile); t.transform(source, result); } ``` 在上面的代码中,我们首先创建了一个 TransformerFactory 对象,然后创建了一个 ...

    java_dom解析xml xml java

    java dom解析xml <?xml version="1.0" encoding="UTF-8"?> <File> <Head> <FileVersion>010000</FileVersion> <FileLength>00000CC3</FileLength> <FileTime>20100629173405</FileTime> ...

    处理xml文件的java类库

    jdom.rar - jdom 是一个处理xml文件的java类库,可以方便地对xml文件进行读或写-jdom is a processing xml file java class library, can easily read or write xml file

    java-open-the-xml-file.zip_java Open xml_open

    java 打开xml文件 使用JAVA实现 精确简洁 使用最小的空间实现最全的功能

    XML.rar_XML java_java xml_java xml Document sa_xml

    Save Document object to XML file

    tir file java to xml conver to file

    Get New Customers Online Advertise On Google. Get 1500 INR Advertising Credit When You Sign-Up www.Google.com/AdWords

    用Java实现的XML编辑器源码

    1.本程序初步实现了可视化编辑XML的功能,即通过在树上点击添加、删除结点,添加结点内容、属性等来实现XML文件的编辑。 2.整个编辑通过选中树结点后点右键实现。 3.菜单栏上仅File菜单中的功能全部实现,其它菜单暂...

    javaapi源码文档-java-xml-tool:这个Java项目说明了Java提供的XMLAPI的用法:解析和验证XML文档,执行XSL转

    Java API API文档java-xml-tool 这个Java项目说明了Java提供的XML API的用法: 解析和验证XML文档, 执行XSL转换 使用来自XML源的JPA填充数据库。 要求 ...Java项目管理工具maven ...xmlfile validate schemafile

    java 工具类(File,Date,Xml,Number,sqlhelp..)

    个人收录工具包里包括 对文件的操作,对日前的操作,对于xml的操作,对于数字转换中文操作,连接数据库通用类,正则表达式等其他

    idea设置file mask后缀名的find.xml配置文件

    idea设置file mask后缀名的find.xml配置文件。替换位置:C:\用户名\用户账号\AppData\Roaming\JetBrains\IntelliJIdea2021.1\options下。

    xml2jsonjar包

    import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import ...

    java文件读写操作

    File file=new File("/root/sms.log"); if(!file.exists()) file.createNewFile(); FileOutputStream out=new FileOutputStream(file,true); for(int i=0;i;i++)......{ StringBuffer sb=new StringBuffer();...

    Java把plist转成xml,备份shsh文件

    ios在解锁或者备份shsh文件时,从手机里获取的plist文件是打不开的,我们可以通过java转成xml或者可以打的plist。shsh文件备份, import java.io.*; import com.dd.plist.PropertyListParser; public class ...

    JAVA WEB 开发详解:XML+XSLT+SERVLET+JSP 深入剖析与实例应用.part2

    本书共分4部分,从xml、servlet、jsp和应用的角度向读者展示了java web开发中各种技术的应用,循序渐进地引导读者快速掌握java web开发。.  本书内容全面,涵盖了从事java web开发所应掌握的所有知识。在知识的讲解...

    Web.xml文件配置JDOM對XML文件操作

    import java.io.File;... Document document=sAXBuilder.build(xmlFile); element=document.getRootElement().getChild(firstChild); }catch(Exception e){ e.printStackTrace(); } return element; } }

    根据XML生成JAVA类-freemarker

    freemarker相关介绍 ...默认输出目录:D:\test_file\genrate_java 可在工具类CommonUtil中修改 关于freemarker的更多介绍,可以查看相匹配的文章 http://blog.csdn.net/mu_wangyue/article/details/9878747

    xml parser

    The information can be retrieved from the XML file, with the help of JAVA external library JDOM, which is a convenient kind of Java external library specially used to deal with the XML files....

    java+JDK+examples.rar_Swing Examples_java Applet_java file handl

    169个实例,内容涉及Java的语言基础、面向对象程序设计、数字处理、数组与集合、字符串、异常处理、文件操作、多线程、Swing编程、图形和多媒体编程、反射机制、网络程序设计、数据库编程、Applet、Java与XML、Java ...

Global site tag (gtag.js) - Google Analytics