I'm using the COBRA HTMLParser but haven't had luck parsing one particular tag. Here's the source:
<li id="eta" class="hentry">
<span class="body">
<span class="actions">
</span>
<span class="content">
</span>
<span class="meta entry">Content here
</span>
<span class="meta entry stub">Content here
<span class="shared-content">
Information by
<a class="title" data="associate" href="/associate">Associate</a>
</span>
</span>
</span>
</li>
I am able to use the following XPaths to get the proper information:
XPath xpath = XPathFactory.newInstance().newXPath();
NodeList nodeList = (NodeList) xpath.evaluate("//span[contains(@class, 'body')]", document, XPathConstants.NODESET);
int length = nodeList.getLength();
System.out.println(nodeList.getLength());
for(int i = 0; i < length; i++) {
Element element = (Element) nodeList.item(i);
NodeList n = null;
try {
n = (NodeList) xpath.evaluate("span[contains(@class, 'content')]", element, XPathConstants.NODESET);
String body = n.item(0).getTextContent();
System.out.println("Content: " + body);
} catch (Exception e) {};
try {
String date = (String) xpath.evaluate("span[contains(@class, 'meta entry')]/a/span/@data", element, XPathConstants.STRING);
System.out.println("DATA: " + date);
String source = (String) xpath.evaluate("//span[contains(@class, 'meta entry')]/span", element, XPathConstants.STRING);
System.out.println("DATA: " + source);
} catch (Exception e) {};
//This does not work at all! I've tried every combination and still can't get it to run
try {
String info = (String) xpath.evaluate("//span[@class='shared-content']/a/@data", element, XPathConstants.STRING);
System.out.println("INFO: " + info);
} catch (Exception e) {};
}
The last expression does not work whatever combination I try. I've tried the following too but it doesn't help,
String info = (String) xpath.evaluate("//span[contains(@class, 'shared-content')]/a/@data", element, XPathConstants.STRING);
String info = (String) xpath.evaluate("//span[contains(@class, 'meta entry info')]/span/a/@data", element, XPathConstants.STRING);
Any suggestions?
EDIT: There have been a couple of suggestions about the XML being illegal (which honestly I am not sure myself as to why it is illegal because I've seen it almost everywhere till now) but I don't have control over the XML though (at least until Monday till my other pals get back). I am trying to see the feasibility of writing a mashup including this information. Is there someway to disable checking or something?
Here's the XML that was parsed:
<?xml version="1.0" encoding="UTF-8"?>
<span class="body">
<span class="content">TextContent</span>
<span class="meta entry">TextContent</span>
</span>
I guess the document is not getting parsed correctly.