更新的xml数据未在xml文件中更新

时间:2014-01-27 12:40:04

标签: java xml dom

我已经使用GUI制作了一个在xml文件中更新我的xml的方法。 但是当我更新它时,一切似乎工作正常,控制台正在打印出正确的东西。 但是当我打开xml文件并按refrah时,没有更新。

我的问题是什么?

public void updateObjType(String newTxt, int x) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {
    System.out.println("String value : " + newTxt);
    System.out.println("Index value : " + x);

    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

    DocumentBuilder builder = builderFactory.newDocumentBuilder();

    Document xmlDocument = builder.parse("xmlFiles/CoreDatamodel.xml");

    XPath xPath = XPathFactory.newInstance().newXPath();

    // Go thru the Object_types in the XML file and get item x.
    NodeList nodeList = (NodeList) xPath.compile("//OBJECT_TYPE/text()")
                .evaluate(xmlDocument, XPathConstants.NODESET);

    // Set new NodeValue
    nodeList.item(x).setNodeValue(newTxt);
    String value = nodeList.item(x).getTextContent();

    System.out.println(value);
} 

这是控制台的输出:

Original data :  IF150Data
Incoming String value : Data
Index value : 4
updated data : Data

1 个答案:

答案 0 :(得分:0)

我用变压器解决了这个问题。

完整解决方案:

// Update the object type name from the object type list.
    public void updateObjType(String newTxt, int x)
            throws ParserConfigurationException, SAXException, IOException,
            XPathExpressionException {

        File file = new File("xmlFiles/CoreDatamodel.xml");
        System.out.println("Incoming String value : " + newTxt);
        System.out.println("Index value : " + x);

        DocumentBuilderFactory builderFactory = DocumentBuilderFactory
                .newInstance();

        DocumentBuilder builder = builderFactory.newDocumentBuilder();

        Document xmlDocument = builder.parse(file);

        XPath xPath = XPathFactory.newInstance().newXPath();

        NodeList nodeList = (NodeList) xPath.compile("//OBJECT_TYPE/text()")
                .evaluate(xmlDocument, XPathConstants.NODESET);
        // Set new NodeValue
        nodeList.item(x).setNodeValue(newTxt);

        // Save the new updates
        try {
            save(file, xmlDocument);
        } catch (Exception e) {

            e.printStackTrace();
        }
    }

然后我添加的方法:

public void save(File file, Document doc) throws Exception {

        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        StringWriter writer = new StringWriter();
        StreamResult result = new StreamResult(writer);
        DOMSource source = new DOMSource(doc);
        transformer.transform(source, result);
        String s = writer.toString();
        System.out.println(s);

        FileWriter fileWriter = new FileWriter(file);
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);

        bufferedWriter.write(s);
        bufferedWriter.flush();
        bufferedWriter.close();
    }