2014-02-25 98 views
0

我嘗試此代碼:如何在XML中創建新元素?

public static void sendXml(String result, int operationN) throws ParserConfigurationException, SAXException, IOException{ 
     String filepath = "journal.xml"; 
     DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); 
     Document doc = docBuilder.parse(filepath); 
     doc.getFirstChild(); 
     doc.getElementsByTagName("command").item(0); 
     Element res = doc.createElement("result"); 
     res.setTextContent(result); 
     doc.appendChild(res); 
    } 

我肯定知道,一切都確定了journal.xmlresult。我有例外HIERARCHY_REQUEST_ERR: An attempt was made to insert a node where it is not permitted at org.apache.xerces.dom.CoreDocumentImpl.insertBefore(Unknown Source) at org.apache.xerces.dom.NodeImpl.appendChild(Unknown Source)。我做錯了什麼?

我有這樣的結構:

<?xml version="1.0"?> 
    <config> 
    <command> Check title (Total posts,Total topics,Total members,Our newest member) 
// here i need result 
    </command> 
    <command> Check login 
    </command> 
    </config> 
+1

您需要將該元素添加到doc,如doc.appendChild(res); ..如何打印...以concole或xml文件..post總代碼 – Naren

+0

我添加例外和文件結構 –

+0

你是否想爲已經有textContent的標籤添加一個子標籤(即檢查標題(總帖子,總主題,總會員數,我們最新的成員))...是不是? – Naren

回答

1

你可以像這樣

import java.io.File; 
import java.io.IOException; 

import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.transform.Transformer; 
import javax.xml.transform.TransformerFactory; 
import javax.xml.transform.dom.DOMSource; 
import javax.xml.transform.stream.StreamResult; 

import org.w3c.dom.Document; 
import org.w3c.dom.Element; 
import org.w3c.dom.Node; 

public class WriteXMLFile { 

    public static void sendXml(String result, int operationN) throws Exception, IOException{ 
     String filepath = "file.xml"; 
     DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); 
     Document doc = docBuilder.parse(filepath); 

     Node tonode=doc.getElementsByTagName("command").item(0); 
     Element res = doc.createElement("result"); 
     res.setTextContent(result); 
     tonode.appendChild(res); 
      TransformerFactory transformerFactory = TransformerFactory.newInstance(); 
      Transformer transformer = transformerFactory.newTransformer(); 
      DOMSource source = new DOMSource(doc); 
      StreamResult result1 = new StreamResult(new File("file2.xml")); 

      // Output to console for testing 
      StreamResult result3 = new StreamResult(System.out); 

      transformer.transform(source, result1); 

      System.out.println("File saved!"); 
    } 

    public static void main(String argv[]) { 

     try { 
      WriteXMLFile writeXMLFile=new WriteXMLFile(); 
      writeXMLFile.sendXml("hitest", 1); 

     } catch (Exception pce) { 
     pce.printStackTrace(); 
     } 
    } 
} 

file2.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<config> 
    <command> 
     Check title (Total posts,Total topics,Total members,Our newest member) 
     <result>hitest</result> 
    </command> 
    <command> Check login 
    </command> 
</config> 
+0

我應該創建新文件我不能只更新舊的? –

+0

這就是我從file.xml中讀取並將其寫入file2.xml – Naren

1

您應該將新創建的元素最後追加到文件:

doc.appendChild(res); 
+0

謝謝,我改變了一點點的問題 –