[英]How to use user input to add another XML element to an preexisting file?
I have a program that creates a XML file for a music library, with different elements such as artist, album etc. I have hard coded one entry: 我有一个程序可以为音乐库创建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);
But I want the program to ask the user if they want to add more records to the XML file, this is what I have so far: 但是我想让程序询问用户是否要向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
However, this only appends the user input to the existing records rather than creating a new record. 但是,这仅将用户输入附加到现有记录,而不是创建新记录。 Is what I am asking possible?
我要问的可能吗?
Sorry if this kind of question has already been answered, couldn't find a similar question anywhere! 抱歉,如果您已经回答了这种问题,那么您将无法在任何地方找到类似的问题! Thanks in advance.
提前致谢。
It sounds like you want to have multiple <Album>
elements but your program only creates the 1 <Album>
element you hard coded and then appends the title, artist, year elements from user input inside the single <Album>
element? 听起来您想拥有多个
<Album>
元素,但是您的程序只创建了1个<Album>
元素,并对其进行了硬编码,然后将来自用户输入的标题,艺术家,年份元素添加到单个<Album>
元素中?
The problem is you are appending to <Album>
rather than <Albums>
with this line in your for loop: 问题是您要在for循环中将此行追加到
<Album>
而不是<Albums>
:
album.appendChild(doc.createTextNode(getTitle));
Instead of calling appendChild on your existing album variable, you'll need to create a new album variable and append it to rootElement. 您需要创建一个新的相册变量并将其附加到rootElement上,而不是对现有的相册变量调用appendChild。
Since you'll be using the exact same code to append an <Album>
as you did with your hard coded elements, I would suggest creating a method to avoid writing duplicate code, such as: 由于您将使用与硬编码元素相同的代码来附加
<Album>
,因此建议您创建一种避免编写重复代码的方法,例如:
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);
}
The rootElement argument is the <Albums>
element you created in your hard coded version. rootElement参数是您在硬编码版本中创建的
<Albums>
元素。
You can call this method for your hard coded version as well: 您也可以为您的硬编码版本调用此方法:
appendAlbum(rootElement, "A Weekend in the City", "1", "Bloc Party", "2007")
As a side note you can simplify your program slight by using the equalsIgnoreCase() method to check the user input, instead of checking for upper and lower case Y. 附带说明一下,您可以使用equalsIgnoreCase()方法检查用户输入,而不是检查大小写Y,从而可以稍微简化程序。
input.equalsIgnoreCase("y")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.