Reputation: 23
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:
//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:
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.
Upvotes: 0
Views: 103
Reputation: 74
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?
The problem is you are appending to <Album>
rather than <Albums>
with this line in your for loop:
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.
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:
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.
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.
input.equalsIgnoreCase("y")
Upvotes: 1