Java向xml文件添加具有属性的元素

标签 java xml file fullcalendar

各位程序员大家好:)。

我对 Java 很陌生,如果这是 PHP,我已经这样做了:P,无论如何,我尝试在这里搜索所有答案(stackoverflow),但没有一个适合我的特定问题,或者我没有看到示例,或者我错过了一些东西......无论如何,如果您知道与我的问题类似的解决方案,请发布答案链接。

问题解决了;)

我在一个文件中有这个 XML,我需要它像这样:

<?xml version="1.0" encoding="UTF-8"?>
<events>
    <event id="46" title="Ferias" start="2013-04-25" end="2013-04-26" allDay="false" editable="true"/>
    <event id="47" title="Falta" start="Wed Apr 17 2013 00:00:00 GMT+0100" end="Thu Apr 18 2013 00:00:00 GMT+0100" allDay="false" editable="true"/>
    <event id="48" title="Tolerancia de Ponto" start="Mon Apr 01 2013 00:00:00 GMT+0100" end="" allDay="false" editable="true"/>    
    <event id="49" title="Titulo teste" start="Thu Apr 11 2013 00:00:00 GMT+0100" end="Sat Apr 13 2013 00:00:00 GMT+0100" allDay="true" editable="true"/>
    <event id="50" title="dfgfdgf" start="Fri Apr 12 2013 00:00:00 GMT+0100" end="Sat Apr 13 2013 00:00:00 GMT+0100" allDay="true" editable="true"/>
    <event id="51" title="hghfjfghj" start="Tue Apr 16 2013 00:00:00 GMT+0100" end="Wed Apr 17 2013 00:00:00 GMT+0100" allDay="true" editable="true"/>
    <event id="52" title="grande evento" start="Tue Apr 23 2013 00:00:00 GMT+0100" end="Wed May 01 2013 00:00:00 GMT+0100" allDay="true" editable="true"/>
</events>

我正在尝试添加一个带有属性的新元素“事件”,并且我正在使用它来加载和修改:

        String FilePath = "D:/myxml.xml";
    Document doc = openXMLfile(FilePath);

这里加载没有问题..

其中 openXMLfile 是这样的:

protected Document openXMLfile(String filepath){
    Document doc = null;
    try {
        File fXmlFile = new File(filepath);
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        doc = dBuilder.parse(fXmlFile);         
    } catch (Exception e) {
        e.printStackTrace();
    }
    return doc;
}

然后在我的添加元素的函数中我这样做:

File file = new File("D:\myxml.xml");
Element event = doc.createElement("event");
    event.setAttribute("test","testvalue");
    doc.getDocumentElement().appendChild(event);

    filePutContents(doc,file);

其中 filePutContents 我有这个功能:

protected void filePutContents(Document doc,File file){
    try{                    
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer();    
        transformer.setOutputProperty(OutputKeys.INDENT,"yes");

        DOMSource source = new DOMSource(doc);
        //StreamResult result = new StreamResult(System.out);//problem was this
        StreamResult result = new StreamResult(file);//correct way
                transformer.transform(source, result);

    }catch(TransformerConfigurationException tce){
        /*ERRO do Transformer*/         
        System.out.println("* Transformer Factory error");
        System.out.println(" " + tce.getMessage());

        Throwable x = tce;
        if (tce.getException() != null)
            x = tce.getException();
            x.printStackTrace(); 
    }catch(TransformerException te){
        /*ERRO da Factory*/
        System.out.println("* Transformation error");
        System.out.println(" " + te.getMessage());

        Throwable x = te;
        if (te.getException() != null)
            x = te.getException();
            x.printStackTrace();
    }
}

问题是,没有元素添加到文件中,我想要更新文件,我做错了什么?谢谢

问题已解决,我已经反射(reflect)了代码中的更改;)感谢帮助者:)

最佳答案

StreamResult result = new StreamResult(System.out); 

上面的语句将输出定向到控制台 (System.out)。如果您想要更新文件,请将其指向该文件。

试试这个-

StreamResult result = new StreamResult(new FileOutputStream("somefile.xml"));

关于Java向xml文件添加具有属性的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16144962/

相关文章:

C打开并读取txt文件

java - 执行查询未定义

java - 使用 gradlew 时如何安装缺少的 jenkins 插件?

java - Android 中的 HSVToColor 有点奇怪

android - 根据 ImageButton drawable 调整按钮宽度

css - 使用XSL-FO,CSS3而不是CSS2创建分页文档(如PDF)?

file - 如何避免 doxygen 基于自动文件夹的命名空间?

c# - 写回解码文件后出现异常字符添加

java - Java打印InternalError实际错误

java - 如何使用 XSLT 和 Java 而不是 XSLFO 创建 PDF?