First of all English is not my first language, so sorry in advance for the mistakes.
The thing is I need some help understanding why I´m overwriting some values inside my array list.
I know this question has been asked before but all solutions I found about this issue are related to variables declared static when they shouldn´t be, which I think is not my case.
The code is searching in two different .xml documents for pop songs contained in a especific album, and returning the array with the title and genres of each song.
First I have a Song defined as:
public class Song {
private String title;
private ArrayList<String> genre;
public Song (String t, ArrayList<String> g) {
title = t;
genre = g;
}
public String getTitle () { return title; }
public ArrayList<String> getGenre () { return genre; }
}
Then I´m searching trough the .xml documents for the required album and country. It correctly finds the songs and the genres, but I´m having trouble adding them into an array. The code is the following: In this example the required album is called "titulo5" and thecountry is "england".
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
public class Main {
private static ArrayList<Song> Songs = new ArrayList<Song>();
static DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
static DocumentBuilder db;
static {
try {
db = dbf.newDocumentBuilder();
} catch (ParserConfigurationException e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException {
String nextURL = run("tmvl_01.xml","england","titulo5");
nextURL = run(nextURL,"england","titulo5");
}
private static String run(String filename, String country, String album)throws ParserConfigurationException, IOException, SAXException
{
String nextURL = null;
ArrayList<String> auxArray = new ArrayList<String>();
try
{
Document doc = db.parse(new File(filename));
Boolean test = false;
NodeList nl = doc.getElementsByTagName("Album");
Element elem;
for (int x=0; x < nl.getLength(); x++)
{
elem = (Element)nl.item(x);
NodeList nlTitles = elem.getElementsByTagName("Titulo");
NodeList nlSongs = elem.getElementsByTagName("Song");
Element elemAlbumTitle = (Element)nlTitles.item(0);
String albumName = elemAlbumTitle.getTextContent().trim();
if (albumName.equals(album)){
for(int i= 0; i < nlSongs.getLength();i++)
{
Element elemSongs = (Element)nlSongs.item(i);
NodeList nlSongTitle = elem.getElementsByTagName("Title");
Element elemSongTItle = (Element) nlSongTitle.item(i);
String songTItle = elemSongTItle.getTextContent().trim();
NodeList nlGenre = elemSongs.getElementsByTagName("Genre");
auxArray.clear();
for (int e = 0; e< nlGenre.getLength(); e++)
{
Element elemGenre = (Element) nlGenre.item(e);
String genreName = elemGenre.getTextContent().trim();
auxArray.add(genreName);
if(genreName.equals("pop"))
{
test = true;
}
}
if(test == true)
{
System.out.println("Array: " + auxArray);
System.out.println("Titulo: " +songTItle);
Song check = new Song(songTItle,auxArray);
System.out.println("Check SOng: " + check.getGenre());
Songs.add(check); //Here is the issue
System.out.println("ArraySize: " + Songs.size());
for(int z = 0; z<Songs.size();z++) {
String songTitle = Songs.get(z).getTitle();
System.out.println("ArrayTitulo: " + songTitle);
String songGenres;
for (int p = 0 ; p < Songs.get(z).getGenre().size();p++)
{
songGenres = Songs.get(z).getGenre().get(p);
System.out.println("ArrayGenreP: "+p + " ArrayGenreZ: " + z + " Item: "+songGenres);
}
}
System.out.println("Genres: " + Songs.get(0).getGenre());
if(Songs.size()>1){
System.out.println("Genres: " + Songs.get(1).getGenre());
}
auxArray.clear();
test = false;
}
}
}
}
NodeList nl_TVMLs = doc.getElementsByTagName("TVML");
Element elemnt;
for (int x=0; x < nl_TVMLs.getLength(); x++)
{
elemnt = (Element)nl_TVMLs.item(x);
nextURL = elemnt.getTextContent().trim();
System.out.println("Siguiente URL: " + nextURL);
}
}
catch(SAXException e)
{
System.out.println("Excepction no well formed");
}
return nextURL;
}
}
This, I think, it should add in the Songs array two songs, with two titles and, two genres the first one and three genres the second one, resulting in something like this:
Song Title: 8 Genres:[metal,pop,balad]
Song Title: 11 Genres:[funk,pop]
But the output I´m getting is:
Song Title: 8 Genres: [funk,pop]
Song Title: 11 Genres:[funk,pop]
Titles are correct, but genres are beeing overwrited by the last search in the loop.
My System.out.println() results are:
Array: [metal, pop, ballad] --> *First genre extracted array*
Title: 8
Check SOng: [metal, pop, ballad] --> *Genres inside the Song object*
ArraySize: 1
ArrayTitulo: 8 --> Title is OK
ArrayGenreP: 0 ArrayGenreZ: 0 Item: metal --> *P = genres index and Z = title index*
ArrayGenreP: 1 ArrayGenreZ: 0 Item: pop
ArrayGenreP: 2 ArrayGenreZ: 0 Item: ballad
Genres: [metal, pop, ballad] *--> Here is OK: Title 8 should have this 3 genres*
Array: [funk, pop] *--> This is the second array which is correct*
Title: 11 *--> This is the second title which is also ok*
Check SOng: [funk, pop] *--> This is the array stored in the new song object*
ArraySize: 2 *--> The new updated array size*
ArrayTitle: 8 *--> The first title is correct*
ArrayGenreP: 0 ArrayGenreZ: 0 Item: funk *--> Here is the problem, it overwrites the previous genres*
ArrayGenreP: 1 ArrayGenreZ: 0 Item: pop * --> (but not the previous title)*
ArrayTitle: 11 --> Second title is also correct
ArrayGenreP: 0 ArrayGenreZ: 1 Item: funk *-->These are the correct genres for song with title 11*
ArrayGenreP: 1 ArrayGenreZ: 1 Item: pop
Genres: [funk, pop] *--> Here should be [metal, pop, ballad] *
Genres: [funk, pop] *--> Here is OK*
And I´m completely stuck, I don´t know what else I can try, so some help would be very appreciated.
Maybe is something that has already beeing answered, if is the case I´m sorry, not my intention to waste anyones time.
Just in case I´m showing next the 2 .xml documents I created. The first one is:
<?xml version="1.0"?>
<Music>
<Year>2001</Year>
<Album>
<Name>uno</Name>
<Country>spain</Country>
<Titulo>titulo1</Titulo>
<Song>
<Title>1</Title>
<Genre>pop</Genre>
<Genre>rock</Genre>
</Song>
<Song>
<Title>2</Title>
<Genre>metal</Genre>
</Song>
</Album>
<Album>
<Name>dos</Name>
<Country>england</Country>
<Titulo>titulo2</Titulo>
<Song>
<Title>3</Title>
<Genre>pop</Genre>
<Genre>metal</Genre>
<Genre>soul</Genre>
</Song>
</Album>
<TVML>tmvl_02.xml</TVML>
</Music>
And the second one:
<?xml version="1.0"?>
<Music>
<Year>2003</Year>
<Album>
<Name>tres</Name>
<Country>morocco</Country>
<Titulo>titulo3</Titulo>
<Song>
<Title>4</Title>
<Genre>pop</Genre>
<Genre>soul</Genre>
</Song>
<Song>
<Title>5</Title>
<Genre>reggae</Genre>
</Song>
<Song>
<Title>6</Title>
<Genre>rock</Genre>
<Genre>pop</Genre>
</Song>
</Album>
<Album>
<Name>cuatro</Name>
<Country>france</Country>
<Titulo>titulo4</Titulo>
<Song>
<Title>7</Title>
<Genre>inide</Genre>
</Song>
</Album>
<Album>
<Name>cinco</Name>
<Country>england</Country>
<Titulo>titulo5</Titulo>
<Song>
<Title>10</Title>
<Genre>rock</Genre>
</Song>
<Song>
<Title>8</Title>
<Genre>metal</Genre>
<Genre>pop</Genre>
<Genre>ballad</Genre>
</Song>
<Song>
<Title>9</Title>
<Genre>rock</Genre>
</Song>
<Song>
<Title>11</Title>
<Genre>funk</Genre>
<Genre>pop</Genre>
</Song>
</Album>
</Music>
Thx in advance for your help and merry christmas.