0

I have tried loads of different methods but cant seem to get it right at one point i got a EOFException, but the string lyric was logged, I would really appreciate any help

My code

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);

xml from url to be parsed

<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>

I am now using this code to parse the 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;
}

This method almost works as the lyrics are being printed to the log but i am then getting a EOFException

Here is the url i am using

 http://api.chartlyrics.com/apiv1.asmx/SearchLyricDirect?artist=michael%20jackson&song=bad
2
  • I have tried JSOUP and i keep getting a EOFException Commented May 27, 2016 at 19:21
  • No issue here with your code in standard Java. Commented May 27, 2016 at 21:33

1 Answer 1

1

I'm assuming that GetLyricResult is the root element. Try this :

    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);
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Merve I have just rebooted the computer ran again with your method and i'm getting NO errors... brilliant thanks so much

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.