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 StAX Parser - Create XML Document
Demo Example
Here is the XML that we need to create −
<?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>
StAXCreateXMLDemo.java
package com.tutorialspoint.xml; import java.io.IOException; import java.io.StringWriter; import javax.xml.stream.XMLOutputFactory; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamWriter; pubpc class StAXCreateXMLDemo { pubpc static void main(String[] args) { try { StringWriter stringWriter = new StringWriter(); XMLOutputFactory xMLOutputFactory = XMLOutputFactory.newInstance(); XMLStreamWriter xMLStreamWriter = xMLOutputFactory.createXMLStreamWriter(stringWriter); xMLStreamWriter.writeStartDocument(); xMLStreamWriter.writeStartElement("cars"); xMLStreamWriter.writeStartElement("supercars"); xMLStreamWriter.writeAttribute("company", "Ferrari"); xMLStreamWriter.writeStartElement("carname"); xMLStreamWriter.writeAttribute("type", "formula one"); xMLStreamWriter.writeCharacters("Ferrari 101"); xMLStreamWriter.writeEndElement(); xMLStreamWriter.writeStartElement("carname"); xMLStreamWriter.writeAttribute("type", "sports"); xMLStreamWriter.writeCharacters("Ferrari 202"); xMLStreamWriter.writeEndElement(); xMLStreamWriter.writeEndElement(); xMLStreamWriter.writeEndDocument(); xMLStreamWriter.flush(); xMLStreamWriter.close(); String xmlString = stringWriter.getBuffer().toString(); stringWriter.close(); System.out.println(xmlString); } catch (XMLStreamException e) { e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
This would produce the following result −
<?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>Advertisements