- Java XML 教程
- Java XML 主页
- Java XML 概述
- Java XML 解析器
- Java DOM 解析器
- Java DOM 解析器
- 解析XML文档
- 查询XML文档
- 创建 XML 文档
- 修改XML文档
- Java SAX 解析器
- Java SAX 解析器
- 解析XML文档
- 查询XML文档
- 创建 XML 文档
- 修改XML文档
- JDOM XML 解析器
- JDOM XML 解析器
- 解析XML文档
- 查询XML文档
- 创建 XML 文档
- 修改XML文档
- Java StAX 解析器
- Java StAX 解析器
- 解析XML文档
- 查询XML文档
- 创建 XML 文档
- 修改XML文档
- Java XPath 解析器
- Java XPath 解析器
- 解析XML文档
- 查询XML文档
- 创建 XML 文档
- 修改XML文档
- Java DOM4J 解析器
- Java DOM4J 解析器
- 解析XML文档
- 查询XML文档
- 创建 XML 文档
- 修改XML文档
- Java XML 有用资源
- Java XML - 问题与解答
- Java XML - 快速指南
- Java XML - 有用的资源
- Java XML - 讨论
Java DOM 解析器 - 创建 XML 文档
演示示例
这是我们需要创建的 XML -
<?xml version = "1.0" encoding = "UTF-8" standalone = "no"?> <cars> <supercars company = "Ferrari"> <carname type = "formula one">Ferrari 101</carname> <carname type = "sports">Ferrari 202</carname> </supercars> </cars>
CreateXmlFileDemo.java
package com.tutorialspoint.xml; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder; 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.Attr; import org.w3c.dom.Document; import org.w3c.dom.Element; import java.io.File; public class CreateXmlFileDemo { public static void main(String argv[]) { try { DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.newDocument(); // root element Element rootElement = doc.createElement("cars"); doc.appendChild(rootElement); // supercars element Element supercar = doc.createElement("supercars"); rootElement.appendChild(supercar); // setting attribute to element Attr attr = doc.createAttribute("company"); attr.setValue("Ferrari"); supercar.setAttributeNode(attr); // carname element Element carname = doc.createElement("carname"); Attr attrType = doc.createAttribute("type"); attrType.setValue("formula one"); carname.setAttributeNode(attrType); carname.appendChild(doc.createTextNode("Ferrari 101")); supercar.appendChild(carname); Element carname1 = doc.createElement("carname"); Attr attrType1 = doc.createAttribute("type"); attrType1.setValue("sports"); carname1.setAttributeNode(attrType1); carname1.appendChild(doc.createTextNode("Ferrari 202")); supercar.appendChild(carname1); // write the content into xml file TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(new File("C:\\cars.xml")); transformer.transform(source, result); // Output to console for testing StreamResult consoleResult = new StreamResult(System.out); transformer.transform(source, consoleResult); } catch (Exception e) { e.printStackTrace(); } } }
这将产生以下结果 -
<?xml version = "1.0" encoding = "UTF-8" standalone = "no"?> <cars> <supercars company = "Ferrari"> <carname type = "formula one">Ferrari 101</carname> <carname type = "sports">Ferrari 202</carname> </supercars> </cars>