我已经尝试了很多不同的方法,但似乎无法在某一点上得到它,我得到了一个EOFException,但字符串歌词被记录下来,我真的很感激任何帮助
我的代码
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
org.w3c.dom.Document doc = docBuilder.parse(new URL(url).openStream());
NodeList nodeList = doc.getDocumentElement().getChildNodes();
Node node = nodeList.item(0);
String str = node.getAttributes().getNamedItem("Lyric").getNodeValue();
LogUtils.log(TAG, "Chart Lyrics : " + str);
来自要解析的url的xml
<GetLyricResult xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://api.chartlyrics.com/">
<TrackId>0</TrackId>
<LyricChecksum>dd03bcbe7914921ad7daec823f830d74</LyricChecksum>
<LyricId>1271</LyricId>
<LyricSong>Sweet Caroline</LyricSong>
<LyricArtist>Neil Diamond</LyricArtist>
<LyricUrl>
http://www.chartlyrics.com/1T8tpN5VBkKGw0-7VAQBjw/Sweet+Caroline.aspx
</LyricUrl>
<LyricCovertArtUrl>
http://images.amazon.com/images/P/B000002PBD.01.MZZZZZZZ.jpg
</LyricCovertArtUrl>
<LyricRank>9</LyricRank>
<LyricCorrectUrl>
http://www.chartlyrics.com/app/correct.aspx?lid=MQAyADcAMQA=
</LyricCorrectUrl>
<Lyric>
Where it began I can't begin to knowin' But then I know it's growing strong Was in the spring And spring became the summer Who'd have believed you'd come along? Hands, touchin' hands Reaching out Touching me Touching you Sweet Caroline Good times never seemed so good I've been inclined To believe it never would But now I Look at the night And it don't seem so lonely We fill it up with only two And when I hurt Hurtin' runs off my shoulders How can I hurt when holding you Warm, touchin' warm Reachin' out Touching me Touching you Sweet Caroline Good times never seem so good I've been inclined To believe they never would Oh, no, no Sweet Caroline Good times never seemed so good Sweet Caroline I believed they never could Sweet Caroline
</Lyric>
</GetLyricResult>
我现在使用此代码来解析XML
public String getLyricFromChartLyrics(String url) {
String lyric = null;
try {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
org.w3c.dom.Document doc = docBuilder.parse(url);
NodeList nodeList = doc.getElementsByTagName("GetLyricResult");
Node node = nodeList.item(0);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) node;
lyric = eElement.getElementsByTagName("Lyric").item(0).getTextContent();
LogUtils.log(TAG, "Element accepted " + lyric);
}
} catch (SAXException e) {
LogUtils.log(TAG, "SAX Exception");
} catch (IOException e) {
e.printStackTrace();
LogUtils.log(TAG, "IO Exception " + e.getLocalizedMessage());
} catch (ParserConfigurationException e) {
LogUtils.log(TAG, "ParserConfigurationException");
}
return lyric;
}
这个方法几乎可以正常,因为歌词被打印到日志中,但我得到了一个EOFException
这是我正在使用的网址
http://api.chartlyrics.com/apiv1.asmx/SearchLyricDirect?artist=michael%20jackson&song=bad
答案 0 :(得分:1)
我假设GetLyricResult是根元素。试试这个:
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
org.w3c.dom.Document doc = docBuilder.parse(url);
Element root = doc.getDocumentElement();
NodeList nodeList = root.getElementsByTagName("Lyric");
Node node = nodeList.item(0);
if (node.getNodeType() == Node.ELEMENT_NODE) {
lyric = node.getTextContent();
LogUtils.log(TAG, "Element accepted " + lyric);
}