The place from where try {
starts doesn't seems to be in a method. My guess is that you want that to be in main
method. Enclose the lines from try {
till the last-but-one }
in main
method
Here is the modified program:
public class XPlusShowTitleControllerCmdImpl extends ControllerCommandImpl implements XPlusShowTitleControllerCmd {
// ...
protected Node getNode(String tagName, NodeList nodes) {
for ( int x = 0; x < nodes.getLength(); x++ ) {
Node node = nodes.item(x);
if (node.getNodeName().equalsIgnoreCase(tagName)) {
return node;
}
}
return null;
}
protected String getNodeValue( Node node ) {
NodeList childNodes = node.getChildNodes();
for (int x = 0; x < childNodes.getLength(); x++ ) {
Node data = childNodes.item(x);
if ( data.getNodeType() == Node.TEXT_NODE )
return data.getNodeValue();
}
return "";
}
protected String getNodeValue(String tagName, NodeList nodes ) {
for ( int x = 0; x < nodes.getLength(); x++ ) {
Node node = nodes.item(x);
if (node.getNodeName().equalsIgnoreCase(tagName)) {
NodeList childNodes = node.getChildNodes();
for (int y = 0; y < childNodes.getLength(); y++ ) {
Node data = childNodes.item(y);
if ( data.getNodeType() == Node.TEXT_NODE )
return data.getNodeValue();
}
}
}
return "";
}
protected String getNodeAttr(String attrName, Node node ) {
NamedNodeMap attrs = node.getAttributes();
for (int y = 0; y < attrs.getLength(); y++ ) {
Node attr = attrs.item(y);
if (attr.getNodeName().equalsIgnoreCase(attrName)) {
return attr.getNodeValue();
}
}
return "";
}
protected String getNodeAttr(String tagName, String attrName, NodeList nodes ) {
for ( int x = 0; x < nodes.getLength(); x++ ) {
Node node = nodes.item(x);
if (node.getNodeName().equalsIgnoreCase(tagName)) {
NodeList childNodes = node.getChildNodes();
for (int y = 0; y < childNodes.getLength(); y++ ) {
Node data = childNodes.item(y);
if ( data.getNodeType() == Node.ATTRIBUTE_NODE ) {
if ( data.getNodeName().equalsIgnoreCase(attrName) )
return data.getNodeValue();
}
}
}
}
return "";
}
public static void main(String args[]) {
try {
DOMParser parser = new DOMParser();
String UrlToParse = "http://www.theurlhere.com";
parser.parse(UrlToParse);
Document doc = parser.getDocument();
// Get the document's root XML node
NodeList root = doc.getChildNodes();
// Navigate down the hierarchy to get to the X node
Node comp = getNode("XGuide", root);
Node exec = getNode("X", comp.getChildNodes() );
String execType = getNodeAttr("type", exec);
// Load the executive's data from the XML
NodeList nodes = exec.getChildNodes();
String showTitleAttr = getNodeValue("XTitle", nodes);
System.out.println("X title is: " + showTitleAttr);
}
catch ( Exception e ) {
e.printStackTrace();
}
}
}