2

From the given XML String, i have to update End Date value . Even though I'm updating the xml in updateNodeValue() method, my final output xml is same as the input xml.

Can someone tell me what is the mistake in this code

import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;


public class MyClass{

    static String strXml = "<INFO><BeginDate>2013-12-02</BeginDate><EndDate>2014-01-31</EndDate></INFO>";

    public static void main(String[] args) throws Exception {

        System.out.println(strXml);
        Document doc = StringToDocument(strXml);
        updateNodeValue(doc);
        String newxml = DocumentToString(doc);
        System.out.println(newxml);

    }

    public static void updateNodeValue(Document doc) {

        Node rootNode = doc.getFirstChild();
        NodeList list = rootNode.getChildNodes();
        for (int i = 0; i < list.getLength(); i++) {

            Element element = (Element) list.item(i);
            Node node = list.item(i);
            if ("EndDate".equals(node.getNodeName())) {
                element.setNodeValue("2013-12-12");
                return;
            }
        }
    }

    public static String DocumentToString(Document doc) throws Exception {

        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(doc), new StreamResult(writer));
        String output = writer.getBuffer().toString();
        return output;
    }

    public static Document StringToDocument(String strXml) throws Exception {

        Document doc = null;
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            StringReader strReader = new StringReader(strXml);
            InputSource is = new InputSource(strReader);
            doc = (Document) builder.parse(is);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }

        return doc;
    }
}
1
  • Initially this change worked, and once i imported this piece of code to my actual project I'm getting this error "The method setTextContent(String) is undefined for the type Element" Commented Dec 3, 2013 at 15:28

2 Answers 2

4

Useelement.setTextContent(...) in your updateNodeValue method.

Sign up to request clarification or add additional context in comments.

Comments

1

The method you should use is not setNodeValue() but setTextContent()

See http://docs.oracle.com/javase/1.5.0/docs/api/org/w3c/dom/Node.html#setNodeValue(java.lang.String)

Comments

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.