how to modify xml tag specific value in java?

喜夏-厌秋 提交于 2019-11-28 10:36:43

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

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" );
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!