使用换行符创建XML文件

时间:2014-04-01 15:22:58

标签: java xml line break

我参考了以下教程,使用JAVA创建XML文件。 http://www.mkyong.com/java/how-to-create-xml-file-in-java-dom/

但是,当我打开xml文件时,它会显示在1个极长的单行中。这使得我很难验证结果,并且我确信可以为每个元素标记设置换行符。 但我没有任何线索如何做到这一点。请指教。 以下是我的代码:

 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ir;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
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.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

//##############################################################################################################
public class IR {

    public static void main(String[] args) {

        IR obj = new IR();
    obj.Reader();


    }//end main

//##############################################################################################################  
    public void Reader() {

    String csvFile = "TWEETS.csv";
    BufferedReader br = null;
    String line = "";
    String cvsSplitBy = ",";
        String[] heading = {};          //The headings (E.g. ID, Message, Link)
        String[] content = {};         
        int counter = 0;                //Num of columns
        boolean isheading = true;       //First line is heading

    try 
        {
            //##############################################################################################################
            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

            // <add> elements
            Document doc = docBuilder.newDocument();
            Element rootElement = doc.createElement("add");
            doc.appendChild(rootElement);
            //##############################################################################################################

            br = new BufferedReader(new FileReader(csvFile));
            while ((line = br.readLine()) != null) 
            {

                    //The first line is heading
                    if(isheading)
                    {
                        //The headings (E.g. ID, Message, Link)
                        heading = line.split(cvsSplitBy);
                        counter = heading.length;       //get number of columns
                        isheading = false;              
                        continue;                       //Go to next loop
                    }else{  //2nd line onwards
                        content = line.split(cvsSplitBy);
                        System.out.println("SG News "
                            + "[ID= " + content[0] 
                            + " , FULL_MESSAGE=" + content[1] + "]");
                    }

                    //##############################################################################################################
                    // <doc> elements
                    Element docEle = doc.createElement("doc");
                    rootElement.appendChild(docEle);                        

                        //Loop according to number of columns
                        for(int i=0; i<counter; i++)
                        {
                            // <field> elements
                            Element fieldEle = doc.createElement("field");
                            rootElement.appendChild(fieldEle);

                            // set attribute to <field> element
                            Attr attr = doc.createAttribute("name");
                            attr.setValue(heading[i]);
                            fieldEle.setAttributeNode(attr);
                            fieldEle.appendChild(doc.createTextNode(content[i]));
                        }
                    //##############################################################################################################

            }
            //##############################################################################################################
            // write the content into xml file
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(doc);
            StreamResult result = new StreamResult(new File("TWEETs_solr.xml"));

            // Output to console for testing
            transformer.transform(source, result);

            System.out.println("File saved!");
            //##############################################################################################################
    }catch (ParserConfigurationException pce) {
        pce.printStackTrace();
    }catch (TransformerException tfe) {
        tfe.printStackTrace();
    }catch (FileNotFoundException e) {
        e.printStackTrace();
    }catch (IOException e) {
        e.printStackTrace();
    }finally {
            if (br != null) 
            {
                try {
                        br.close();
                } catch (IOException e) {
                        e.printStackTrace();
                }
            }
    }
    System.out.println("Done"); 

  }//end Reader
 //##############################################################################################################


}//end class

1 个答案:

答案 0 :(得分:12)

您需要向变换器对象添加属性。 像这样:

Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");

它应该可以解决问题。