Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Problems reading XML file -- casting problem

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Problems reading XML file -- casting problem

    Well as you can probably notice this is my very first post in this forum so i do not really have any idea how to do this properly.
    Below i copied and pasted the full code of one of my little programs.

    The error message shown if i try to launch the program is "Exception in thread "main" java.lang.ClassCastException: com.sun.org.apache.xerces.internal.dom.DeferredTex tImpl cannot be cast to org.w3c.dom.Element
    at studying.MoviesTrying.main(MoviesTrying.java:20)"


    import javax.xml.parsers.*;
    import org.xml.sax.*;
    import org.w3c.dom.*;  // imports
     
    import java.text.*;
     
     
     
    public class MoviesTrying
    {
    	private static NumberFormat cf = NumberFormat.getCurrencyInstance();
     
    	public static void main(String[] args)
    	{
     
    		Document doc = getDocument("C:\\Users\\Edi\\Desktop\\Chris\\Program\\eclipse\\eclipse\\workspace\\studies\\movies.xml");
    		//above i use my own method getDocument to create a doc
     
     
    		Element root = (Element)doc.getDocumentElement(); 		//at this line i get the error (Line 20) --
                                                                                                    //com.sun.org.apache.xerces.internal.dom.DeferredTextImpl cannot                                               [#]                                                                                            //be cast to org.w3c.dom.Element
    												//at studying.MoviesTrying.main(MoviesTrying.java:20)
     
     
    		Element movieElement =  (Element) root.getFirstChild(); //the rest should not be hard to figure out
    																// i use the getMovies method i created below to retrieve
    																//  year, title and price of the movies and move on to the next
    																// element (in the loop)
    		Movie m;
    		while (movieElement != null)
    		{
    			m = getMovies(movieElement);
    			String msg = Integer.toString(m.year);
    			msg += " : " + m.title;
    			msg += " ( " + cf.format(m.price) + ").";
    			System.out.println(msg);
    			movieElement = (Element)movieElement.getNextSibling();
    		}
    	}
     
     
    	private static Document getDocument(String name)   //a method to return a doc builder 
    	{
    		try 
    		{
    			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    			factory.setIgnoringComments(true);
    			factory.setIgnoringElementContentWhitespace(true);
    	//		factory.setValidating(true);
     
    			DocumentBuilder builder = factory.newDocumentBuilder();
    			return builder.parse(new InputSource(name));
    		}catch(Exception e)
    		{
    			System.out.println(e.getMessage());
    		}
    		return null;
     
    	}
     
    	private static Movie getMovies(Element e)      // getting the values of the Elements (year is an attribute) from my .xml file
     
    	{
    		String yearString = e.getAttribute("year");
    		int year = Integer.parseInt(yearString);
     
    		Element tElement = (Element)e.getFirstChild();
    		String title = getTextValue(tElement).trim();
     
    		Element pElement = (Element)tElement.getNextSibling();
    		String pString = getTextValue(pElement).trim();
    		double price = Double.parseDouble(pString);
     
    		return new Movie(title,year,price);
    	}
     
    	private static String getTextValue(Node n)
    	{
    		return n.getFirstChild().getNodeValue();
     
    	}
     
    	private static class Movie 
    	{
    		public String title;
    		public int year;
    		public double price;
     
    		public Movie(String title, int year, double price)
    		{
    			this.title = title;
    			this.year = year;
    			this.price = price;
    		}
     
    	}
     
     
    }
    Last edited by jps; August 13th, 2013 at 04:22 PM. Reason: code tags


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Problems reading XML file -- casting problem

    DeferredTextImpl inherits from the Node class, not the Element class. You cannot cast to Element.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. The Following User Says Thank You to aussiemcgr For This Useful Post:

    Christian Egger (August 14th, 2013)

  4. #3
    Junior Member
    Join Date
    Aug 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problems reading XML file -- casting problem

    ouhw how have i been that dumb. Great thanks

Similar Threads

  1. Problems reading a pgm-file (pixel gray map).
    By morob05 in forum File I/O & Other I/O Streams
    Replies: 11
    Last Post: December 25th, 2012, 07:54 AM
  2. [SOLVED] Reading ip and port from xml file
    By learn_java in forum Java Networking
    Replies: 0
    Last Post: November 19th, 2012, 09:08 AM
  3. Reading XML File using DOMParser and have problem with accessing xml
    By optiMystic23 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 21st, 2012, 02:22 PM
  4. End Element exception reading XML file
    By treshr in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 5th, 2011, 12:58 AM
  5. Anyone Help me in XML file Reading??????????
    By goplrao in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: May 2nd, 2010, 11:04 AM

Tags for this Thread