我有一个XML文件,我通过我的java程序解析并搜索节点内的特定子字符串。我的XML结构如下:
<Album>
<Title>The New Daylight</Title>
<AlbumArtist>Dash Berlin</AlbumArtist>
<Genre>House</Genre>
<Publisher>Sony</Publisher>
<AlbumYear>2012</AlbumYear>
<AlbumPrice>20</AlbumPrice>
<Song>
<Artist>Dash Berlin</Artist>
<SongTitle>Till the sky falls down</SongTitle>
<SongGenre>House</SongGenre>
<AlbumTitle>The New Daylight</AlbumTitle>
<Year>2011</Year>
<Price>10</Price>
</Song>
<Song>
<Artist>Dash Berlin</Artist>
<SongTitle>Better Half of Me</SongTitle>
<SongGenre>House</SongGenre>
<AlbumTitle>The New Daylight</AlbumTitle>
<Year>2012</Year>
<Price>10</Price>
</Song>
</Album>
<Album>
<Title>Problematic</Title>
<AlbumArtist>Muse</AlbumArtist>
<Genre>Rock</Genre>
<Publisher>EMI</Publisher>
<AlbumYear>2000</AlbumYear>
<AlbumPrice>10</AlbumPrice>
<Song>
<Artist>Muse</Artist>
<SongTitle>Head of the problematic</SongTitle>
<SongGenre>Rock</SongGenre>
<AlbumTitle>Muse Self Labeled</AlbumTitle>
<Year>1999</Year>
<Price>10</Price>
</Song>
</Album>
例如,我有一堆专辑,我搜索了他们所有人找到了正确的专辑。我想存储我在arraylist中找到的所有歌曲。我做过类似的事情:
NodeList nl = docEle.getElementsByTagName("Album");
for (int temp = 0; temp < nl.getLength(); temp++) {
Element al = (Element)nl.item(temp);
if (compareSearchwithAlbum(searchName, al)) {
NodeList nlist = docEle.getElementsByTagName("Song");
for (int i = 0; i < nlist.getLength(); i++) {
Element s = (Element)nlist.item(i);
searchedSongs.add(new Song (getString(s, "SongTitle"), getString(s, "AlbumTitle"), getInt(s, "Price"), getInt(s, "Year")));
// return or do something here
}
}
问题是,当我运行那个内循环时,它抓住了专辑之外的所有不正确的歌曲,我想知道如何只抓取特定专辑中的歌曲。例如,来自Dash Berlin艺术家的所有歌曲。