我嘗試此代碼:如何在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.xml
和result
。我有例外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>
您需要將該元素添加到doc,如doc.appendChild(res); ..如何打印...以concole或xml文件..post總代碼 – Naren
我添加例外和文件結構 –
你是否想爲已經有textContent的標籤添加一個子標籤(即檢查標題(總帖子,總主題,總會員數,我們最新的成員))...是不是? – Naren