2015-03-02 86 views
1

我有以下XML文件:如何在插入值,以XML標籤

<?xml version="1.0"?> 
<SMS> 
    <ALERTS> 
     <SNO>1</SNO> 
     <MOBILENUMBER>1234567890</MOBILENUMBER> 
     <TEXT>HI</TEXT> 
     <TIME></TIME> 
     <RESPONSEID></RESPONSEID> 
    </ALERTS>  
</SMS> 

我想插入字符串值轉換爲XML標籤(RESPONSEID)。我嘗試過使用setTextContent和setNodeValue方法,這些方法對我不起作用。

以下是我的小程序:

public void selectRecords() throws SQLException 
{ 
    File file = new File("E:\\Workspace\\netbeans-workspace\\DOM_Parser_Sample\\MyXMLFile1.xml"); 
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
    DocumentBuilder db = dbf.newDocumentBuilder(); 
    Document doc = db.parse(file); 
    doc.getDocumentElement().normalize(); 
    NodeList nodeList = doc.getElementsByTagName("ALERTS"); 
    for (int s = 0; s < nodeList.getLength(); s++) 
    { 
    Node firstNode = nodeList.item(s); 

     if (firstNode.getNodeType() == Node.ELEMENT_NODE) 
     { 
      Element eElement = (Element) firstNode; 
      BufferedReader in = new BufferedReader(new InputStreamReader(httpcon.getInputStream())); 
      StringBuffer response = new StringBuffer(); 
      while ((inputLine = in.readLine()) != null) 
      { 
        res = response.append(inputLine).toString(); 
      } 
      in.close(); 
      NodeList responseList = eElement.getElementsByTagName("RESPONSEID").item(0).getChildNodes(); 
      responseList.item(0).setTextContent(res); 
     } 
    } 
} 
+0

這個[張貼在這裏(http://stackoverflow.com/questions/7610894/how-to-insert-value-into-xml )包含一些有用的鏈接和響應。 – Rohit416 2015-03-02 05:56:26

+0

'DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); Document doc = docBuilder.parse(inputFile); 節點messageType = doc.getElementsByTagName(「messageType」)。item(0); //零告訴xml中的順序 messageType.setTextContent(「SMS」);'即使這個例子不起作用:( – kittu 2015-03-02 06:03:09

回答

2

示例代碼檢查節點名稱和值:我希望這會幫助你解決你的問題。

Node alertNode = doc.getElementsByTagName("ALERTS").item(0); 
    NodeList nodeList = alertNode.getChildNodes(); 
    for (int i = 0; i < nodeList.getLength(); i++) { 

     Node node = nodeList.item(i); 
     System.out.println("NodeName:" + node.getNodeName() + ":TextContent:" + node.getTextContent()); 
     if ("RESPONSEID".equalsIgnoreCase(node.getNodeName())) { 
      node.setTextContent("RESPONSEID-1"); 
     } 
    } 

/**只需驗證設定值*/

Node resNode = doc.getElementsByTagName("RESPONSEID").item(0); 
    System.out.println("NodeName:" + resNode.getNodeName() + ":TextContent:" + resNode.getTextContent()); 

/**要保存文件作爲MyXMLFile1-的test.xml */

TransformerFactory transformerFactory = TransformerFactory.newInstance(); 
    Transformer transformer = transformerFactory.newTransformer(); 
    DOMSource source = new DOMSource(doc); 
    StreamResult result = new StreamResult(new File("MyXMLFile1-test.xml")); 
    transformer.transform(source, result); 

你只需要訪問「RESPONSEID」節點並設置文本內容。

+0

當我驗證設置值:我得到的輸出爲RESPONSEID-1,但是當我打開實際的xml文件時,它沒有在文件中更新。 – kittu 2015-03-02 06:33:48

+0

@kittu你必須將其保存到文件中,請檢查更新的答案 – nayakam 2015-03-02 06:36:06

+1

我沒有'不知道我有新的變換,並添加到文件。非常感謝幫助我:) – kittu 2015-03-02 06:44:34

1

你只有一個小疏忽。當你做getElementsByTagName("RESPONSEID").item(0)時,你找回一個節點。您不希望該節點的子元素(沒有子元素)。相反,您直接想要設置該節點的文本內容。

更改兩行代碼,它的工作原理:

Node responseList = eElement.getElementsByTagName("RESPONSEID").item(0); 
responseList.setTextContent(res);