如何使用用户输入将另一个XML元素添加到预先存在的文件中?

时间:2015-04-23 23:10:35

标签: java xml

我有一个程序可以为音乐库创建一个XML文件,其中包含不同的元素,如艺术家,专辑等。我有一个硬编码的条目:

        //Root elements
        Document doc = docBuilder.newDocument();
        Element rootElement = doc.createElement("Albums");
        doc.appendChild(rootElement);

        //Album elements
        Element album = doc.createElement("Album");
        rootElement.appendChild(album);

        //Set attribute to album element
        Attr attr = doc.createAttribute("ID");
        attr.setValue("1");
        album.setAttributeNode(attr);

        //Title elements
        Element title = doc.createElement("Title");
        title.appendChild(doc.createTextNode("A Weekend in the City"));
        album.appendChild(title);

        //Artist elements
        Element artist = doc.createElement("Artist");
        artist.appendChild(doc.createTextNode("Bloc Party"));
        album.appendChild(artist);

        //Year elements
        Element year = doc.createElement("Year");
        year.appendChild(doc.createTextNode("2007"));
        album.appendChild(year);

但是我希望程序询问用户是否要将更多记录添加到XML文件中,这是我到目前为止所做的:

        if ((input.equals("Y")) || (input.equals("y")))
        {
            System.out.println("How many records would you like to add?");
            String userInput = scanner.next();
            int numRecords=Integer.parseInt(userInput);

            for (int i=0; i<numRecords; i++)
            {
                System.out.println("Enter the name of the album");
                String getTitle = scanner.nextLine();
                album.appendChild(doc.createTextNode(getTitle));
                album.appendChild(title);
                System.out.println("Enter the name of the artist");
                String getArtist = scanner.nextLine();
                artist.appendChild(doc.createTextNode(getArtist));
                album.appendChild(artist);
                System.out.println("Enter the year the album was released");
                String getYear = scanner.nextLine();
                artist.appendChild(doc.createTextNode(getYear));
                album.appendChild(year);

            } //End for loop

但是,这只会将用户输入附加到现有记录而不是创建新记录。我问的可能吗?

很抱歉,如果此类问题已经得到解答,则无法在任何地方找到类似的问题!提前谢谢。

1 个答案:

答案 0 :(得分:1)

听起来你想拥有多个<Album>元素,但你的程序只创建你硬编码的1 <Album>元素,然后在单个{{}}中附加用户输入的标题,艺术家,年元素{ {1}}元素?

问题是您在for循环中使用此行追加<Album>而不是<Album>

<Albums>

您需要创建新的相册变量并将其附加到rootElement,而不是在现有的相册变量上调用appendChild。

由于您将使用完全相同的代码来附加album.appendChild(doc.createTextNode(getTitle)); ,就像对硬编码元素一样,我建议您创建一种避免编写重复代码的方法,例如:

<Album>

rootElement参数是您在硬编码版本中创建的private void appendAlbum(Element rootElement, String titleStr, String id, String artistStr, String yearStr) { Element album = doc.createElement("Album"); rootElement.appendChild(album); //Set attribute to album element Attr attr = doc.createAttribute("ID"); attr.setValue(id); album.setAttributeNode(attr); //Title elements Element title = doc.createElement("Title"); title.appendChild(doc.createTextNode(titleStr)); album.appendChild(title); //Artist elements Element artist = doc.createElement("Artist"); artist.appendChild(doc.createTextNode(artistStr)); album.appendChild(artist); //Year elements Element year = doc.createElement("Year"); year.appendChild(doc.createTextNode(yearStr)); album.appendChild(year); } 元素。

您也可以为您的硬编码版本调用此方法:

<Albums>

作为旁注,您可以使用equalsIgnoreCase()方法检查用户输入,而不是检查大写和小写Y,从而简化您的程序。

appendAlbum(rootElement, "A Weekend in the City", "1", "Bloc Party", "2007")