写入xml(XMLWriter )

当我们写入xml时(就是把我们的数据写入到xml对应的标签里):
1.建立一个text.xml
<?xml version="1.0" encoding="UTF-8"?>
<Books>
    <Book>
        <Price></Price>
        <Title></Title>
    </Book>
</Books>
2.
package xml;  
 
import java.io.BufferedReader;
import java.io.BufferedWriter;  
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;  
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;  
 
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;  
import org.xml.sax.SAXException;
 
public class XMLWriter {  
    private String fileName;  
    private Document document;  
 
    public XMLWriter(String fileName) throws Exception {  
        this.fileName = fileName;  
        createXmlDocument();  
    }  
 
    private void createXmlDocument() throws Exception {  
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();  
        DocumentBuilder builder = factory.newDocumentBuilder();  
        document = builder.newDocument();  
    }  
 
    public Element createDocumentElement(String name) {  
        Element root = document.createElement(name);  
        document.appendChild(root);  
        return root;  
    }  
 
    public Element createElement(Element parent, String tagName, String text) {  
        Element element = document.createElement(tagName);  
        if (text != null && text.length() > 0) {  
            element.setTextContent(text);  
        }
        parent.appendChild(element);  
        return element;  
    }  
 
    public void toSave() throws Exception {  
        TransformerFactory factory = TransformerFactory.newInstance();  
        factory.setAttribute("indent-number", new Integer(4));// 设置缩进长度为4  
        Transformer transformer = factory.newTransformer();  
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");// 设置自动换行  
        DOMSource source = new DOMSource(document);  
        transformer.transform(source, new StreamResult(new BufferedWriter(  
                new OutputStreamWriter(new FileOutputStream(fileName), "UTF-8"))));  
    }  
 
    public static void main(String[] args) {  
        String fileName = "E:\\test.xml";  
        System.out.println(fileName);  
        try {  
            XMLWriter writer = new XMLWriter(fileName);  
            Element root = writer.createDocumentElement("Books");  
            Element book = writer.createElement(root, "Book", "");  
            writer.createElement(book, "Price", "52.00");  
            Element title = writer.createElement(book,"Title","test.xml");  
            title.setAttribute("Auth", "yy");  
            writer.toSave();
        } catch (Exception e) {  
            e.printStackTrace();  
        }
        byte[] bytes ;
        File file = new File("E:\\test.xml");
        if (file.exists()) {
        if (file.isFile()) {
        try {
        BufferedReader input = new BufferedReader(new FileReader(file));
        StringBuffer buffer = new StringBuffer();
        String text;

        while ((text = input.readLine()) != null)
        buffer.append(text + "\n");
       
        System.err.println("===="+buffer.toString());
        bytes = buffer.toString().getBytes();
        } catch (IOException ioException) {
        System.err.println("File Error!");

        }
        }
        }
        //这样就有byte[]数组了,你就可以返回了


       
    }  

你可能感兴趣的:(Writer)