[android-developers] Re: xml parsing error

2012-11-26 Thread bob
 

Not all Nodes are Elements.


Apparently, that node is not an element.



On Thursday, November 22, 2012 6:15:36 AM UTC-6, Ananda Krishna wrote:
>
> Hi
> I am trying to parse an xml file.But I am getting an exception. 
> error Message :org.apache.harmony.xml.dom.TextImpl cannot be cast to 
> org.w3c.dom.Element
>
> *Code is as follows:*
> *Xml File:*
> 
> 
>   
>   Which are the right words to greet someone?
>   Hello
>   Hai
>   Bye
>   
> 
>   When do you use the word who?
>   
>   
>  
>   What is AM used for?
>   Morning
>   Evening
>   
> 
>
> *Java code for Parsing:*
> DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
> DocumentBuilder builder = factory.newDocumentBuilder();
> Document doc = builder.parse(new URL(filepath).openStream());
> doc.getDocumentElement().normalize();
> NodeList questionList = doc.getElementsByTagName("QuizQuestion");
> int len = questionList.getLength();
> if (len > 0) {
> for (int i = 0; i < len; i++) {
> Node node = questionList.item(i);
> if (node.getNodeType() == Node.ELEMENT_NODE) {
> Element element = (Element) node;
> int childrenCount = element.getChildNodes().getLength();
> Node child = element.getFirstChild();
> int j = 0;
> while (child != null && j < childrenCount) {
>  * Element element1 = (Element) child;*
> if 
> (element1.getTagName().startsWith("term")) {
> String term = 
> String.valueOf(element1.getTextContent());
> System.out.println(term);
> }
> }
> }
> }
> }
> In the highlighted line exception is been raised.
> Kindly tell me where am i going wrong.
> Regards,
> AnandaKrishna S
>

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

[android-developers] Re: xml parsing error

2012-11-22 Thread Piren
I haven't checked your code very throughly (its no fun trying to read xml 
parsing code) but that error seems to indicate you're trying to cast a 
value into an xml element.
or as the Inception people said: you went too deep :)

you should do the same type checking you do in the level above it. first 
verify what the child is, then do something with it.



On Thursday, November 22, 2012 2:15:36 PM UTC+2, Ananda Krishna wrote:
>
> Hi
> I am trying to parse an xml file.But I am getting an exception. 
> error Message :org.apache.harmony.xml.dom.TextImpl cannot be cast to 
> org.w3c.dom.Element
>
> *Code is as follows:*
> *Xml File:*
> 
> 
>   
>   Which are the right words to greet someone?
>   Hello
>   Hai
>   Bye
>   
> 
>   When do you use the word who?
>   
>   
>  
>   What is AM used for?
>   Morning
>   Evening
>   
> 
>
> *Java code for Parsing:*
> DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
> DocumentBuilder builder = factory.newDocumentBuilder();
> Document doc = builder.parse(new URL(filepath).openStream());
> doc.getDocumentElement().normalize();
> NodeList questionList = doc.getElementsByTagName("QuizQuestion");
> int len = questionList.getLength();
> if (len > 0) {
> for (int i = 0; i < len; i++) {
> Node node = questionList.item(i);
> if (node.getNodeType() == Node.ELEMENT_NODE) {
> Element element = (Element) node;
> int childrenCount = element.getChildNodes().getLength();
> Node child = element.getFirstChild();
> int j = 0;
> while (child != null && j < childrenCount) {
>  * Element element1 = (Element) child;*
> if 
> (element1.getTagName().startsWith("term")) {
> String term = 
> String.valueOf(element1.getTextContent());
> System.out.println(term);
> }
> }
> }
> }
> }
> In the highlighted line exception is been raised.
> Kindly tell me where am i going wrong.
> Regards,
> AnandaKrishna S
>

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

[android-developers] Re: XML Parsing Error

2008-10-18 Thread JavaAndroid

Hi Ludwig,
Thanks for ur Response.. i added that entry. After adding  tag in androidmanifest.xml, i was getting
UnknownHostException...it was basically due to URL classI replaced
everything with HTTPClient classes

Here is my modified class and it worked me.
import java.io.InputStream;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.HttpConnection;
import org.apache.commons.httpclient.HttpConnectionManager;
import org.apache.commons.httpclient.HttpState;
import org.apache.commons.httpclient.HttpURL;
import org.apache.commons.httpclient.SimpleHttpConnectionManager;
import org.apache.commons.httpclient.methods.PostMethod;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class ParsingXML extends Activity {

private final String DEBUG_TAG = "ParsingXML";
protected HttpConnectionManager connectionManager = new
SimpleHttpConnectionManager();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
String baseURL = "http://www.anddev.org/images/tut/basic/
parsingxml/example.xml";
HttpConnection connection = null;
try{
HttpURL httpURL = new HttpURL(baseURL);
HostConfiguration host = new HostConfiguration();
host.setHost(httpURL.getHost(), httpURL.getPort());
connection = connectionManager.getConnection(host);
Log.d("MyLogger", "Before Opening Conn");
connection.open();
PostMethod postMethod = new PostMethod(baseURL);
postMethod.execute(new HttpState(), connection);
InputStream response = postMethod.getResponseBodyAsStream();
Log.d("MyLogger", response.toString());
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xmlReader = sp.getXMLReader();
ExampleHandler exampleHandler = new ExampleHandler();
xmlReader.setContentHandler(exampleHandler);
xmlReader.parse(new InputSource(response));
ParsedExampleDataSet parsedExampleDataSet =
exampleHandler.getParsedData();
tv.setText(parsedExampleDataSet.toString());

}catch(Exception e){
tv.setText("Error: " + e.getMessage());
Log.e(DEBUG_TAG, "ParsingXMLError", e);
}
this.setContentView(tv);
}
}

 i got response from that Specified URL

Thanks
JavaAndroid



On Oct 18, 9:37 pm, Ludwig <[EMAIL PROTECTED]> wrote:
> I think you can see from your stack that you do not even get near your XML
> parsing code.It is my impression that you fail open the Http connection to
> get the file.
> This might be because you have not given permission to your app to access
> the internet.
>
> Could you check that you have
>           
> in your AndroidManifest.xml?
>
> Ludwig
>
> 2008/10/18 JavaAndroid <[EMAIL PROTECTED]>
>
>
>
>
>
> > Hi All,
> > I m just tried XML parsing in Android activity class...When i run the
> > application i m getting unKnown Error in Screen...
>
> > This is my Activity Class
> > import java.net.URL;
>
> > import javax.xml.parsers.SAXParser;
> > import javax.xml.parsers.SAXParserFactory;
>
> > import org.xml.sax.InputSource;
> > import org.xml.sax.XMLReader;
>
> > import android.app.Activity;
> > import android.os.Bundle;
> > import android.util.Log;
> > import android.widget.TextView;
>
> > public class ParsingXML extends Activity {
>
> >        private final String DEBUG_TAG = "ParsingXML";
> >    /** Called when the activity is first created. */
> >   [EMAIL PROTECTED]
> >    public void onCreate(Bundle savedInstanceState) {
> >        super.onCreate(savedInstanceState);
> >        TextView tv = new TextView(this);
> >        try{
> >                URL url = new URL("http://www.anddev.org/images/tut/basic/
> > parsingxml/example.xml
> > ");
>
> >                SAXParserFactory spf = SAXParserFactory.newInstance();
> >                SAXParser sp = spf.newSAXParser();
>
> >                XMLReader xmlReader = sp.getXMLReader();
> >                ExampleHandler exampleHandler = new ExampleHandler();
>
> >                xmlReader.setContentHandler(exampleHandler);
> >                xmlReader.parse(new InputSource(url.openStream()));
>
> >                ParsedExampleDataSet parsedExampleDataSet =
> > exampleHandler.getParsedData();
>
> >                tv.setText(parsedExampleDataSet.toString());
>
> >        }catch(Exception e){
> >                tv.setText

[android-developers] Re: XML Parsing Error

2008-10-18 Thread Ludwig
I think you can see from your stack that you do not even get near your XML
parsing code.It is my impression that you fail open the Http connection to
get the file.
This might be because you have not given permission to your app to access
the internet.

Could you check that you have
  
in your AndroidManifest.xml?

Ludwig

2008/10/18 JavaAndroid <[EMAIL PROTECTED]>

>
> Hi All,
> I m just tried XML parsing in Android activity class...When i run the
> application i m getting unKnown Error in Screen...
>
> This is my Activity Class
> import java.net.URL;
>
> import javax.xml.parsers.SAXParser;
> import javax.xml.parsers.SAXParserFactory;
>
> import org.xml.sax.InputSource;
> import org.xml.sax.XMLReader;
>
> import android.app.Activity;
> import android.os.Bundle;
> import android.util.Log;
> import android.widget.TextView;
>
> public class ParsingXML extends Activity {
>
>private final String DEBUG_TAG = "ParsingXML";
>/** Called when the activity is first created. */
>@Override
>public void onCreate(Bundle savedInstanceState) {
>super.onCreate(savedInstanceState);
>TextView tv = new TextView(this);
>try{
>URL url = new URL("http://www.anddev.org/images/tut/basic/
> parsingxml/example.xml
> ");
>
>SAXParserFactory spf = SAXParserFactory.newInstance();
>SAXParser sp = spf.newSAXParser();
>
>XMLReader xmlReader = sp.getXMLReader();
>ExampleHandler exampleHandler = new ExampleHandler();
>
>xmlReader.setContentHandler(exampleHandler);
>xmlReader.parse(new InputSource(url.openStream()));
>
>ParsedExampleDataSet parsedExampleDataSet =
> exampleHandler.getParsedData();
>
>tv.setText(parsedExampleDataSet.toString());
>
>}catch(Exception e){
>tv.setText("Error: " + e.getMessage());
>Log.e(DEBUG_TAG, "ParsingXMLError", e);
>}
>this.setContentView(tv);
>}
> }
>
> And my ExampleHandler class
>
> is
> import org.xml.sax.Attributes;
> import org.xml.sax.SAXException;
> import org.xml.sax.helpers.DefaultHandler;
>
> public class ExampleHandler extends DefaultHandler{
>
>private boolean in_outertag = false;
>private boolean in_innertag = false;
>private boolean in_mymytag = false;
>
>private ParsedExampleDataSet myParsedExampleDataSet = new
> ParsedExampleDataSet();
>
>public ParsedExampleDataSet getParsedData(){
>return this.myParsedExampleDataSet;
>}
>
>public void startDocument() throws SAXException{
>this.myParsedExampleDataSet = new ParsedExampleDataSet();
>}
>
>public void endDocument(){
>
>}
>
>public void startElement(String nameSpaceURI, String localName,
> String qName, Attributes atts) throws SAXException{
>if(localName.equals("outertag")){
>this.in_outertag = true;
>}else if(localName.equals("innertag")){
>this.in_innertag = true;
>}else if(localName.equals("myTag")){
>this.in_mymytag = true;
>}else if(localName.equals("tagwithnumber")){
>String attributeValue = atts.getValue("thenumber");
>int attr = Integer.parseInt(attributeValue);
>myParsedExampleDataSet.setExtractedInt(attr);
>}
>}
>
> public void endElement(String namespaceURI, String
> localName,
> String qName) throws SAXException{
> if(localName.equals("outertag")){
> this.in_outertag = false;
> }else if(localName.equals("innertag")){
> this.in_innertag = false;
> }else if(localName.equals("myTag")){
>this.in_mymytag = true;
>}else if(localName.equals("tagwithnumber")){
> }
>}
>
> public void characters(char ch[],int start, int end){
> if(this.in_mymytag){
>
> myParsedExampleDataSet.setExtractedString(new
> String(ch,start,end));
> }
> }
>
> }
>
>
> ParsedExampleDataSet class is this
> public class ParsedExampleDataSet {
>private String extractedString = null;
>private int extractedInt = 0;
>
>public void setExtractedString(String extractedString){
>this.extractedString = extractedString;
>}
>
>public String getExtractedString(){
>return this.extractedString;
>}
>
>public void setExtractedInt(int extractedInt){
>this.extractedInt = extractedInt;
>}
>
>