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:*
> <?xml version="1.0" encoding="UTF-8"?>
> <Quiz>
> <QuizQuestion>
> <Question>Which are the right words to greet someone?</Question>
> <option1>Hello</option1>
> <option2>Hai</option2>
> <option3>Bye</option3>
> </QuizQuestion>
> <QuizQuestion type="Discription">
> <Question>When do you use the word who?</Question>
> <Answer></Answer>
> </QuizQuestion>
> <QuizQuestion>
> <Question>What is AM used for?</Question>
> <option1>Morning</option1>
> <option2>Evening</option2>
> </QuizQuestion>
> </Quiz>
>
> *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 [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en