Reputation: 6487
I'm new to XML Parsing. I am trying to access the "I Heart Quotes" API. This is the piece of code generating the error:
String link = "http://www.iheartquotes.com/api/v1/random.xml";
URL url = new URL(link);
InputStream is = url.openStream();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(is);`
And this is the error:
Content is not allowed in prolog.
org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:256) at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:345)
at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:121)
at com.nicolasekhoury.IHQuotes.IHQuotes.main(IHQuotes.java:28)
What should I do?
Upvotes: 0
Views: 117
Reputation: 2869
Accessing the mentioned resource gives an output similar to this:
You are fairminded, just and loving.
[fortune] http://iheartquotes.com/fortune/show/46886
This is not XML, since it is not well-formed.
What I think you should do depends. If this is just for learning go find a real XML source (e.g. your Stack Overflow user feed) and fiddle around with it. If you need to work with exactly this data source then look for something else than XML.
I just found that they offer HTML which is not XML but can work with an XML parser under certain circumstances. Read their docs and try accessing http://www.iheartquotes.com/api/v1/random?format=html
which will give you output similar to this:
<html>
<head>
<title>I Heart Quotes - Random Quote Widget</title>
<style type="text/css">/* ... */</style>
</head>
<body>
<table>
<tr>
<td>
<div class="rbroundbox">
<div class="rbtop"><div></div></div>
<div class="rbcontent">
<a target="_parent"
href='http://www.iheartquotes.com/fortune/show/halleys_comet_it_came_we_saw_we_drank'>
Halley's Comet: It came, we saw, we drank.
</a>
<div class="source">
<a target="_parent"
href="http://www.iheartquotes.com/fortune/rand?source=codehappy">[codehappy quote]</a>
</div>
</div><!-- /rbcontent -->
<div class="rbbot"><div></div></div>
</div><!-- /rbroundbox -->
</td></tr></table>
</body>
</html>
Upvotes: 0
Reputation: 1362
When I open http://www.iheartquotes.com/api/v1/random.xml in my browser, the are escaped symbols and I think it is not xml at all - it is just a freeform text.
Upvotes: 1