更改节点后如何更新XML文件? [英] How do you update an XML file after you change a node?

查看:144
本文介绍了更改节点后如何更新XML文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Node.setTextContent()来编辑节点,但它没有改变文件中的任何内容。如果我打印文本内容后它将显示为已更改但在程序关闭后它将不会保留。

I'm using Node.setTextContent() to edit a node but it's not changing anything in the file. If I print the text content after it will appear as changed but it will not persist after the program is closed.

    for (int y=0; y<calendarDataNode.getChildNodes().getLength(); y++) {
        //if (year node name == "y" + current year)
        if (calendarDataNode.getChildNodes().item(y).getNodeName().equals("y" + Main.year)) {
            //for (int m=0; m<number of child nodes of year node; m++)
            for (int m=0; m<calendarDataNode.getChildNodes().item(y).getChildNodes().getLength(); m++) {
                //if (month node name == "m" + current month)
                if (calendarDataNode.getChildNodes().item(y).getChildNodes().item(m).getNodeName().equals("m" + (Main.month_index-1))) {
                    //for (int d=0; d<number of child nodes of month node; d++)
                    for (int d=0; d<calendarDataNode.getChildNodes().item(y).getChildNodes().item(m).getChildNodes().getLength(); d++) {
                        //label node
                        node = calendarDataNode.getChildNodes().item(y).getChildNodes().item(m).getChildNodes().item(d).getChildNodes().item(0);
                        node.setTextContent(tf_label.getText());

                    }    
                }
            }
        }
    }

    try (FileOutputStream outStream = new FileOutputStream("Calendar.xml")) {
        Transformer tf = TransformerFactory.newInstance().newTransformer();
        tf.setOutputProperty(OutputKeys.INDENT, "yes");
        tf.setOutputProperty(OutputKeys.METHOD, "xml");
        tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");


        DOMSource domSource = new DOMSource(xmlDoc);
        StreamResult sr = new StreamResult(outStream );
        tf.transform(domSource, sr);
    } catch (TransformerException | IOException e) {e.printStackTrace(System.out);}


推荐答案

替换...

OutputFormat outFormat = new OutputFormat(xmlDoc);
try (FileOutputStream outStream = new FileOutputStream("src/virtualagenda/Calendar.xml")) {
    XMLSerializer serializer = new XMLSerializer(outStream, outFormat);
    serializer.serialize(xmlDoc);

    outStream.close();
}catch(IOException e) {e.printStackTrace(System.out);}

更像是......

Transformer tf = TransformerFactory.newInstance().newTransformer();
tf.setOutputProperty(OutputKeys.INDENT, "yes");
tf.setOutputProperty(OutputKeys.METHOD, "xml");
tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

try (FileOutputStream outStream = new FileOutputStream("Calendar.xml")) {
    DOMSource domSource = new DOMSource(document);
    StreamResult sr = new StreamResult(outStream );
    tf.transform(domSource, sr);
} catch (TransformerConfigurationException, TransformerException exp) {
    exp.printStackTrace();
}

使用runnable示例更新

所以使用......

So using...

<?xml version="1.0" encoding="UTF-8"?>
<fruit>
    <banana>yellow</banana>
    <orange>orange</orange>
    <pear>yellow</pear>    
</fruit>

然后使用...

try {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();

    Document document = builder.parse(new File("Calendar.xml"));

    NodeList nodeList = document.getDocumentElement().getChildNodes();
    for (int index = 0; index < nodeList.getLength(); index++) {
        Node node = nodeList.item(index);
        if (node.getNodeType() != Node.TEXT_NODE) {
            node.setTextContent("Some text");
        }
    }

    Transformer tf = TransformerFactory.newInstance().newTransformer();
    tf.setOutputProperty(OutputKeys.INDENT, "yes");
    tf.setOutputProperty(OutputKeys.METHOD, "xml");
    tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

    try (FileOutputStream os = new FileOutputStream(new File("Calendar.xml"))) {

        DOMSource domSource = new DOMSource(document);
        StreamResult sr = new StreamResult(os);
        tf.transform(domSource, sr);

    }

} catch (SAXException | TransformerException | IOException | ParserConfigurationException ex) {
    ex.printStackTrace();
}

输出......

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<fruit>
    <banana>Some text</banana>
    <orange>Some text</orange>
    <pear>Some text</pear>    
</fruit>

转换代码有效,您的代码中还有其他内容,您没有向我们展示哪些是工作......

The transformation code works, there is something else within your code which you're not showing us which isn't working...

这篇关于更改节点后如何更新XML文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆