2014-03-24 22 views
1

我已經開始看JDOM。到目前爲止,我已經創建了一個名爲file.xml的文件。現在我想在XML文件中添加一些內容,但我有點不安全如何做到這一點? 這可能是外匯: - 姓名 - 姓氏 - 年齡通過JDOM將contant添加到XML

希望有人能幫助我嗎?朱莉問候在控制檯

ppackage examplePackage; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import org.jdom2.Document; 
import org.jdom2.Element; 
import org.jdom2.JDOMException; 
import org.jdom2.input.SAXBuilder; 
import org.jdom2.output.Format; 
import org.jdom2.output.XMLOutputter; 

public class ReadXMLFile { 

    public static void main(String[] args) { 
     try { 
      write(); 
      read(); 
     } 
     catch (FileNotFoundException e){ 
      e.printStackTrace(); 
     } 
     catch(IOException e) { 
      e.printStackTrace(); 
     } 
     catch(JDOMException e) { 
      e.printStackTrace(); 
     } 
    } 

     public static void read() throws JDOMException, IOException { 


      SAXBuilder reader = new SAXBuilder(); 
      Document document = reader.build(new File("file.xml")); 
      XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat()); 
      xout.output(document, System.out); 
     } 

     public static void write() throws FileNotFoundException, IOException { 



Document document = new Document(); 
     Element root = new Element("document"); 
     root.setAttribute("file", "file.xml"); 
     root.addContent(new Element("style")); 
     document.setRootElement(root); 

     Element person = new Element("Person");    
     person.setAttribute("name", "Mads"); 
     } 
} 

打印出來:

<?xml version="1.0" encoding="UTF-8"?> 
<document file="file.xml"> 
    <style /> 
</document> 

在這種情況下,它應該打印出一個名爲的人「的Mads」吧?

回答

0

在你read方法,你可能要改變這一行:

的System.out.println(document.getContent());

喜歡的東西:

XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat()); 
xout.output(document, System.out); 

那會在它的部分打印整個文檔,而不僅僅是toString()

之後,有關將內容....在write()方法:

  • 您設置的根元素有:

    Document document = new Document(); 
        Element root = new Element("document"); 
        root.setAttribute("file", "file.xml"); 
        root.addContent(new Element("style")); 
        document.setRootElement(root); 
    

    。有什麼錯但是它通常更常用,例如:

    Element root = new Element("document"); 
        root.setAttribute("file", "file.xml"); 
        root.addContent(new Element("style")); 
        Document document = new Document(root); // note the root constructor 
    
  • ,如果你想添加額外的內容,例如:

    for (int i = 0; i < 10; i++) { 
        Element count = new Element("count" + i); 
        root.addContent(count); 
    } 
    

    Element person = new Element("person");   
    person.setAttribute("name", "Jon"); 
    person.setAttribute("lastname", "Skeet"); 
    person.setAttribute("age", "37"); 
    root.addContent(person); 
    

更新:最後,你已經添加的內容後,你實際需要編寫該文件出磁盤:

XMLOutputter xmlout = new XMLOutputter(Format.getPrettyFormat()); 
try (FileOutputStream fos = new FileOutputStream("file.xml")) { 
    xmlout.output(document, fos); 
} 
+0

你好。非常感謝您的非常好的答案,並給出了答案。我剛剛更改了代碼(我編輯了我的問題)。當我運行代碼時,它仍然打印出舊的一個 – Julie24

+0

@ Julie24 - 添加了一個更新到我的答案(編輯) – rolfl