一段时间后,我一直收到此错误,在消失之前我遇到了一个错误,现在我得到了这个错误:
Exception in thread "main" org.w3c.dom.DOMException: INVALID_CHARACTER_ERR: An invalid or illegal XML character is specified.
at org.apache.xerces.dom.CoreDocumentImpl.createAttribute(Unknown Source)
at org.apache.xerces.dom.ElementImpl.setAttribute(Unknown Source)
at creandoXML2.CreaDOM.createEmpleadoElement(CreaDOM.java:91)
at creandoXML2.CreaDOM.createDOMTree(CreaDOM.java:84)
at creandoXML2.CreaDOM.<init>(CreaDOM.java:47)
at creandoXML2.CreaDOM.main(CreaDOM.java:33)
Java Result: 1"
我已经审查了我的代码几次,但是它不起作用,有人可以帮我吗?这是代码:
package creandoXML2;
import com.sun.org.apache.xml.internal.serialize.OutputFormat;
import com.sun.org.apache.xml.internal.serialize.XMLSerializer;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
/**
*
* @author Juan Pablo
*/
public class CreaDOM {
private List myData;
private Document dom;
private String rutaArchivo;
public static void main(String[] args) {
new CreaDOM();
}
public CreaDOM(){
this.dom=null;
this.rutaArchivo="./Archivos/";
myData=new ArrayList();
this.loadData();
this.createDocument();
this.createDOMTree();
this.printToFile();
}
private void loadData(){
myData.add(new Empleado("123","Luis Sierra","20","Prestación"));
myData.add(new Empleado("234","Kathy Fuquen","23","Temporal"));
myData.add(new Empleado("345","L. Miguel Beltrán","22","Permanente"));
myData.add(new Empleado("456","Juan Pablo Fajardo Cano","19","Temporal"));
}
private void createDocument(){
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
try{
DocumentBuilder db=dbf.newDocumentBuilder();
dom=db.newDocument();
}
catch(ParserConfigurationException pce){
System.out.println("Error while trying to instantiate DocumentBuilder");
System.exit(1);
}
}
private void createDOMTree(){
Element rootEle=dom.createElement("Personnel");
dom.appendChild(rootEle);
Iterator it=myData.iterator();
while(it.hasNext()){
Empleado e=(Empleado)it.next();
Element empleadoEle=createEmpleadoElement(e);
rootEle.appendChild(empleadoEle);
}
}
private Element createEmpleadoElement(Empleado e){
Element empleadoEle=dom.createElement("Empleado");
empleadoEle.setAttribute("Tipo de empleado: ",e.getTipoEmp());
Element nomEle=dom.createElement("Nombre");
Text nomText=dom.createTextNode(e.getName());
nomEle.appendChild(nomText);
empleadoEle.appendChild(nomEle);
Element idEle=dom.createElement("ID");
Text idText=dom.createTextNode(e.getId());
idEle.appendChild(idText);
empleadoEle.appendChild(idEle);
Element ageEle=dom.createElement("Edad");
Text ageText=dom.createTextNode(e.getAge());
ageEle.appendChild(ageText);
empleadoEle.appendChild(ageEle);
return empleadoEle;
}
private void printToFile(){
try{
OutputFormat format=new OutputFormat(dom);
format.setIndenting(true);
File file=new File(rutaArchivo+"Empleados.xml");
FileOutputStream fos=new FileOutputStream(file);
XMLSerializer serializer=new XMLSerializer(fos,format);
serializer.serialize(dom);
System.out.println("It's OK...file generated successfully!");
}
catch(IOException ie){
System.out.println("Error generando archivo!...."+ie);
}
}
}
这里是Emppleado类:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package creandoXML2;
/**
*
* @author Juan Pablo
*/
public class Empleado {
private String id;
private String name;
private String age;
private String tipoEmp;
public Empleado() {
this.id="";
this.name="";
this.age="";
this.tipoEmp="";
}
public Empleado(String id, String name, String age, String tipoEmp) {
this.id = id;
this.name = name;
this.age = age;
this.tipoEmp = tipoEmp;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getTipoEmp() {
return tipoEmp;
}
public void setTipoEmp(String tipoEmp) {
this.tipoEmp = tipoEmp;
}
@Override
public String toString() {
String datos=this.name+","+this.id+","+this.age+","+this.tipoEmp;
return datos;
}
}
如果查看允许的XML Attribute name characters,实际上会发现空格不存在,因此是非法的
我知道这已经解决。就我而言,我正在使用一个函数来修改我的xpath以读取和写入值。