| Author |
error while creating XML doc through java code
|
apeksha agarwal
Greenhorn
Joined: Oct 28, 2011
Posts: 12
|
|
hello,
I am trying to create a XML Document with the help of java Code. but it is giving me the error.
I am using eclipse and Java 1.7.
Code and error are given below.
Code:
package opennlp.tools;
import java.io.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import org.w3c.dom.*;
public class CreateXMLFile
{
static String word= new String();
static String word1= new String();
static String[] Sigml_codes=new String[5];
public static void main(String[] args) throws Exception
{
word="hello";
//Sigml_codes=splitter.Sigml_codes;
Sigml_codes[0]="addf";
Sigml_codes[1]="addfg";
Sigml_codes[2]="addfh";
Sigml_codes[3]="addfi";
Sigml_codes[4]="addf";
word1="hns_sign gloss="+word+"";
System.out.println(word1);
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.newDocument();
Element rootElement = document.createElement("Sigml");
document.appendChild(rootElement);
Element child = document.createElement(word1);
rootElement.appendChild(child);
System.out.println("</hamnosys_nonmanual>");
System.out.println("<hamnosys_manual>");
Element child1=document.createElement("hamnosys_nonmanual");
child.appendChild(child1);
Element child3=document.createElement("hamnosys_manual");
child.appendChild(child3);
for(int i=0;i<=Sigml_codes.length-1;i++)
{
System.out.print("Enter the element: ");
String element = Sigml_codes[i];
Element em = document.createElement(element);
child3.appendChild(em);
}
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(document);
StringWriter sw=new StringWriter();
StreamResult result=new StreamResult(sw);
transformer.transform(source, result);
String xmlString = sw.toString();
File file = new File("c:\\codes.xml");
BufferedWriter bw = new BufferedWriter
(new OutputStreamWriter(new FileOutputStream(file)));
bw.write(xmlString);
bw.flush();
bw.close();
}
}
Error
Exception in thread "main" org.w3c.dom.DOMException: INVALID_CHARACTER_ERR: An invalid or illegal XML character is specified.
at com.sun.org.apache.xerces.internal.dom.CoreDocumentImpl.createElement(CoreDocumentImpl.java:622)
at opennlp.tools.CreateXMLFile.main(CreateXMLFile.java:38)
Hoping for a reply sooner.
thanking you..
Apeksha
|
 |
Swastik Dey
Ranch Hand
Joined: Jan 08, 2009
Posts: 1195
|
|
Apeksha,
I guess, the variable word1 denotes an element name in the xml file, and in an element name you can't have any space or = sign, it doesn't support the xml standard.
|
Swastik
|
 |
William Brogden
Author and all-around good cowpoke
Rancher
Joined: Mar 22, 2000
Posts: 12268
|
|
Invalid character means exactly that.
There are many characters Java can write that are not valid in an XML document. Control codes and Microsoft Word "smart punctuation" are frequently a cause of this error.
A google search for "illegal xml characters" will find you lots of references.
Bill
|
Java Resources at www.wbrogden.com
|
 |
 |
|
|
subject: error while creating XML doc through java code
|
|
|