I need a little guidance with reading from a URL XHTML page in java:
Here's my best try to print a specific String:
try {
URL item = new URL("url");
URLConnection connect = item.openConnection();
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc= dBuilder.parse(connect.getInputStream());
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("tag");
for(int temp = 0; temp<nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if(nNode.getNodeType() == Node.ELEMENT_NODE) {
Element el = (Element) nNode;
System.out.println((el.getElementsByTagName("wantedElement").item(0).getTextContent()));
}}
}catch(IOException | ParserConfigurationException | SAXException e) {
e.printStackTrace();
}
Response from Eclipse:
[Fatal Error] :1:1: Content is not allowed in prolog.
org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.
XHTML sample I'm trying to parse (from TD Ameritrade API):
<CandleList>
<candles>
<candles>
<open>45.97</open>
<high>46.26</high>
<low>45.8</low>
<close>46.0</close>
<volume>7176781</volume>
<datetime>1496293200000</datetime>
</candles>
<candles>
<open>46.22</open>
<high>46.86</high>
<low>45.9</low>
<close>46.8</close>
<volume>9523927</volume>
<datetime>1496379600000</datetime>
</candles>
I appreciate any help!