2017-10-06 15 views
0

eElementにある値を古い値と一致するものを別の値に変更する必要があります。xmlの値をjavaでdomを使用

eElement.setAttribute(...)機能とsetTextContent機能で試してみましたが機能しません。

新しい値がnewValueという文字列変数に格納されているとしたら、コードを実行するにはどうすればよいですか?

NodeList leaf = doc.getElementsByTagName(relativeLeaf); 
System.out.println(leaf.item(0).getNodeName()); 
for (int temp = 0; temp < leaf.getLength(); temp++) { 
    Node nNode = leaf.item(temp); 
    if (nNode.getNodeType() == Node.ELEMENT_NODE) { 
     Element eElement = (Element) nNode; 
     String oldValueInCells = eElement.getElementsByTagName(relativeLeaf).item(0).getTextContent(); 
     System.out.println("old tag : " + eElement.getElementsByTagName(relativeLeaf).item(0).getTextContent()); 
     if(oldValueInCells.contentEquals(oldVal)){ 
      // #### 
      // here i have to change tha value in eElement 
      // where it match with the old Value with a new one  
     } 
    } 
} 

答えて

0

文書のベースノード要素を取得して値を設定できると思います。私がしようが、メソッドが値を更新していけないあなたの答えのためのhttps://docs.oracle.com/javase/7/docs/api/org/w3c/dom/Node.html

+0

感謝=):

Node node = eElement.getElementsByTagName(relativeLeaf).item(0); //You get the node here so you can use it as well to create OldValueInCells String oldValueInCells = node.getTextContent(); System.out.println("old tag : " + oldValueInCells); if(oldValueInCells.contentEquals(oldVal)){ // #### // here i have to change tha value in eElement // where it match with the old Value with a new one node.setTextContent("new value"); //Here you will set the value to the node } 

出典:あなたはまず、次のようにノード要素を取得する必要があります。私はテストコードを貼り付けます。 \t \t \t \t \t IF(eElement.getElementsByTagName( "説明")。項目(0).getTextContent()。contentEquals( "X")){ \t \t \t \t \t \t \t \t \t \t \t \t NNODE。 setNodeValue( "co"); \t \t \t \t \t \tのSystem.out.println( "説明:" +((要素)NNODE).getElementsByTagName( "説明")項目(0).getTextContent())。 \t \t \t \t equals関数は正常に機能しますが、値を変更する必要があることはわかりません。何が問題なの? –

+0

値が更新されている様子はどうですか? –

+0

((Element)nNode).getElementsByTagName( "description")。item(0).getTextCo ntent()); ノードを要素にキャストし、タグ名で取得する関数を適用します。 –

関連する問題