0

I ma trying to reset the value of a node but for some reason the change is not reflecting. I get the tag through XPATH but it doesn't set the value I give. reqXML is the XML file

My Code

public static String changeProductVersion(String reqXML) {

    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        Document document = null;
        DocumentBuilder builder = factory.newDocumentBuilder();
        InputSource source = new InputSource();
        source.setCharacterStream(new StringReader(reqXML));
        document = builder.parse(source);

        XPath xPath = XPathFactory.newInstance().newXPath();
        Element element = (Element) xPath.evaluate(NYPG3Constants.NY_PG3_RL, document, XPathConstants.NODE);

        if(element != null) {
            element.setTextContent("17.1.0");
            System.out.println(element.getTextContent());
        }

    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return reqXML;
}

Thanks in advance

2
  • What is the structure of the XML file and what is the value of NYPG3Constants.NY_PG3_RL constant? Are you just setting the value of the node in the code or do you want to save it in the file? If you want to save it, there are no instructions for it in this code. Commented Mar 12, 2018 at 22:45
  • This is my XPATh and I want to set 17.1.0 instead of 14.0.0 Commented Mar 12, 2018 at 22:46

1 Answer 1

1

I had to make some assumptions and changes because I don't know what your XML document looks like. But, the Element class extends Node and the setTextContent is a method on Node. What you need to update is the first child node, which is typically the text value of the element, but you should add some validation to make sure. Then, once you've updated the text value, you need to serialise the DOM back to whatever form it came from:

public static String changeProductVersion(String reqXML, String xpathExpression) {

    Document document = null;
    String updatedXML = null;

    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(reqXML));
        document = builder.parse(is);

        XPath xPath = XPathFactory.newInstance().newXPath();
        Element element = (Element) xPath
                .compile(xpathExpression)
                .evaluate(document, XPathConstants.NODE);

        if(element != null) {
            NodeList childNodes = element.getChildNodes();

            // Get the first node which should be the text value.
            // Add some validation to make sure Node == Node.TEXT_NODE.
            Node node = (Node) childNodes.item(0);
            node.setTextContent("17.1.0");
        }

        System.out.println("Updated element value: " + element.getTextContent());

        // Serialise the updated DOM
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(document);
        StreamResult result = new StreamResult(new StringWriter());
        transformer.transform(source, result);

        updatedXML = result.getWriter().toString();

    } catch (Exception ex) {
        ex.printStackTrace();
    }

    System.out.println(updatedXML);
    return updatedXML;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yup it does what I am looking for, thank you so much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.