• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

error while creating XML doc through java code

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic