使用Xml解析XML文件时出错DOMParser:Android
问题描述:
我有XML文件。 XML文件的结构如下:使用Xml解析XML文件时出错DOMParser:Android
<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相册的歌词。为此,我写了代码,它显示了歌曲列表,但对于那首歌曲我不能提取歌词。
这里是我的函数来获取歌词:
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;
}
那么,什么是我的代码中的错误。我花了整整一天的时间来解决它。但不幸的是,我无法识别我的错误。请尽快解决我的问题。感谢名单。
答
使用此链接SAX parser
而且也见B/W SAX和DOM diffrence
的区别只是使用这个解析器来解析你的XML
public class EmployeeParser extends DefaultHandler {
private StringBuilder responseTag;
private List<Album> listAblum = new ArrayList<Album>();
private Album album;
@Override
public void startDocument() throws SAXException {
super.startDocument();
listAblum.clear();
}
@Override
public void endDocument() throws SAXException {
super.endDocument();
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
super.startElement(uri, localName, qName, attributes);
responseTag = new StringBuilder();
if (localName.equals("album")) {
album = new Album();
album.setAlbumValue(attributes.getValue("value"));
} else if ("imageid".equals(localName)) {
album.setDetails(attributes.getValue("value"));
} else if ("songs".equals(localName)) {
album.setSongs(attributes.getValue("value"));
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
super.endElement(uri, localName, qName);
if (localName.equals("album")) {
listAblum.add(album);
} else if ("lyrics".equals(localName)) {
album.setLyrics(responseTag.toString());
}
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
super.characters(ch, start, length);
String str = new String(ch, start, length);
responseTag.append(str);
}
class Album {
private String songs, imageId, lyrics, albumValue, details;
public String getSongs() {
return songs;
}
public void setSongs(String songs) {
this.songs = songs;
}
public String getImageId() {
return imageId;
}
public void setImageId(String imageId) {
this.imageId = imageId;
}
public String getLyrics() {
return lyrics;
}
public void setLyrics(String lyrics) {
this.lyrics = lyrics;
}
public String getAlbumValue() {
return albumValue;
}
public void setAlbumValue(String albumValue) {
this.albumValue = albumValue;
}
public String getDetails() {
return details;
}
public void setDetails(String details) {
this.details = details;
}
}
}
你为什么这样做DOM解析器而不是不使用更易于开发和调试的SAX解析器。 – Jitendra 2013-02-23 12:51:25
oh thanx ..你可以请发表代码使用SAX解析器访问子节点吗? – zanky 2013-02-23 12:52:48