package com.imooc.learn3;



import java.io.File;



import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.parsers.ParserConfigurationException;









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;



public class createXML {

public DocumentBuilder getDocumentBuilder() throws ParserConfigurationException{

DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();

DocumentBuilder builder=factory.newDocumentBuilder();

return builder;



}


public void createXML() throws ParserConfigurationException{

DocumentBuilder builder=getDocumentBuilder();

Document document=builder.newDocument();

Element bookstore=document.createElement("bookstore");

Element book=document.createElement("book");

book.setAttribute("ID", "4");

bookstore.appendChild(book);

document.appendChild(bookstore);

TransformerFactory tff=TransformerFactory.newInstance();

try {

Transformer fs=tff.newTransformer();

fs.setOutputProperty(OutputKeys.INDENT, "yes");

fs.transform(new DOMSource(document), new StreamResult("demo/books1.xml"));

} catch (Exception e) {

e.printStackTrace();

// TODO: handle exception

}



}


public static void main(String[] args) {


createXML p=new createXML();

try {

p.createXML();

} catch (ParserConfigurationException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}



}