简体   繁体   English

在Java中删除XML元素

[英]Deleting XML element in Java

I have an XML: 我有一个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>

and my Java code is: 我的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);
        }   
    }

The song to be deleted is passed into the method and then compared to the songs in the xml file. 要删除的歌曲将传递到方法中,然后与xml文件中的歌曲进行比较。 However, if the song matches a song in the xml, it isn't deleted? 但是,如果歌曲与xml中的歌曲匹配,则不会删除? No exceptions come up. 没有例外。

You need to remove relevant node, in you code you are removing node of firstchild which seems to be incorrect. 您需要删除相关节点,在您的代码中删除似乎不正确的firstchild节点。

And write back your changes to the file. 并将您的更改写回文件。

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

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

// write back to xml file //写回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);

From what I see, you are only reading the documents. 从我看到的,你只是阅读文件。 At some point, you will have to flush the changes back to the XML file. 在某些时候,您必须将更改刷新回XML文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM