我有两个基于xsd,Comprobante和Detallista的类,我填充了两个类但是当我想将我的类转换为xml字符串时,我得到了这个错误:
(javax.xml.bind.MarshalException)javax.xml.bind.MarshalException - 包含链接异常:[com.sun.istack.internal.SAXException2:class java.util.ArrayList ni ninguna de sus superclases se conocen en este contexto。 javax.xml.bind.JAXBException:class java.util.ArrayList ni ninguna de sus superclases se conocen en este contexto。]
这就是我填写课程的方式:
try
{
String cfdi="",complemento_det="";
cFunctions functions=new cFunctions();
ObjectMapper omapper=new ObjectMapper();
Comprobante comprobante=new Comprobante();
Detallista cdetallista=new Detallista();
JSONObject jpreCFDI=new JSONObject(java.net.URLDecoder.decode(preCFDI, "UTF-8"));
ArrayList<Object> lst_complemento=new ArrayList();
if(jpreCFDI.has("complemento"))
{
JSONArray complementos=jpreCFDI.getJSONArray("complemento");
jpreCFDI.remove("complemento");
for(int i=complementos.length()-1;i>=0;i--)
{
JSONObject jcomplemento=complementos.getJSONObject(i);
JSONObject any=jcomplemento.getJSONObject("any");
if(any.has("cdetallista"))
{
JSONObject jcdetallista=any.getJSONObject("cdetallista");
cdetallista=omapper.readValue(jcdetallista.toString(),Detallista.class);
lst_complemento.add(cdetallista);
complemento_det="detallista";
}
}
}
comprobante=omapper.readValue(jpreCFDI.toString(),Comprobante.class);
cfdi=functions.getCFDI_string(comprobante,"");
setOriginal_string(functions.getOriginal_String(cfdi,getXlst_path()));
if(getOriginal_string().equals("e-cadena"))
return new JSONObject().put("Code","afe-e05").put("Message","Error al generar cadena original").put("CFDI","");
if(functions.getStamp_CFDI(getPassword_key_file(),getOriginal_string(),getKey_file_path()).equals("e-campos"))
return new JSONObject().put("Code","afe-e06").put("Message","Faltan campos para generar sello digital").put("CFDI","");
else if(functions.getStamp_CFDI(getPassword_key_file(),getOriginal_string(),getKey_file_path()).equals("e-password"))
return new JSONObject().put("Code","afe-e07").put("Message","Contraseña incorrecta de archivo .key").put("CFDI","");
comprobante.setSello(functions.getStamp_CFDI(getPassword_key_file(),getOriginal_string(),getKey_file_path()));
comprobante.setCertificado(functions.getCertificated_CFDI(getCertificated_file_path()));
Complemento complemento=new Complemento();
complemento.getAny().add(lst_complemento);
comprobante.getComplemento().add(complemento);
cfdi=functions.getCFDI_string(comprobante,complemento_det);
return new JSONObject().put("Code","afe-200").put("Message","success").put("CFDI",cfdi);
}
catch(Exception ex)
{
String cause=ex.getLocalizedMessage();
if(cause!=null && (cause.contains("JSONObject")||cause.contains("JSONArray")))
return new JSONObject().put("Code","afe-e08").put("Message","Error al generar CFDI. Faltan nodos requeridos.").put("CFDI","");
return new JSONObject().put("Code","afe-e00").put("Message","Ocurrio un error en el servidor.").put("CFDI","");
}
这是为了转换类:
public String getCFDI_string(Comprobante cfdi,String complemento) throws Exception
{
String prefix;
JAXBContext jcontext;
if(complemento.equals("detallista")){
jcontext=JAXBContext.newInstance(Comprobante.class,Detallista.class);
prefix="http://www.sat.gob.mx/detallista http://www.sat.gob.mx/sitio_internet/cfd/detallista/detallista.xsd";
}
/*else if(complemento.equals("donat"))
{
jcontext=JAXBContext.newInstance(new Class[]{Comprobante.class});
}*/
else
{
jcontext=JAXBContext.newInstance(Comprobante.class);
prefix="";
}
String xml="";
Marshaller marsh=jcontext.createMarshaller();
marsh.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marsh.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
marsh.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "http://www.sat.gob.mx/cfd/3 http://www.sat.gob.mx/sitio_internet/cfd/3/cfdv33.xsd "+prefix);
try(StringWriter swriter=new StringWriter()){
marsh.marshal(cfdi,swriter);
xml=swriter.toString();
}
return xml;
}
这是造成错误的Comprobante类的一部分:
@XmlElement(name = "Complemento")
protected List<Comprobante.Complemento> complemento;
public List<Comprobante.Complemento> getComplemento() {
if (complemento == null) {
complemento = new ArrayList<Comprobante.Complemento>();
}
return this.complemento;
}
public static class Complemento {
@XmlAnyElement(lax = true)
protected List<Object> any;
public List<Object> getAny() {
if (any == null) {
any = new ArrayList<Object>();
}
return this.any;
}
}