【问题标题】:how to modify xml tag specific value in java?如何在java中修改xml标签的具体值?
【发布时间】:2011-11-30 14:08:24
【问题描述】:

我是 xml 的新手。我使用的 xml 文件如下:

<?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>

从上面的xml我只想改变

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

我怎样才能改变华尔街?作为奇迹世界?通过我的 java 应用程序。

我写的java方法如下:

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

通过使用上面的代码,我可以将所有标签文本更改为奇迹世界,但我的意图是我只想更改wallstreet?作为 WonderWorld。

任何人请帮助我.....

【问题讨论】:

  • 到目前为止您尝试过什么?你有一些代码给我们看吗?另外,如果接受更多答案,这里的人会更有帮助。
  • @forty-2 检查我的 java 代码

标签: java xml edit


【解决方案1】:

使用

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

作为你的 if 条件。

【讨论】:

    【解决方案2】:

    您没有检查节点的值是否为“wallstreet”? - 所以它只是改变每个第一个子节点。

    String str = child.getFirstChild( ).getNodeValue( );
    if ( "wallstreet?".compareTo( str ) == 0 )
    {
        child.getFirstChild( ).setNodeValue( "WonderWorld" );
        System.out.println( "tag val modified success fuly" );
    }
    

    【讨论】:

      【解决方案3】:

      我建议 XPath 用更少的代码准确地选择您想要编辑的内容:

      XPath xpath = XPathFactory.newInstance().newXPath();
      Element e = (Element) xpath.evaluate("//Ans[. = 'wallstreet']", document, XPathConstant.NODE);
      if (e != null)
        e.setTextContent("Wonderland");
      

      【讨论】:

      • 如果我想同时更改 black?wallstreet? 那么如何?
      • 嗯,你不能同时改变两者。只需用另一个表达式重复,或重构为一个方法。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-29
      • 2019-05-28
      • 2013-09-21
      相关资源
      最近更新 更多