i am new to work on xml.i have used an xml file as follows:
-
-
You are not checking if the value of the node is "wallstreet?" - so it simply changes every first child node.
String str = child.getFirstChild( ).getNodeValue( );
if ( "wallstreet?".compareTo( str ) == 0 )
{
child.getFirstChild( ).setNodeValue( "WonderWorld" );
System.out.println( "tag val modified success fuly" );
}
use
if (child.getNodeName().equals("Ans") && child.getTextContent().equals("wallstreet?"))
as your if condition.
I would recommend XPath to select exactly what you want to edit with a lot less code:
XPath xpath = XPathFactory.newInstance().newXPath();
Element e = (Element) xpath.evaluate("//Ans[. = 'wallstreet']", document, XPathConstant.NODE);
if (e != null)
e.setTextContent("Wonderland");