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