用Java编写XML文件

时间:2010-03-16 09:08:51

标签: java xml

我正在处理以下代码:

import java.io.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import org.w3c.dom.*;

public class CreatXMLFile {
  public static void main(String[] args) throws Exception {
    BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
//    System.out.print("Enter number to add elements in your XML file: ");
//    String str = bf.readLine();
    int no=2;
//    System.out.print("Enter root: ");
    String root = "SMS";
    DocumentBuilderFactory documentBuilderFactory =DocumentBuilderFactory.newInstance();

        DocumentBuilder documentBuilder =documentBuilderFactory.newDocumentBuilder();

        Document document = documentBuilder.newDocument();

    Element rootElement = document.createElement(root);

        document.appendChild(rootElement);
//    for (int i = 1; i <= no; i++)
//      System.out.print("Enter the element: ");
//      String element = bf.readLine();
      String element ="Number";
      System.out.print("Enter the Number: ");
      String data = bf.readLine();
      Element em = document.createElement(element);
      em.appendChild(document.createTextNode(data));
      rootElement.appendChild(em);

      String element1 ="message";
      System.out.print("Enter the SMS: ");
      String data1 = bf.readLine();
      Element em1 = document.createElement(element1);
      em1.appendChild(document.createTextNode(data1));
      rootElement.appendChild(em1);

    TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(document);
        StreamResult result =  new StreamResult(System.out);
        transformer.transform(source, result);

  }
}

它提供以下输出:

run:
Enter the Number: 768678
Enter the SMS: ytu
<?xml version="1.0" encoding="UTF-8" standalone="no"?><SMS><Number>768678</Number><message>ytu</message></SMS>BUILD SUCCESSFUL (total time: 8 seconds)

现在我想将生成的输出(<?xml version="1.0" encoding="UTF-8" standalone="no"?><SMS><Number>768678</Number><message>ytu</message></SMS>)写入硬盘上的XML文件。我该怎么做?

2 个答案:

答案 0 :(得分:14)

使用FileOutputStream(或File)代替System.out来构建StreamResult

答案 1 :(得分:6)

以下是此问题的完整解决方案:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringWriter;

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.TransformerException;
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 CreatXml {

    /**
     * @param args
     * @throws TransformerException 
     * @throws ParserConfigurationException 
     * @throws IOException 
     */
    public static void main(String[] args) throws TransformerException, ParserConfigurationException, IOException {
        // TODO Auto-generated method stub
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
//      System.out.print("Enter number to add elements in your XML file: ");
//      String str = bf.readLine();
        int no=2;
//      System.out.print("Enter root: ");
        String root = "SMS";
        DocumentBuilderFactory documentBuilderFactory =DocumentBuilderFactory.newInstance();

            DocumentBuilder documentBuilder =documentBuilderFactory.newDocumentBuilder();

            Document document = documentBuilder.newDocument();

        Element rootElement = document.createElement(root);

            document.appendChild(rootElement);
//      for (int i = 1; i <= no; i++)
//        System.out.print("Enter the element: ");
//        String element = bf.readLine();
          String element ="Number";
          System.out.print("Enter the Number: ");
          String data = bf.readLine();
          Element em = document.createElement(element);
          em.appendChild(document.createTextNode(data));
          rootElement.appendChild(em);

          String element1 ="message";
          System.out.print("Enter the SMS: ");
          String data1 = bf.readLine();
          Element em1 = document.createElement(element1);
          em1.appendChild(document.createTextNode(data1));
          rootElement.appendChild(em1);

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(document);

            StreamResult result =  new StreamResult(new StringWriter());

          //t.setParameter(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "5");
            transformer.transform(source, result);

          //writing to file
          FileOutputStream fop = null;
          File file;
          try {

                file = new File("/home/zurelsoft/Desktop/newfile1.txt");
                fop = new FileOutputStream(file);

                // if file doesnt exists, then create it
                if (!file.exists()) {
                    file.createNewFile();
                }

                // get the content in bytes
                String xmlString = result.getWriter().toString();
                System.out.println(xmlString);
                byte[] contentInBytes = xmlString.getBytes();

                fop.write(contentInBytes);
                fop.flush();
                fop.close();

                System.out.println("Done");

            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (fop != null) {
                        fop.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

    }

}