package example01;
import org.w3c.dom.Document;
import org.w3c.dom.Element; 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 java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets; public class doit {
public static void main(String[] args) {
Document doc = createDocument();
Element root = doc.createElement("students");
doc.appendChild(root);
addStudentInfo(doc,root,"王宏","20100101","96","88","90");
addStudentInfo(doc,root,"李娜","20100102","75","56","70");
addStudentInfo(doc,root,"孙武","20100103","77","70","20");
outputXMLFile(doc,"student.xml");
}
/* 通过DOM解析器创建一个空的Document对象 */
public static Document createDocument() {
// 通过newInstance方法创建DocumentBuilderFactory对象
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
Document doc = null;
try {
// 创建解析器对象
DocumentBuilder db = dbf.newDocumentBuilder();
// 创建一个空的Document对象
doc = db.newDocument();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
return doc;
}
public static void addStudentInfo(Document doc,Element root,String studentName,String studentId,String javaContent,String OracleContent,String umlContent){
Element student = doc.createElement("student");
student.setAttribute("id",studentId);
root.appendChild(student);
Element name = doc.createElement("name");
name.setTextContent(studentName);
student.appendChild(name);
Element java = doc.createElement("java");
java.setTextContent(javaContent);
student.appendChild(java);
Element oracle = doc.createElement("oracle");
oracle.setTextContent(OracleContent);
student.appendChild(oracle);
Element uml = doc.createElement("uml");
uml.setTextContent(umlContent);
student.appendChild(uml);
}
/* 将内存中的DOM树输出为一个xml文档 */
public static void outputXMLFile(Document doc,String filename) {
try {
TransformerFactory tff = TransformerFactory.newInstance();
Transformer tf = tff.newTransformer();
//设置输出XML文件的换行
tf.setOutputProperty(OutputKeys.INDENT, "yes");
//设置输出XML文件的缩进
tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
//把DOM对象转换为XML对象
DOMSource source = new DOMSource(doc);
//创建一个输出XML文件对象
//StreamResult result = new StreamResult(new File(filename));
StreamResult result = new StreamResult(new PrintStream(new FileOutputStream(filename),true, StandardCharsets.UTF_8));
//把XML源代码输出为XML文件
tf.transform(source, result);
} catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} }
}

关于编码问题

保存的XML是UTF-8格式

整个java项目也是UTF-8格式

我在写的过程中遇到了保存出来的XML文件有中文乱码的问题。

事情是这样的,老师发的Java文件是Eclipse写的,我用的Idea打开,然后打开中文乱码

于是我就把Java文件项目编码调成了GBK,结果写出来的文件就中文乱码了。

所以我在输出文件的函数那,把StreamResult result = new StreamResult(new File(filename));改成了StreamResult result = new StreamResult(new PrintStream(new FileOutputStream(filename),true, StandardCharsets.UTF_8));

你可以试一试这样,但是我这样并没有生效。

萌狼蓝天的解决方法是:

把项目编码,Java文件编码都设置成UTF-8,清除缓存重新启动即可。

萌狼-图1-修改项目编码

萌狼-图2-清除缓存

【XML】Java创建XML文档的更多相关文章

  1. [java,2017-05-04] 创建word文档

    package test; import java.text.SimpleDateFormat; import java.util.Date; import com.aspose.words.Data ...

  2. java对word文档的操作(提取标题和内容等)-直接操作或poi工具包或freemarker+xml或html转word

    1,java自带工具包实现对word的排版和写入 import java.awt.Color; import java.io.FileNotFoundException; import java.io ...

  3. Java 操纵XML之创建XML文件

    Java 操纵XML之创建XML文件 一.JAVA DOM PARSER DOM interfaces The DOM defines several Java interfaces. Here ar ...

  4. poi 读取使用 Strict Open XML 保存的 excel 文档

    poi 读取使用 Strict Open XML 保存的 excel 文档 某项目有一个功能需要读取 excel 报表内容,使用poi读取时报错: 具体错误为: org.apache.poi.POIX ...

  5. xml和xsd架构文档相关知识

    1.使用架构(XSD)验证XML文件 2.使用自动生成工具: 工具目录:C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4. ...

  6. Java 后台创建word 文档

    ---恢复内容开始--- Java 后台创建 word 文档 自己总结  网上查阅的文档 分享POI 教程地址:http://www.tuicool.com/articles/emqaEf6 方式一. ...

  7. java将office文档pdf文档转换成swf文件在线预览

    第一步,安装openoffice.org openoffice.org是一套sun的开源office办公套件,能在widows,linux,solaris等操作系统上执行. 主要模块有writer(文 ...

  8. 使用Docx4j创建word文档

    原文标题:Creating Word documents with Docx4j 原文链接:http://blog.iprofs.nl/2012/09/06/creating-word-documen ...

  9. Java版office文档在线预览

    java将office文档pdf文档转换成swf文件在线预览 第一步,安装openoffice.org openoffice.org是一套sun的开源office办公套件,能在widows,linux ...

  10. 使用Java生成word文档(附源码)

    当我们使用Java生成word文档时,通常首先会想到iText和POI,这是因为我们习惯了使用这两种方法操作Excel,自然而然的也想使用这种生成word文档.但是当我们需要动态生成word时,通常不 ...

随机推荐

  1. Android USB 整条通知链分析

    以Android13 为例:在Android 13中,USB插入拔出事件的通知链从硬件层到应用层,依次经过硬件.内核.HAL.Framework.以及应用广播等多个模块,整个流程涉及的具体文件和逻辑如 ...

  2. 如何实现高效运维?来谈谈性能优化那些事(含直播回顾 Q&A)

    数据库性能问题,常常是困扰DBA高效运维的难题之一.如何多角度地帮助DBA,找到"数据库慢"的原因,保证系统高效.稳定.安全地运行? 2021年10月14日,云和恩墨技术顾问,拥有 ...

  3. js 时间日期

    Date.parse()  把字符串时间转化为时间戳. new Date(时间戳) 转化 时间格式 时间比较大小

  4. 【异常处理】Assistive Technology not found: com.sun.java.accessibility.AccessBridge

    十一回来之后,工作电脑上的抓包工具Charles突然启动不起来了,双击图标后,一闪而过,就没动静了. 不知道是不是因为之前安装了什么工具.软件引起的. 打开CMD命令行,跳转到目录下启动,提示:Ass ...

  5. KubeSphere 在互联网医疗行业的应用实践

    作者:宇轩辞白,运维研发工程师,目前专注于云原生.Kubernetes.容器.Linux.运维自动化等领域. 前言 2020 年我国互联网医疗企业迎来了"爆发元年",越来越多居民在 ...

  6. 在 KubeSphere 中开启新一代云原生数仓 Databend

    作者:尚卓燃(https://github.com/PsiACE),Databend 研发工程师,Apache OpenDAL (Incubating) PPMC. 前言 Databend 是一款完全 ...

  7. 安装gd库出错

    最近手痒,就安装了一个centos 6.5玩玩. 由于新的centos的当前版本已经达到7.1. 而且最糟的是在本人不小心的时候在安装软件的时候, 确定里里面的更新系统命令,导致版本变为了6.9. 然 ...

  8. python多线程应用-批量下载拉勾网课程

    import concurrent import os import re import time from collections.abc import Iterable from Crypto.C ...

  9. 记录EntityFramework增删改产生的SQL语句

    项目里遇到一个数据交换的需求,申报端和审批端的系统和数据库都不在同一个网段:甲方提供一个msmq队列:我们把申报和审批产生的变化数据用sql记录到xml报文中,通过交换xml文件再解析出sql语句来实 ...

  10. 熟悉使用DOS操作命令

    DOS操作实验 一.实验目的 DOS是市场上早期获得巨大成功的桌面操作系统,现在很多同学都不太熟悉.本实验的目的就是让同学们读者从操作系统理论的观点来重新认识它们,了解和掌握DOS有关用户接口的特点. ...