2014-01-28 39 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您现有的播放列表节点不会得到覆盖,你在现有的根节点加入。得到它?