XPath cannot find table by id
I am doing screen scripting with WATIJ, but it cannot read HTML tables (throws NullPointerExceptions or UnknownObjectExceptions). To overcome this, I read the HTML and ran it through JTidy to get well-formed XML.
I want to parse it using XPath, but it can't find <table ...>
on id
, although the table is present in the XML plain as day. Here is my code:
XPathFactory factory=XPathFactory.newInstance();
XPath xPath=factory.newXPath();
InputSource inputSource = new InputSource(new StringReader(tidyHtml));
XPathExpression xPathExpression=xPath.compile("//table[@id='searchResult']");
String expression = "//table[@id='searchResult']";
String table = xPath.evaluate(expression, inputSource);
System.out.println("table = " + table);
The table is an empty string.
However, the table is in XML. If I print a tidyHtml
String it shows
<table
class="ApptableDisplayTag"
id="searchResult"
style="WIDTH: 99%">
I haven't used XPath before, so maybe I am missing something.
Can anyone customize me directly? Thanks.
a source to share
I don't know anything about JTidy, but I'm for WATIJ, I believe the reason you are getting NullPointer and UnknownObject exceptions is because your XPATH is using bottom node nodes. So say you are using "// table [@id = 'searchResult']" as the xpath to find a table in WATIJ. It won't actually work because "table" is in lower case. For WATIJ you need to have all node names in uppercase, for example: "// TABLE [@id = 'searchResult']". For example, if you wanted to print the number of rows of this table using WATIJ, you would do the following:
import watij.runtime.ie.IE;
import static watij.finders.SymbolFactory. *;
public class Example {
public static void main (String [] args) {
IE ie = new IE ();
ie.start ("your_url_goes_here");
System.out.println (ie.table (xpath, "// TABLE [@ id = 'searchResult']"). RowCount ());
ie.close ();
}
}
This code or answer may not be correct as I just started using WATIJ today. Although I faced this same issue with xpaths. Tried a couple of hours of searching / testing before I noticed how all the xpaths were cased on this page: WATIJ User Guide Once I changed the corpus in my xpaths, WATIJ was able to find the objects, so it should work for you too.
a source to share
I have never used the XPath API directly, I have always used it via dom4j or other languages โโ(Perl and C), but I am well aware of how it works fine. You should probably parse the input as a DOM document first, this will help a lot. Also, if you know your document has an ID, you have to parse it with a DTD or schema download that describes it in this way, the XML parser will mark and identify nodes with matching IDs. Once you have done that, you can use your code with the DOM tree.
Documentation [XPath.evaluate (expression, item)] ( http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/xpath/XPath.html#evaluate(java.lang.String, % 20java.lang.Object) indicates that the second element must be a Node or NodeList, which is probably why you have many UnknownObjectExceptions.
If your XML parser can recognize ID elements, you can access the ID element with the following XPath expression:
XPathExpression xPathExpression=xPath.compile("id('searchResult')");
xPathExpression.evaluate(document); // document is a DOM document instance
Using the XPath id () function is the most efficient way to access elements, that is, when the elements use an identifier and have been declared this way in a DTD or Schema.
a source to share
It looks like the problem is mostly JTidy related. I can get the xpath to parse the JTidy-s result by doing the following:
Remove all "& amp> nbsp;" JTidy returns xhtml with <& amp> nbsp; "outside the tags. Remove In the tag, remove the xmlns = ... attribute. Remove the" head "tags. (I'm using some funny formatting because HTML objects won't display when typed correctly)
JTidy also puts newlines in the middle of the text content if ... elements.
I need to look at other options for converting HTML -> XML. I quickly asked for Cobra, but I also couldn't find the Id table. I haven't tried manually clearing the result from Cobra, so I don't know how it compares to JTidy.
If you know about an HTML parser that returns good XML, please let me know.
a source to share