XMLファイルを持っています。XMLファイルの構造はここにあります:
<albumlist>
<album value="Enrique Iglesias"><details value="1995"/>
<imageid value="eenrique"/>
<songs value="No Llores Por Mi">
<lyrics>hhhehhehhehe </lyrics>
</songs>
<songs value="Trapecista">
<lyrics>hhhehhehhehe </lyrics>
</songs>
<songs value="Por Amarte">
<lyrics>hhhehhehhehe </lyrics>
</songs>
</album>
</albumlist>
perticulerアルバムの特定の曲の歌詞を抽出したい。そのために私は曲のリストを表示するコードを書きましたが、そのperticulerの曲については歌詞を抽出できません。
これが歌詞を取得するための私の関数です:
private String[] getLyrics() {
// TODO Auto-generated method stub
try {
InputStream in = getResources().openRawResource(R.raw.data);
DocumentBuilder builder = DocumentBuilderFactory.newInstance()
.newDocumentBuilder();
Document doc = builder.parse(in, null);
NodeList albumnode = doc.getElementsByTagName("album");
for (int k = 0; k < albumnode.getLength(); k++)
{
if (((Element) albumnode.item(k)).getAttribute("value").equals(albumname))
{
NodeList nd = albumnode.item(k).getChildNodes();
for (int j = 0; j < nd.getLength(); j++) {
Node subNode = nd.item(j);
if (subNode.getNodeName().equals("songs")) {
if (subNode.hasAttributes()) {
NamedNodeMap nnm = subNode.getAttributes();
for (int i = 0; i < nnm.getLength(); i++) {
Node attrNode = nnm.item(i);
if (attrNode.getNodeType() == Node.ATTRIBUTE_NODE) {
Attr attribute = (Attr) attrNode;
String value = attribute.getValue();
if (value.equals(songname)) {
NodeList lyricslist = subNode
.getChildNodes();
for(int m=0 ; m<lyricslist.getLength();m++)
{
Node lyricsnode = lyricslist.item(m);
if (lyricsnode.getNodeType() == Node.TEXT_NODE) {
//Attr attribute = (Attr) attrNode;
String value1 = lyricsnode.getNodeValue();
Lyrics[0]=value1;
Toast.makeText(getApplicationContext(), ""+value1, Toast.LENGTH_LONG).show();
}
}
}
}
}
}
}
}
}
}
in.close();
}
catch (Exception t) {
Toast.makeText(this, "Exception: " + t.getMessage(),
Toast.LENGTH_LONG).show();
}
return Lyrics;
}
それで、私のコードの間違いは何ですか。私はそれを解決するために一日を過ごしました。しかし、残念ながら私は自分の間違いを特定することができません。Pleazeは私の問題を解決します。ありがとう。