Save XML format to a string instead of a file [duplicate]

倾然丶 夕夏残阳落幕 提交于 2019-12-20 03:55:38

问题


I used the DocumentBuilderFactory to create XML file as done here: How to create XML file with specific structure in Java.

Instead of saving it to a file, I want to store the result as a java String. How to achieve that.

Duplicate: How do I convert a org.w3c.dom.Document object to a String?


回答1:


This example may help you out.

import java.io.StringWriter;
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.Document;
import org.w3c.dom.Element;

public class DOMBasicDoc {
    public static void main(String args[]) {
        try {
            String[] input = {"John Doe,123-456-7890", "Bob Smith,123-555-1212"};
            String[] line = new String[2];
            DocumentBuilderFactory dFact = DocumentBuilderFactory.newInstance();
            DocumentBuilder build = dFact.newDocumentBuilder();
            Document doc = build.newDocument();
            Element root = doc.createElement("root");
            doc.appendChild(root);
            Element memberList = doc.createElement("members");
            root.appendChild(memberList);
            for (int i = 0; i < input.length; i++) {
                line = input[i].split(",");
                Element member = doc.createElement("member");
                memberList.appendChild(member);
                Element name = doc.createElement("name");
                name.appendChild(doc.createTextNode(line[0]));
                member.appendChild(name);
                Element phone = doc.createElement("phone");
                phone.appendChild(doc.createTextNode(line[1]));
                member.appendChild(phone);
            }
            TransformerFactory tFact = TransformerFactory.newInstance();
            Transformer trans = tFact.newTransformer();

            StringWriter writer = new StringWriter();
            StreamResult result = new StreamResult(writer);
            DOMSource source = new DOMSource(doc);
            trans.transform(source, result);
            System.out.println(writer.toString());

        } catch (TransformerException ex) {
            System.out.println("Error outputting document");
        } catch (ParserConfigurationException ex) {
            System.out.println("Error building document");
        }
    }
}



回答2:


I advice you to read the Java documentation about I/O Streams. Specifically the characters stream section talking about Reader and Writer classes.

As you can see in the XMLOutputter JavaDoc the method output takes different parameters. You just have to chose the approriate Writer, instead of a FileWriter use a StringWriter for example.

However, the XMLOutputter also have outputString() methods that let you produce a String directly doing that job internally (probably).



来源:https://stackoverflow.com/questions/36174793/save-xml-format-to-a-string-instead-of-a-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!