微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java-更改节点后如何更新XML文件?

该问题与以下内容完全相同:            >            How do you instantly update an XML document after you add a node to it?                                    1个
我正在使用Node.setTextContent()编辑节点,但它没有更改文件中的任何内容.如果我在打印后显示文本内容,它将显示为已更改,但在程序关闭后将不会保留.

    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();
}

更新了可运行示例

所以使用…

<?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>

转换代码有效,您的代码中还有其他内容,您没有向我们展示这是行不通的…

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。