Wrox Programmer Forums
|
BOOK: Beginning Android Application Development
This is the forum to discuss the Wrox book Beginning Android Application Development by Wei-Meng Lee; ISBN: 978-1-1180-1711-1
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Android Application Development section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 17th, 2012, 12:43 AM
Registered User
 
Join Date: May 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter 8: Parsing XML data

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!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Parsing an Xml with No Xml Declaration Zoheb XML 2 March 26th, 2011 05:31 AM
Parsing XML data into SQL Server timeware ASP.NET 2.0 Professional 0 March 1st, 2010 08:03 AM
XML::Simple parsing xml geek.shrek Perl 1 August 19th, 2009 10:32 PM
Chapter 2: ToDo Example - Error Parsing XML archergin BOOK: Professional Android Application Development ISBN: 978-0-470-34471-2 2 May 2nd, 2009 10:01 AM
Parsing XML Data from a remote site timeware XML 1 June 1st, 2006 08:25 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.