首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java将具有属性的元素添加到xml文件

Java将具有属性的元素添加到xml文件
EN

Stack Overflow用户
提问于 2013-04-22 18:34:42
回答 1查看 3.5K关注 0票数 1

程序员朋友们好:)。

我对Java非常陌生,如果这是PHP,我已经这样做了:P,无论如何我试着在这里搜索所有的答案(stackoverflow),但没有适合我的特定问题,或者我没有看到一个例子或我错过了something...anyway请张贴到答案的链接,如果你知道任何类似我的问题的解决方案。

对问题没问题;)

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

代码语言:javascript
复制
<?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>

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

代码语言:javascript
复制
        String FilePath = "D:/myxml.xml";
    Document doc = openXMLfile(FilePath);

它已经加载了,这里没问题..

其中,openXMLfile是:

代码语言:javascript
复制
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;
}

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

代码语言:javascript
复制
File file = new File("D:\myxml.xml");
Element event = doc.createElement("event");
    event.setAttribute("test","testvalue");
    doc.getDocumentElement().appendChild(event);

    filePutContents(doc,file);

在filePutContents中,我有这个函数:

代码语言:javascript
复制
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();
    }
}

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

问题解决了,我已经反映了代码中的变化;)感谢做帮助者:)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-04-22 18:39:45

代码语言:javascript
复制
StreamResult result = new StreamResult(System.out); 

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

试试这个-

代码语言:javascript
复制
StreamResult result = new StreamResult(new FileOutputStream("somefile.xml"));
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16144962

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档