Java 中的漂亮打印 XML
漂亮地打印 XML 意味着以预期的方式打印 XML 代码。 本教程演示如何在控制台中漂亮地打印 XML 和 XML 文件。
在控制台中漂亮地打印 XML
下面的示例演示了如何创建 XML 并在控制台中漂亮地打印它:
package jiyik;
import java.io.File;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
public class XML_Pretty_Print {
public static void main(String[] args) {
DocumentBuilderFactory Doc_Builder_Factory = DocumentBuilderFactory.newInstance();
DocumentBuilder Document_Builder;
try {
Document_Builder = Doc_Builder_Factory.newDocumentBuilder();
Document document = Document_Builder.newDocument();
//adding elements to Document
Element Root_Element = document.createElementNS("https://www.jiyik.com/", "Tutorials");
document.appendChild(Root_Element);
//add a first child element to the root element
Root_Element.appendChild(Get_Tutorial(document, "1", "Perform String to String Array Conversion in Java",
"https://www.jiyik.com/howto/java/how-to-perform-string-to-string-array-conversion-in-java/",
"Java", "May-21, 2020"));
//add a second child
Root_Element.appendChild(Get_Tutorial(document, "2", "Compile a C++ Program Using GCC",
"https://www.jiyik.com/howto/cpp/gcc-compile-cpp/",
"C++", "March-25, 2022"));
//add third child
Root_Element.appendChild(Get_Tutorial(document, "3", "Python Tutorial - Introduction",
"https://www.jiyik.com/tutorial/python-3-basic-tutorial/python-introduction/",
"Python", "January-29, 2018"));
//create output
TransformerFactory Transformer_Factory = TransformerFactory.newInstance();
Transformer transformer = Transformer_Factory.newTransformer();
//open indent for the xml code (Pretty Print)
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
DOMSource Dom_Source = new DOMSource(document);
//Pretty Print the XML to Console
StreamResult XML_console = new StreamResult(System.out);
transformer.transform(Dom_Source, XML_console);
} catch (Exception e) {
e.printStackTrace();
}
}
private static Node Get_Tutorial(Document document, String id, String article_name, String link, String programming_language, String date_created) {
Element tutorial = document.createElement("Tutorial");
//set the tutorial id attribute
tutorial.setAttribute("Id", id);
//create article name element
tutorial.appendChild(Get_Tutorial_Elements(document, tutorial, "ArticleName", article_name));
//create the Link element
tutorial.appendChild(Get_Tutorial_Elements(document, tutorial, "Link", link));
//create the Programming Language element
tutorial.appendChild(Get_Tutorial_Elements(document, tutorial, "ProgrammingLanguage", programming_language));
//create Date Created element
tutorial.appendChild(Get_Tutorial_Elements(document, tutorial, "DateCreated", date_created));
return tutorial;
}
//method to create text node
private static Node Get_Tutorial_Elements(Document document, Element element, String element_name, String element_value) {
Element element_node = document.createElement(element_name);
element_node.appendChild(document.createTextNode(element_value));
return element_node;
}
}
输出:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Tutorials xmlns="https://www.jiyik.com/">
<Tutorial Id="1">
<ArticleName>Perform String to String Array Conversion in Java</ArticleName>
<Link>https://www.jiyik.com/howto/java/how-to-perform-string-to-string-array-conversion-in-java/</Link>
<ProgrammingLanguage>Java</ProgrammingLanguage>
<DateCreated>May-21, 2020</DateCreated>
</Tutorial>
<Tutorial Id="2">
<ArticleName>Compile a C++ Program Using GCC</ArticleName>
<Link>https://www.jiyik.com/howto/cpp/gcc-compile-cpp/</Link>
<ProgrammingLanguage>C++</ProgrammingLanguage>
<DateCreated>March-25, 2022</DateCreated>
</Tutorial>
<Tutorial Id="3">
<ArticleName>Python Tutorial - Introduction</ArticleName>
<Link>https://www.jiyik.com/tutorial/python-3-basic-tutorial/python-introduction/</Link>
<ProgrammingLanguage>Python</ProgrammingLanguage>
<DateCreated>January-29, 2018</DateCreated>
</Tutorial>
</Tutorials>
在文件中漂亮地打印 XML
要在文件中漂亮打印 XML,我们必须创建一个 XML 文件并使用漂亮打印方式在该文件中写入 XML。 参见示例:
package jiyik;
import java.io.File;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
public class XML_Pretty_Print {
public static void main(String[] args) {
DocumentBuilderFactory Doc_Builder_Factory = DocumentBuilderFactory.newInstance();
DocumentBuilder Document_Builder;
try {
Document_Builder = Doc_Builder_Factory.newDocumentBuilder();
Document document = Document_Builder.newDocument();
//adding elements to Document
Element Root_Element = document.createElementNS("https://www.jiyik.com/", "Tutorials");
document.appendChild(Root_Element);
//add a first child element to the root element
Root_Element.appendChild(Get_Tutorial(document, "1", "Perform String to String Array Conversion in Java",
"https://www.jiyik.com/howto/java/how-to-perform-string-to-string-array-conversion-in-java/",
"Java", "May-21, 2020"));
//add a second child
Root_Element.appendChild(Get_Tutorial(document, "2", "Compile a C++ Program Using GCC",
"https://www.jiyik.com/howto/cpp/gcc-compile-cpp/",
"C++", "March-25, 2022"));
//add third child
Root_Element.appendChild(Get_Tutorial(document, "3", "Python Tutorial - Introduction",
"https://www.jiyik.com/tutorial/python-3-basic-tutorial/python-introduction/",
"Python", "January-29, 2018"));
//create output
TransformerFactory Transformer_Factory = TransformerFactory.newInstance();
Transformer transformer = Transformer_Factory.newTransformer();
//open indent for the xml code (Pretty Print)
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
DOMSource Dom_Source = new DOMSource(document);
//Create a file and write XML to it
StreamResult XML_file = new StreamResult(new File("jiyik.xml"));
//write data
transformer.transform(Dom_Source, XML_file);
System.out.println("The XML file has been Generated");
} catch (Exception e) {
e.printStackTrace();
}
}
private static Node Get_Tutorial(Document document, String id, String article_name, String link, String programming_language, String date_created) {
Element tutorial = document.createElement("Tutorial");
//set the tutorial id attribute
tutorial.setAttribute("Id", id);
//create article name element
tutorial.appendChild(Get_Tutorial_Elements(document, tutorial, "ArticleName", article_name));
//create the Link element
tutorial.appendChild(Get_Tutorial_Elements(document, tutorial, "Link", link));
//create the Programming Language element
tutorial.appendChild(Get_Tutorial_Elements(document, tutorial, "ProgrammingLanguage", programming_language));
//create Date Created element
tutorial.appendChild(Get_Tutorial_Elements(document, tutorial, "DateCreated", date_created));
return tutorial;
}
//method to create text node
private static Node Get_Tutorial_Elements(Document document, Element element, String element_name, String element_value) {
Element element_node = document.createElement(element_name);
element_node.appendChild(document.createTextNode(element_value));
return element_node;
}
}
上面的代码将使用漂亮的打印方式将 XML 写入文件中。
输出:
The XML file has been Generated
相关文章
将 XML 解析为 Java 对象
发布时间:2023/08/02 浏览次数:177 分类:Java
-
本文将教我们如何用 Java 解析 XML 并将其转换为对象。将 XML 解析为 Java 对象 当谈论用 Java 解析 XML 文件时,Java 中提供了一些常见且最常用的解析器。
Java 的 Gitignore 文件
发布时间:2023/08/02 浏览次数:101 分类:Java
-
本文通过示例讨论 Java 的 .gitignore 文件,并列出了手动创建 .gitignore 时要遵循的规则。Java .gitignore 文件概述
在 Java 中创建 SSH 连接
发布时间:2023/08/02 浏览次数:91 分类:Java
-
本文讨论如何打开 SSH 连接,并演示如何使用 JSch 和 Apache Mina SSHD 创建 SSH 连接。在 Java 中创建 SSH 连接
Java 中的二进制数加法
发布时间:2023/08/02 浏览次数:65 分类:Java
-
本文介绍如何在 Java 中执行二进制加法。Java 中的二进制数加法 二进制数是 1 和 0 的组合,与算术数不同,相加的方式不同。 二进制数的加法规则为:
Java Spring Boot 中的Autowired
发布时间:2023/08/02 浏览次数:67 分类:Java
-
Spring Boot 是一个流行的 Java 框架。 在Spring Boot中,最常用的注解是@Autowired,主要用于自动收集bean。在这篇文章中,我们将讨论Java Spring Boot中的@Autowired。
Java 中的后缀表达式
发布时间:2023/08/02 浏览次数:136 分类:Java
-
后缀表达式比中缀表达式更容易计算并且速度更快,因为我们不需要处理或遵循任何运算符优先级规则。 此外,后缀表达式不包含任何括号。
Java 中的增量映射
发布时间:2023/08/02 浏览次数:100 分类:Java
-
在 Java 中使用 Map 或 HashMap 时,您可能需要增加该值。 在 Java 中,有很多方法可以增加 Map 的值。方法一:Java中Map值递增的通用方法
设置 java.util.Date 的时区
发布时间:2023/08/02 浏览次数:130 分类:Java
-
本文将介绍如何使用 Java 设置与日期关联的时区。 java.util.Date 没有时区。使用 ZonedDateTime 设置 java.util.Date 的时区