2014-01-28 74 views
0

我的XML(DOM)寫入器無法按預期工作。使用addSong()方法可以很好地向其中添加歌曲,該方法將3個字符串傳入saveSong()方法。Java xml書寫器覆蓋

saveSong()它們保存到一個XML,但是當我將它保存到XML文件,將其寫入什麼它應該做的,但別的壓倒一切,這它不是」 T應該做的..

我只需要它就像一個你可以繼續添加歌曲的「列表」,請幫助!

宋類:

public class Song { 
List<String[]> songs = new ArrayList<String[]>(); 

public void addSong(String s, String a, String yt){ 
    String[] songarray= new String[3]; 
    songarray[0] = s; 
    songarray[1] = a; 
    songarray[2] = yt; 
    songs.add(songarray); 
    saveSong(songarray); 



} 
public void editSong(int i, String s, String a, String yt){ 
    String[] editsongarray = new String[3]; 
    editsongarray[0] = s; 
    editsongarray[1] = a; 
    editsongarray[2] = yt; 
    songs.remove(i); 
    songs.add(i,editsongarray); 
} 
public void removeSong(int i){ 
    songs.remove(i); 
} 

public String[] getList(int i){ 
    String[] j = songs.get(i); 
    return j; 
} 
public void saveSong(String[] songl){ 
    try{ 

     DocumentBuilderFactory song = DocumentBuilderFactory.newInstance(); 
      DocumentBuilder songBuilder = song.newDocumentBuilder(); 

      Document doc = songBuilder.newDocument(); 
      Element playlist = doc.createElement("playlist"); 
      doc.appendChild(playlist); 

      Element songs = doc.createElement("songs"); 
      playlist.appendChild(songs); 

      Attr attr = doc.createAttribute("index"); 
      attr.setValue("1"); 
      playlist.setAttributeNode(attr); 


      Element name = doc.createElement("name"); 
      name.appendChild(doc.createTextNode(songl[0])); 
      songs.appendChild(name); 

      Element artistname = doc.createElement("artistname"); 
      artistname.appendChild(doc.createTextNode(songl[1])); 
      songs.appendChild(artistname); 

      Element youtubeurl = doc.createElement("youtubeurl"); 
      youtubeurl.appendChild(doc.createTextNode(songl[2])); 
      songs.appendChild(youtubeurl); 

      TransformerFactory tf = TransformerFactory.newInstance(); 
      Transformer tr = tf.newTransformer(); 
      DOMSource dom = new DOMSource(doc); 
      StreamResult sr = new StreamResult(new File("c:\\Applications\\staff.xml")); 

      tr.transform(dom, sr); 

      System.out.println("done"); 

     }catch(ParserConfigurationException pce){ 
      pce.printStackTrace(); 
     }catch(TransformerException fce){ 
      fce.printStackTrace(); 
     } 
    } 
} 

主類: string傳給addSong(),其將它們傳遞給saveSong(),這在XML文件寫入其中。

public class main { 
    public static void main(String[] args){ 
     Song Song = new Song(); 
     Song.addSong("This is", "A very good","Song"); 
    } 
} 
+2

那麼*它意味着要對現有文件做什麼?如果你想修改現有的文件(例如添加元素),那麼你應該解析現有的文件,添加任何相關的元素,然後再次保存。# –

+0

@JonSkeet雅,我應該添加內容到現有的文件: - )..我需要它來存儲歌曲的細節,並再次將它們加載到我製作的音樂播放器中。您知道任何好的教程解析 - 添加 - 保存?聽起來很複雜,我感謝你的幫助! -/Oliver –

+2

而不是尋找一個單一的教程,爲什麼你不研究每個主題?例如,必須有成千上萬的頁面向您展示如何使用Java解析XML文件。 –

回答

1

我沒有測試此代碼,請相應地修改。

try{ 
    // open your existing xml file first 
     FileInputStream in=new FileInputStream("c:\\Applications\\staff.xml");   

     DocumentBuilderFactory song = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder songBuilder = song.newDocumentBuilder(); 
      // Parse this file as input document 
     Document docIn=builderel.parse(staff.xml); 
     //Define your root Element here, I am assuming it as <root> 
     Element root; 
     // now we will add all elements in your root 
     Element playlist = root.createElement("playlist"); 
     // Now add all new childs in root.Here i am adding only one. Add rest of the child as per your need in root 
      root.appendChild(playlist); 
     //Now add this new root in your output doc 
      docIn.appendChild(root); 
     TransformerFactory tf = TransformerFactory.newInstance(); 
     Transformer tr = tf.newTransformer(); 
     DOMSource dom = new DOMSource(docIn); 
     StreamResult sr = new StreamResult(new File("c:\\Applications\\staff.xml")); 

     tr.transform(dom, sr); 

     System.out.println("done"); 

您需要首先定義一個空staff.xml,其中具有唯一根節點,

c:\\Applications\\staff.xml 

<?xml version="1.0" encoding="UTF-8"?> 
       <root></root> 

我希望,你會得到現在的想法。 總之,

我們開口上方staff.xml

添加在現有根節點的所有元素。

覆蓋具有新元素的現有文件以及現有文件。

輸出將是

<?xml version="1.0" encoding="UTF-8"?> 
       <root> 
       <playist></playlist> 
       <playist></playlist> 
       <playist></playlist> 
       .... 
       </root> 

而下一次,當你將打開你的staff.xml並解析它作爲docIn您現有的播放列表節點不會得到覆蓋,你在現有的根節點加入。得到它?