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).
Code Java:
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
1 Attachment(s)
Re: Error in setting node value
I have attached the screenshot for more clarity.
Attachment 583
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.