Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.
import com.meterware.httpunit.*;
public class Tables {
public static void main(String[] args) throws Exception {
HttpUnitOptions.setScriptingEnabled(false);
String START = "http://www.enniselectric.com/bid_list/index.htm";
WebConversation wc = new WebConversation();
WebResponse resp = wc.getResponse(START);
WebTable[] tables = resp.getTables();
System.out.printf("Found %d table(s) in the response\n", tables.length);
for (int r = 0; r < tables[0].getRowCount(); r++) {
for (int c = 0; c < tables[0].getColumnCount(); c++) {
//TableCell cell = tables[0].getTableCell(r, c);
//System.out.printf("%s ", cell.getText());
System.out.printf("%s ", tables[0].getCellAsText(r, c).trim());
}
System.out.println();
}
}
}
| Job Name
| Bid Date & Time
| Dulles Jet Center
| February 21, 2011 @ 2:00 P.M.
| NIST Cooling Tower Replacement
| February 22, 2011 @ 2:00 P.M.
| Catharpin Irrigation
| February 22, 2011 @ 1:00 P.M.
| Mt. Weather - Emergency Ops Center
| February 21, 2011 @ 2:00 P.M.
| AWG Firing Range, Ft. Meade
| February 28, 2011 @ 2:00 P.M
| Center for Strategic & International Studies
| March 7, 2011 @ 2:00 P.M.
|
import java.io.IOException;
import org.xml.sax.SAXException;
import org.w3c.dom.Attr;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.meterware.httpunit.*;
public class App
{
public static String escapeXML(String s) {
StringBuffer str = new StringBuffer();
int len = (s != null) ? s.length() : 0;
for (int i=0; i<len; i++) {
char ch = s.charAt(i);
switch (ch) {
case '<': str.append("<"); break;
case '>': str.append(">"); break;
case '&': str.append("&"); break;
case '"': str.append("""); break;
case '\'': str.append("'"); break;
default: str.append(ch);
}
}
return str.toString();
}
public static String print(Node node) {
String result = "";
int type = node.getNodeType();
switch (type) {
case Node.ELEMENT_NODE:
result += "<" + node.getNodeName();
NamedNodeMap attrs = node.getAttributes();
int len = attrs.getLength();
for (int i=0; i<len; i++) {
Attr attr = (Attr)attrs.item(i);
result += " " + attr.getNodeName() + "=\"" +escapeXML(attr.getNodeValue()) + "\"";
}
result += ">";
NodeList children = node.getChildNodes();
len = children.getLength();
for (int i=0; i<len; i++)
result += print(children.item(i));
result += "</" + node.getNodeName() + ">";
break;
case Node.ENTITY_REFERENCE_NODE:
result += "&" + node.getNodeName() + ";";
break;
case Node.CDATA_SECTION_NODE:
result += "<![CDATA[" + node.getNodeValue() + "]]>";
break;
case Node.TEXT_NODE:
result += escapeXML(node.getNodeValue());
break;
case Node.PROCESSING_INSTRUCTION_NODE:
result += "<?" + node.getNodeName();
String data = node.getNodeValue();
if (data!=null && data.length()>0)
result += " " + data;
result += "?>";
break;
}
return result;
}
public static void runIt(String url) {
HttpUnitOptions.setScriptingEnabled(false);
WebConversation wc = new WebConversation();
WebResponse resp;
try {
resp = wc.getResponse(url);
WebTable[] tables = resp.getTables();
System.out.printf("Found %d table(s) in the response\n", tables.length);
for (int i = 0; i < tables.length; i++) {
System.out.println("Table no "+(i+1));
System.out.println(print(tables[i].getNode()));
System.out.println("");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main( String[] args )
{
String url = "http://www.asu.edu/purchasing/bids/";
runIt(url);
}
}
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
Have a better answer? Share it in a comment.