Java XML Tutorial
Selected Reading
- Java XML - Discussion
- Java XML - Useful Resources
- Java XML - Quick Guide
- Java XML - Questions and Answers
- Modify XML Document
- Create XML Document
- Query XML Document
- Parse XML Document
- Java DOM4J Parser
- Modify XML Document
- Create XML Document
- Query XML Document
- Parse XML Document
- Java XPath Parser
- Modify XML Document
- Create XML Document
- Query XML Document
- Parse XML Document
- Java StAX Parser
- Modify XML Document
- Create XML Document
- Query XML Document
- Parse XML Document
- JDOM XML Parser
- Modify XML Document
- Create XML Document
- Query XML Document
- Parse XML Document
- Java SAX Parser
- Modify XML Document
- Create XML Document
- Query XML Document
- Parse XML Document
- Java DOM Parser
- Java XML Parsers
- Java XML Overview
- Java XML Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Create XML Document
Java DOM4J Parser - Create XML Document
Demo Example
在这方面,我们需要建立——
<?xml version = "1.0" encoding = "UTF-8"?> <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 java.io.IOException; import java.io.UnsupportedEncodingException; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.XMLWriter; pubpc class DOM4JCreateXMLDemo { pubpc static void main(String[] args) { try { Document document = DocumentHelper.createDocument(); Element root = document.addElement( "cars" ); Element supercarElement = root.addElement("supercars") .addAttribute("company", "Ferrai"); supercarElement.addElement("carname") .addAttribute("type", "Ferrari 101") .addText("Ferrari 101"); supercarElement.addElement("carname") .addAttribute("type", "sports") .addText("Ferrari 202"); // Pretty print the document to System.out OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter writer; writer = new XMLWriter( System.out, format ); writer.write( document ); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
这将产生以下结果:
<?xml version = "1.0" encoding = "UTF-8"?> <cars> <supercars company = "Ferrari"> <carname type = "formula one">Ferrari 101</carname> <carname type = "sports">Ferrari 202</carname> </supercars> </cars>Advertisements