Я создаю код Java, который создает XML динамически на основе схемы XML.Я использую Jaxb для этого.Вот мой код:
public class Testing2 {
private static void traverseProps(DynamicJAXBContext c, DynamicEntity e, DynamicType t, int level) throws DynamicException, InstantiationException, IllegalAccessException{
if (t!=null) {
// logger.info(indent(level) + "type [" + t.getName() + "] of class [" + t.getClassName() + "] has " + t.getNumberOfProperties() + " props");
for (String pName:t.getPropertiesNames()){
Class<?> clazz = t.getPropertyType(pName);
// logger.info(indent(level) + "prop [" + pName + "] in type: " + clazz);
//logger.info("prop [" + pName + "] in entity: " + e.get(pName));
if (clazz==null){
// need to create an instance of object
String updatedClassName = pName.substring(0, 1).toUpperCase() + pName.substring(1);
// logger.info(indent(level) + "Creating new type instance for " + pName + " using following class name: " + updatedClassName );
DynamicType child = c.getDynamicType("generated." + updatedClassName);
DynamicEntity childEntity = child.newDynamicEntity();
e.set(pName, childEntity);
traverseProps(c, childEntity, child, level+1);
} else {
// just set empty value
e.set(pName, clazz.newInstance());
}
}
} else {
// logger.warn("type is null");
}
}
public static void main(String[] args) throws FileNotFoundException, JAXBException, DynamicException, InstantiationException, IllegalAccessException {
// TODO Auto-generated method stub
String PATH_TO_XSD = "C:/.....XSD.xsd";
String YOUR_ROOT_TYPE = "Doc";
InputStream in = new FileInputStream(PATH_TO_XSD);
DynamicJAXBContext jaxbContext = DynamicJAXBContextFactory.createContextFromXSD(in, null, Thread.currentThread().getContextClassLoader(), null);
DynamicType rootType = jaxbContext.getDynamicType(YOUR_ROOT_TYPE);
DynamicEntity root = rootType.newDynamicEntity();
traverseProps(jaxbContext, root, rootType, 0);
}
}
Я получаю ошибку ниже, хотя я добавил jars: jaxb-xjc-2.2.11.jar и jaxb-core-2.3.0.jar.Может кто-нибудь помочь мне в отладке проблемы:
Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/persistence/internal/oxm/mappings/Field
at org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext$SchemaContextInput.createContextState(DynamicJAXBContext.java:284)
at org.eclipse.persistence.jaxb.JAXBContext.<init>(JAXBContext.java:174)
at org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext.<init>(DynamicJAXBContext.java:71)
at org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContextFactory.createContextFromXSD(DynamicJAXBContextFactory.java:279)
at com.sap.Testing2.main(Testing2.java:48)
Caused by: java.lang.ClassNotFoundException: org.eclipse.persistence.internal.oxm.mappings.Field
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 5 more