3

i have made a method for updating my xml in the xml file by a using a GUI.. but when I update it everything seem to be working fine and the console is printing out the correct things. But when I open the xml file and press refrah nothing is updated.

What is my problem?

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

this is the output from the console :

Original data :  IF150Data
Incoming String value : Data
Index value : 4
updated data : Data
wannaKnowItAll
  • 340
  • 1
  • 4
  • 20
Sembrano
  • 1,127
  • 4
  • 18
  • 28

1 Answers1

0

I solved it by using a transformer.

Full solution :

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

And then the method I added :

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();
    }
Sembrano
  • 1,127
  • 4
  • 18
  • 28