Hi, I am stuck with a code from the book at page 292, which parses XML data and shows it on the screen using Toast.
The problem is, a for loop uses a variable which was nowhere to be found before that line, so eclipse couldn't run the code as well.
Code:
private void WordDefinition(String word)
{
InputStream in=null;
try
{
in = OpenHttpConnection("http://services.aonaware.com/DictService/DictService.asmx/Define?word=Apple");
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db;
try
{
db = dbf.newDocumentBuilder();
doc=db.parse(in);
}
catch(ParserConfigurationException e)
{
e.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
doc.getDocumentElement().normalize();
NodeList itemNodes = doc.getElementsByTagName("Definition");
String strDefinition = "";
for(int i=0; i < definitionElements.getLength(); i++)
{
Node itemNode =definitionElements.item(i);
if(itemNode.getNodeType()==Node.ELEMENT_NODE)
{
Element definitionElement = (Element) itemNode;
NodeList wordDefinitionElements = (definitionElement).getElementsByTagName("WordDefinition");
strDefinition = "";
for(int j=0; j<wordDefinitionElements.getLength(); j++)
{
Element wordDefinitionElement = (Element) wordDefinitionElements.item(j);
NodeList textNodes = ((Node)wordDefinitionElement).getChildNodes();
strDefinition+=((Node)textNodes.item(0)).getNodeValue()+". ";
}
Toast.makeText(getBaseContext(),strDefinition,Toast.LENGTH_LONG).show();
}
}
}
catch(IOException e)
{
Toast.makeText(this,e.getLocalizedMessage(),Toast.LENGTH_LONG).show();
}
}
This method is called with a string. The for loop which uses
defineElements has error because it wasn't declared before.
Did I miss anything? Please help!