用Java编写XML文件到计算机

时间:2012-10-12 05:10:16

标签: java xml

所以我有以下代码,我想创建一个XML文件并将其本地存储在我的计算机上,但是每当我执行代码时,它似乎都没有保存文件,有人可以帮助我吗?

public void CreateXMLPoll (Poll p) {

        try {
            // Initialize the XML builder
            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder;
            docBuilder = docFactory.newDocumentBuilder();
            Document doc = docBuilder.newDocument();

            // Set the Elements Up
            Element rootElement = doc.createElement("Poll");
            Element creator = doc.createElement("Creator");
            Element name = doc.createElement("Name");
            Element email = doc.createElement("Email");
            Element title = doc.createElement("Title");
            Element location = doc.createElement("Location");
            Element description = doc.createElement("Description");
            Element date = doc.createElement("Date");
            Element time = doc.createElement("Time");

            // Create XML tree
            rootElement.appendChild(creator);
            rootElement.appendChild(title);
            rootElement.appendChild(location);
            rootElement.appendChild(description);
            rootElement.appendChild(date);
            creator.appendChild(name);
            creator.appendChild(email);
            date.appendChild(time);

            // Add values to XML
            name.appendChild(doc.createTextNode(p.getUsername()));
            email.appendChild(doc.createTextNode(p.getEmail()));
            title.appendChild(doc.createTextNode(p.getTitle()));
            location.appendChild(doc.createTextNode(p.getLocation()));
            description.appendChild(doc.createTextNode(p.getDescription()));

            // Appends a bunch of dates
            for (PollDates pd : p.getDates()) {
                time.appendChild(doc.createTextNode(pd.getFirst().toString()));
                if (pd.getSecond() != null) time.appendChild(doc.createTextNode(pd.getSecond().toString()));
                if (pd.getThird() != null) time.appendChild(doc.createTextNode(pd.getThird().toString()));
            }

            // write the content into xml file
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            try {
                Transformer transformer = transformerFactory.newTransformer();
            } catch (TransformerConfigurationException e) {
                e.printStackTrace();
            }
            FileWriter fstream = new FileWriter("test.xml");
            BufferedWriter out = new BufferedWriter(fstream);

            DOMSource source = new DOMSource(doc);
            StreamResult result = new StreamResult(new File("C:/Users/testing.xml"));

        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

我关注了here的教程。

2 个答案:

答案 0 :(得分:3)

在这一行之后,

BufferedWriter out = new BufferedWriter(fstream);

您需要声明为:

out.write("content to write in the file");

写文件。

要获取内容,您错过了教程中的内容:

StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
DOMSource source = new DOMSource(doc);
trans.transform(source, result);
String xmlString = sw.toString();

获得xmlString后,使用以下命令在文件中写入

out.write(xmlString,0, xmlString.length);

完成后,您需要添加

out.close();

关闭流。

更新程序并尝试。

答案 1 :(得分:2)

我建议你使用JAXB

您可以使用JAXB:

  • 从XML架构生成JAXB Java类
  • 使用模式派生的JAXB类来解组和编组XML内容 在Java应用程序中
  • 使用架构派生的JAXB类创建Java内容树
  • 在解组和运行时验证XML内容
  • 自定义JAXB架构到Java绑定

检查此示例

http://www.vogella.com/articles/JAXB/article.html

http://www.mkyong.com/java/jaxb-hello-world-example/

更容易,更可爱的代码,面向对象的方法。