2011-04-16 115 views
1

我想創建一個XML文檔,看起來像這樣,但我不知道如何:如何創建一個簡單的XML文檔這樣一個

<bo type="Employee" id="0000012f41bce2a865f8616b0010007c0008008b"> 
    <username>marv</username> 
</bo> 

這是我到目前爲止,我「M真的只是困惑,如何將username添加元素:

Element bo = testDoc.createElement("bo"); 
     bo.setAttribute("type", "Employee"); 
     bo.setAttribute("id", emp.getId()); 
+0

不是一個很好的問題,你可以花一些時間告訴我們更多。只是閱讀本身的問題,我會告訴你通過將它複製到一個字符串(CTRL + C,CTRL + V)來創建它。但是這並不是很有趣,即使它是你的問題的完美答案。請告訴我們一些事情,這符合你的興趣。此刻,這個問題-1。 – gd1 2011-04-16 01:30:31

+0

@Giacomo感謝您提醒我使其更具體,它更好?確實是 – novicePrgrmr 2011-04-16 01:35:19

+0

。我刪除了我的反對票。 – gd1 2011-04-16 01:41:22

回答

2
Element bo = testDoc.createElement("bo"); 
bo.setAttribute("type", "Employee"); 
bo.setAttribute("id", emp.getId()); 
//create a username element 
Element username = testDoc.createElement("username"); 
//add a text value to the username element 
username.appendChild(testDoc.createTextNode("marv")); 
//add the username element as child of bo element 
bo.appendChild(username); 
0

創建一個用戶名元素,設置它的值,然後使它博元素的子元素。我不知道Java,但可能會使用AddChild()方法或類似的方法。