Java xml编写器覆盖

时间:2014-01-28 16:44:29

标签: java xml dom

我的XML(DOM)编写器无法按预期工作。它可以使用addSong()方法将歌曲添加到其中,它将3个字符串传递给saveSong()方法。

saveSong()将它们保存到XML,但是当我将其保存到XML文件时,它会写出它应该执行的操作,但会覆盖其他所有内容,但它不是'应该这么做..

我只需要它就像一个“列表”,你可以继续添加歌曲,请帮忙!

歌曲课程:

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();
        }
    }
}

主要课程: 将字符串传递给addSong(),将其传递给saveSong()XML将其写入public class main { public static void main(String[] args){ Song Song = new Song(); Song.addSong("This is", "A very good","Song"); } } 文件。

{{1}}

1 个答案:

答案 0 :(得分: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并将其解析为doc时,现有的播放列表节点将不会在现有根节点中添加时覆盖。得到它?