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: Error in setting node value

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Error in setting node value

    Dear all,
    I'm trying to create an xml and i'm facing an error whike trying to set an node value using the code ,element.setTextContext.

    The error that i'm getting while trying to compile is "The method setTextContent(String) is undefined for the type Element".The quick fix solution of casting doesn't help solve the issue.

    What is more interesting is, when i create a new project in the same IDE instance, the same code works fine. Hence i am guessing the problem must be with the Jars or the class path variables. Since i'm new to this programming language and this being my first week, i'd greatly appreciate any help. I have pasted the code below for those who need to take a look. Also the IDE i'm using is RAD 7.5 and the compiler version is 1.4( changing it to 1.5 helps in solving the error, however i'm hard pressed to use 1.4 for project purposes, also the new project runs fine under 1.4 anyways).

    package com.dm.seo;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.util.*;
    import javax.xml.parsers.*;
    import javax.xml.transform.*;
    import javax.xml.transform.dom.DOMSource;
    import javax.xml.transform.stream.StreamResult;
    import org.w3c.dom.*;
     
    public class CreateSiteMap {
    	public static void main(String ar[])
    	{
     
    		ArrayList siteMap= new ArrayList();
    		siteMap.add("http://www.google.com");
    		siteMap.add("http://www.Bing.com");
    		siteMap.add("http://www.ask.com");
    		siteMap.add("http://www.Yahoo.com");
     
    		try{
    			//Create instance of DocumentBuilderFactory
    		    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    		    //Get the DocumentBuilder
    		    DocumentBuilder docBuilder = factory.newDocumentBuilder();
    		    //Create blank DOM Document
    		        Document doc = docBuilder.newDocument();
     
    		    //create the urlset root element with attribute
    		        Element root = doc.createElement("urlset");
    		        root.setAttribute("xmlns","http://www.sitemaps.org/schemas/sitemap/0.9");
    		    //all it to the xml tree
    		        doc.appendChild(root);     
     
    		     //create url location as child element 
    		     for (int i = 0; i < siteMap.size(); i++) {
    		    	//create url child element
    				 Element childElement = doc.createElement("url");
    				    //Add the atribute to the child
    		    	//create url location as child element 
    		    	 Element grandchildElement= doc.createElement("loc");				    
    			      ((Object) grandchildElement).setTextContent(sitemap.get(i)); ==> This line throws the above said error.
    			      childElement.appendChild(grandchildElement);
    			      root.appendChild(childElement);				     
     
    			}    
     
     
     
    		    TransformerFactory tranFactory = TransformerFactory.newInstance(); 
    		    Transformer aTransformer = tranFactory.newTransformer(); 
     
    		    Source src = new DOMSource(doc); 
     
    		    File file = new File("c:\\Sitemap.xml");
    	                 StreamResult result = new StreamResult(file);
    		    aTransformer.transform(src, (javax.xml.transform.Result) result); 
    		    }
     
    		catch(Exception e){
    		      System.out.println(e.getMessage());
    		    }
    	}
    	}
    Thanks and Regards,
    Praveen


  2. #2
    Junior Member
    Join Date
    Jun 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Error in setting node value

    I have attached the screenshot for more clarity.
    error.jpg

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Error in setting node value

    Please copy and paste the exact error message you get when you compile the code. I get a different error message when I compile it. It's important that you post the exact error message. Copy and paste it, don't type it in.

Similar Threads

  1. Node swap (move up)
    By raphytaffy in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 2nd, 2010, 06:56 PM
  2. [SOLVED] XML get/set node by attribute value
    By b_jones10634 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: August 24th, 2010, 11:26 AM
  3. TreeNode vs. Node
    By Kumarrrr in forum Java Theory & Questions
    Replies: 1
    Last Post: March 27th, 2010, 06:06 AM
  4. Printing A TreeNode/Node To Console..
    By Kumarrrr in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: March 17th, 2010, 12:35 AM
  5. Xml-Node Retrieval
    By prasb in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: December 4th, 2009, 12:44 PM

Tags for this Thread