4

我有一个 XML:

<?xml version="1.0" encoding="UTF-8"?>
    <songs>
        <song>
            <title>Gracious</title>
            <artist>Ben Howard</artist>
            <genre>Singer/Songwriter</genre>
        </song>
        <song>
            <title>Only Love</title>
            <artist>Ben Howard</artist>
            <genre>Singer/Songwriter</genre>
        </song>
        <song>
            <title>Bad Blood</title>
            <artist>Bastille</artist>
            <genre>N/A</genre>
        </song>
        <song>
            <title>Keep Your Head Up</title>
            <artist>Ben Howard</artist>
            <genre>Singer/Songwriter</genre>
        </song>
        <song>
            <title>Intro</title>
            <artist>Alt-J</artist>
            <genre>Alternative</genre>
        </song>
    </songs>

我的Java代码是:

public static void deleteSong(Song song) {
        String songTitle = song.getTitle();
        String songArtist = song.getArtist();
        String songGenre = song.getGenre();

        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            File file = new File("songs.xml");
            Document doc = db.parse(file);

            NodeList songList = doc.getElementsByTagName("song");

            if (songList != null && songList.getLength() > 0) {
                for (int i = 0; i < songList.getLength(); i++) {

                    Node node = songList.item(i);
                    Element e = (Element) node;
                    NodeList nodeList = e.getElementsByTagName("title");
                    String title = nodeList.item(0).getChildNodes().item(0)
                            .getNodeValue();
                    nodeList = e.getElementsByTagName("artist");
                    String artist = nodeList.item(0).getChildNodes().item(0)
                            .getNodeValue();
                    nodeList = e.getElementsByTagName("genre");
                    String genre = nodeList.item(0).getChildNodes().item(0)
                            .getNodeValue();
                    System.out.println(title + " Title");
                    System.out.println(songTitle + " SongTitle");

                    if (title.equals(songTitle)) {
                        if (artist.equals(songArtist)) {
                            if (genre.equals(songGenre)) {

                                doc.getFirstChild().removeChild(node);
                            }
                        }
                    }
                }
            }
            MainDisplay.main(null);
        } catch (Exception e) {
            System.out.println(e);
        }   
    }

将要删除的歌曲传入该方法,然后与 xml 文件中的歌曲进行比较。但是,如果歌曲与 xml 中的歌曲匹配,它不会被删除吗?没有例外。

4

2 回答 2

4

您需要删除相关节点,在您的代码中,您正在删除似乎不正确的 firstchild 节点。

并将您对文件的更改写回。

 if (title.equals(songTitle) && artist.equals(songArtist) && genre.equals(songGenre) ) {

                 node.getParentNode().removeChild(node);
  }

//写回xml文件

TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(filepath));
transformer.transform(source, result);
于 2013-05-25T16:05:51.630 回答
3

据我所知,您只是在阅读文件。在某些时候,您必须将更改刷新回 XML 文件。

于 2013-05-25T16:05:24.923 回答