Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8818523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:07:40+00:00 2026-06-14T05:07:40+00:00

Here is my code: // get the factory DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); try {

  • 0

Here is my code:

// get the factory
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

try {

    // Using factory get an instance of document builder
DocumentBuilder db = dbf.newDocumentBuilder();

// parse using builder to get DOM representation of the XML file
    dom = db.parse(file);

} catch (ParserConfigurationException pce) {
    pce.printStackTrace();
} catch (SAXException se) {
    se.printStackTrace();
} catch (IOException ioe) {
    ioe.printStackTrace();
}

NodeList n1 = dom.getChildNodes();
Element e1 = (Element) n1.item(0);

System.out.println(n1.getLength());
System.out.println(e1.getNodeName());

NodeList n2 = n1.item(0).getChildNodes();
Element e2 = (Element) n2.item(0);   //Line 61

System.out.println(n2.getLength());
System.out.println(e2.getNodeName());

Here is my XML file:

<?xml version="1.0" encoding="utf-8"?>

<test-fw:test
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:test-fw="http://simitar/test-fw"> 

<rule-tree>
<rule class="matchlines">
<property name="contiguous"> true</property>
<property name="inOrder">false</property>
<property name="exact">false</property>
<property name="lines">modelInstantiated</property>
</rule>
<rule class="matchlines">
<property name="contiguous"> true</property>
<property name="inOrder">true</property>
<property name="exact">false</property>
<property name="lines">InitEvent</property>
</rule>
</rule-tree>

</test-fw:test>

Here is my output:

1
test-fw:test
Exception in thread "main" java.lang.ClassCastException: com.sun.org.apache.xerces.internal.dom.DeferredTextImpl cannot be cast to org.w3c.dom.Element
    at testpack.Main.run(Main.java:61)
    at testpack.Main.main(Main.java:86)

I keep getting this error. I am completely lost. I have no idea what to do. I want to able to have one node, and be able to grab all it’s children and put them into an array or list, so I can iterate through them.

Here are all my imports:

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Stack;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

I’ve had the hardest time trying to get this Java to parse this XML file.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T05:07:41+00:00Added an answer on June 14, 2026 at 5:07 am
    NodeList n1 = dom.getChildNodes();
    Element e1 = (Element) n1.item(0);
    

    The node is not an Element, but a Node.

    Try this:

    Node no1 = (Node) n1.item(0);
    

    Nodes can be text nodes or elements, for example. In particular,

    <root>
    <element/>
    </root>
    

    is 4 nodes. A root element, a text node containing \n, the element element and another text node containing \n.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

here is my code:(get file line num and word count) import System.IO import Data.Maybe
Here is my code to get Notes from AddressBook. +(NSString*)getNote:(ABRecordRef)record { return ABRecordCopyValue(record, kABPersonNoteProperty);
My code here works fine to get the content length, and tells me how
Here is my jQuery code: $.get('/Home/GetList', function(data) { debugger; $('#myMultiSelect').val(data); }); Here is my
Editing this code here on Stackoverflow and I'm really near to get the result
I am trying to get the code found here: http://snipplr.com/view/26643/mbprogresshud-with-an-asynchronous-nsurlconnection-call/ to work in my
I cannot get this to work, here is code that I found in another
I am trying to get an HTML-based recursive directory listing based on code here:
I've stuck into some weird problem. Here is the code AccountsController.cs // GET /api/accounts
Here's my code I'm trying to get working to no avail: CREATE DEFINER=`auser`@`localhost` PROCEDURE

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.