Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

error while creating XML doc through java code

 
apeksha agarwal
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1604
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic