2013-12-19 192 views
0

我試圖給一個空的XML節點賦值,但似乎沒有工作。我的XML結構是這樣:Java爲XML節點賦值

<createCustomer> 
    <customerAttributes> 
     <firstName></firstName> 
     <lastName></lastName> 
    </customerAttributes> 
</createCustomer> 

我試圖分配在下面的代碼名字和姓氏:

private void createXML(Document skeleton, Map params) { 
skeleton.getDocumentElement().normalize(); 

NodeList customerNodes = skeleton.getElementsByTagName("customerAttributes"); 

      for(int i=0; i<customerNodes.getLength(); i++) { 
       NodeList children = customerNodes.item(i).getChildNodes(); 
        for(int j=0; j<children.getLength(); j++) { 
         String childNode = children.item(j).getNodeName(); 
          if(childNode.equalsIgnoreCase("firstName")){ 
           children.item(j).setNodeValue(String.valueOf(params.get("fname"))); 
           System.out.println(children.item(j)); 
          } 
          else if (childNode.equalsIgnoreCase("lastName")){ 
           children.item(j).setNodeValue(String.valueOf(params.get("sname"))); 
           System.out.println(children.item(j)); 
          } 
        } 
      } 

    } 
} 

打印報表的輸出是:

firstname: null surname: null

但我確定地知道map中的值是正確的,因爲print語句輸出預期的地圖值。另外,如果我用硬編碼字符串替換params.get("string"),我仍然得到輸出:firstname: null。該代碼不會拋出任何異常。我也試着setTextContent但是,這並不工作,要麼

+0

檢查您貼上了正確的代碼段;外環似乎重複;另外,粘貼例外,如果有的話,是有用的 –

+0

@guido謝謝我已經格式化的代碼更容易閱讀。代碼沒有給出任何例外,它建立並運行正常 – adohertyd

回答

1

您可以使用setTextContent(String)

NodeList customerNodes = skeleton.getElementsByTagName("customerAttributes"); 
for (int i = 0; i < customerNodes.getLength(); i++) { 
    NodeList children = customerNodes.item(i).getChildNodes(); 
    for (int j = 0; j < children.getLength(); j++) { 
     String childNode = children.item(j).getNodeName(); 
     if (childNode.equalsIgnoreCase("firstName")) { 
      children.item(j).setTextContent(String.valueOf(params.get("fname"))); 
     } 
     else if (childNode.equalsIgnoreCase("lastName")) { 
      children.item(j).setTextContent(String.valueOf(params.get("sname"))); 
     } 
    } 
} 

編輯:

它的工作原理,下面是該結果的完整的例子。

import java.io.*; 
import javax.xml.parsers.*; 
import javax.xml.transform.*; 
import javax.xml.transform.dom.DOMSource; 
import javax.xml.transform.stream.StreamResult; 
import org.w3c.dom.*; 
import org.xml.sax.InputSource; 

public class Foo { 
    public static void main(String[] args) throws Exception { 
     // --------- LOAD XML 
     DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 
     Document doc = db.parse(new InputSource(new StringReader("<createCustomer>\r\n" + 
       " <customerAttributes>\r\n" + 
       "  <firstName></firstName>\r\n" + 
       "  <lastName></lastName>\r\n" + 
       " </customerAttributes>\r\n" + 
       "</createCustomer>"))); 

     // --------- PROCESS 
     NodeList customerNodes = doc.getElementsByTagName("customerAttributes"); 

     for (int i = 0; i < customerNodes.getLength(); i++) { 
      NodeList children = customerNodes.item(i).getChildNodes(); 
      for (int j = 0; j < children.getLength(); j++) { 
       String childNode = children.item(j).getNodeName(); 
       if (childNode.equalsIgnoreCase("firstName")) { 
        children.item(j).setTextContent(String.valueOf("John")); 
       } 
       else if (childNode.equalsIgnoreCase("lastName")) { 
        children.item(j).setTextContent(String.valueOf("Doe")); 
       } 
      } 
     } 

     // --------- OUTPUT 
     TransformerFactory tf = TransformerFactory.newInstance(); 
     Transformer transformer = tf.newTransformer(); 
     transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); 
     StringWriter writer = new StringWriter(); 
     transformer.transform(new DOMSource(doc), new StreamResult(writer)); 
     System.out.println(writer.getBuffer().toString()); 
    } 
} 

它確實輸出:

<createCustomer> 
    <customerAttributes> 
     <firstName>John</firstName> 
     <lastName>Doe</lastName> 
    </customerAttributes> 
</createCustomer> 
+0

對不起,我已經嘗試過,它也沒有工作。將更新我的問題。感謝您的回答 – adohertyd

+0

@adohertyd是的它的作品,看到我的編輯 – Alex

+0

有趣,但它仍然不適合我!我正在從文件讀取XML到文檔對象中,然後直接從那裏解析XML。雖然 – adohertyd