import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
/**
*
- @description: 读取XML文件信息
*/
public class XmlUtil {
/**
* 根据路径读取XML
*
* @param path 文件路径
* @return
* @throws Exception
*/
public static Document readXmlByPath(String path) throws IOException, SAXException, ParserConfigurationException, URISyntaxException {
return readXML(new File(FileUtils.getAbsolutePath(path)));
}
/**
* 根据文件读取xml,(DOM方式)
*
* @param file
* @return
* @throws Exception
*/
public static Document readXML(File file) throws ParserConfigurationException, IOException, SAXException {
if (!file.exists()) {
throw new FileNotFoundException("File [{}] not a exist!");
} else if (!file.isFile()) {
throw new FileNotFoundException("[{}] not a file!");
} else {
Document doc = null;
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
return builder.parse(new FileInputStream(file));
}
}
/**
* 将Document转化为file
*
* @param doc
* @param path doc地址
* @param charset 字符集
* @throws IOException
* @throws URISyntaxException
*/
public static void toFile(Document doc, String path, String charset) throws IOException, URISyntaxException {
if (StrUtil.isBlank(charset)) {
charset = "UTF-8";
}
BufferedWriter writer = null;
try {
writer = getWriter(path, charset, false);
write(doc, (Writer) writer, charset, 2);
} finally {
close(writer);
}
}
public static void close(Closeable closeable) {
if (null != closeable) {
try {
closeable.close();
} catch (Exception var2) {
}
}
}
/**
* 根据地址写文件
*
* @param path 地址
* @param charsetName 字符集
* @param isAppend 应用程序是否结束
* @return
* @throws IOException
*/
public static BufferedWriter getWriter(String path, String charsetName, boolean isAppend) throws IOException, URISyntaxException {
return getWriter(touch(path), Charset.forName(charsetName), isAppend);
}
/**
* 根据文件路径返文件
*
* @param fullFilePath
* @return
* @throws IOException
*/
public static File touch(String fullFilePath) throws IOException, URISyntaxException {
return fullFilePath == null ? null : touch(file(fullFilePath));
}
/**
* 根据文件绝对路径转换为文件
*
* @param path
* @return
*/
public static File file(String path) throws URISyntaxException {
if (StrUtil.isBlank(path)) {
throw new NullPointerException("File path is blank!");
} else {
return new File(FileUtils.getAbsolutePath(path));
}
}
/**
* 根据文件名创建文件夹和文件
*
* @param file
* @return
* @throws IOException
*/
public static File touch(File file) throws IOException {
if (null == file) {
return null;
} else {
if (!file.exists()) {
mkParentDirs(file);
try {
file.createNewFile();
} catch (Exception var2) {
throw new IOException(var2);
}
}
return file;
}
}
/**
* 创建父目录
*
* @param file 文件名
* @return 父目录
*/
public static File mkParentDirs(File file) {
File parentFile = file.getParentFile();
if (null != parentFile && !parentFile.exists()) {
parentFile.mkdirs();
}
return parentFile;
}
/**
* 根据文件名写文件
*
* @param file 文件名
* @param charset 字符集
* @param isAppend 是否结束
* @return
* @throws IOException
*/
public static BufferedWriter getWriter(File file, Charset charset, boolean isAppend) throws IOException {
return com.huawei.cube.crypto.model.FileWriter.create(file, charset).getWriter(isAppend);
}
/**
* 写文件
*
* @param node 节点
* @param writer 字符流
* @param charset 字符集
* @param indent 缩进
*/
public static void write(Node node, Writer writer, String charset, int indent) {
transform(new DOMSource(node), new StreamResult(writer), charset, indent);
}
/**
* 将XML转化为result
*
* @param source XML路径
* @param result 转化结果
* @param charset 字符集
* @param indent 缩进
*/
public static void transform(Source source, Result result, String charset, int indent) {
TransformerFactory factory = TransformerFactory.newInstance();
try {
Transformer xformer = factory.newTransformer();
if (indent > 0) {
xformer.setOutputProperty("indent", "yes");
xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(indent));
}
if (!StrUtil.isBlank(charset)) {
xformer.setOutputProperty("encoding", charset);
}
xformer.transform(source, result);
} catch (Exception var6) {
throw new RuntimeException("Trans xml document to string error!");
}
}
}