Java での XML のプリティ プリント
Sheeraz Gul
2024年2月15日
XML をきれいに印刷するということは、意図した方法で XML コードを印刷することを意味します。 このチュートリアルでは、コンソールと XML ファイルで XML をプリティプリントする方法を示します。
コンソールでの XML のプリティ プリント
以下の例は、XML を作成してコンソールに表示する方法を示しています。
package delftstack;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
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;
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.delftstack.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.delftstack.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.delftstack.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.delftstack.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.delftstack.com/">
<Tutorial Id="1">
<ArticleName>Perform String to String Array Conversion in Java</ArticleName>
<Link>https://www.delftstack.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.delftstack.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.delftstack.com/tutorial/python-3-basic-tutorial/python-introduction/</Link>
<ProgrammingLanguage>Python</ProgrammingLanguage>
<DateCreated>January-29, 2018</DateCreated>
</Tutorial>
</Tutorials>
ファイル内の XML のプリティ プリント
XML をファイルに整形印刷するには、XML ファイルを作成し、整形印刷方法を使用してそのファイルに XML を書き込む必要があります。 例を参照してください:
package delftstack;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
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;
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.delftstack.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.delftstack.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.delftstack.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.delftstack.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("delftstack.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
著者: Sheeraz Gul
Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.
LinkedIn Facebook