how to modify xml tag specific value in java?

▼魔方 西西 提交于 2019-12-17 19:48:14

问题


i am new to work on xml.i have used an xml file as follows:

<?xml version="1.0" encoding="UTF-8" ?> 
      - <root>
      - <key>
           <Question>Is the color of the car</Question> 
           <Ans>black?</Ans> 
       </key>
     - <key>
           <Question>Is the color of the car</Question> 
           <Ans>black?</Ans> 
       </key>
     - <key>
           <Question>Is the news paper</Question> 
           <Ans>wallstreet?</Ans> 
      </key>
    - <key>
          <Question>fragrance odor</Question> 
          <Ans>Lavendor?</Ans> 
     </key>
   - <key>
          <Question>Is the baggage collector available</Question> 
         <Ans /> 
     </key>
  </root>

from the above xml i would like to change only

             <Ans>wallstreet?</Ans> as <Ans>WonderWorld</Ans>.

how can i change wallstreet? as WonderWorld? through my java application.

i have written java method as shown below:

  public void modifyNodeval(){
 try{
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(new File(path));
        Node nodes1 = doc.getElementsByTagName("*");
        for(int j=0;j<nodes1.getLength();j++)
        {
            //Get the staff element by tag name directly
            Node nodes = doc.getElementsByTagName("key").item(j);
            //loop the staff child node
            NodeList list = nodes.getChildNodes();

            for (int i = 0; i != list.getLength(); ++i)
            {
                Node child = list.item(i);

               if (child.getNodeName().equals("Ans")) {

                   child.getFirstChild().setNodeValue("WonderWorld") ;
                   System.out.println("tag val modified success fuly");
               }

           }
       }
       TransformerFactory transformerFactory = TransformerFactory.newInstance();
       Transformer transformer = transformerFactory.newTransformer();
       DOMSource source = new DOMSource(doc);
       StreamResult result = new StreamResult(path);
       transformer.transform(source, result);
   }
   catch (Exception e) 
   {
       e.printStackTrace();
   }
}

by using above code i am able to change the all tag text as wonder world but my intention is i want change only wallstreet? as WonderWorld.

any body please help me.....


回答1:


use

if (child.getNodeName().equals("Ans") && child.getTextContent().equals("wallstreet?"))

as your if condition.




回答2:


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



回答3:


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


来源:https://stackoverflow.com/questions/7646607/how-to-modify-xml-tag-specific-value-in-java

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