I am developing a desktop application using Java 1.8 and I want this application to be able to first validate a given XML file, then convert it into XSD files and finally convert these XSD files into Java classes. After this conversion I will create other Java classes automatically that use these previously said Java classes. Currently I am doing these conversions manually using online XSD generators and I want to stop doing it manually so using online generators for XSD files are not an option. The conversion of said XML files will be the SOAP response of a server.
Up until now I can validate the given XML file using SAX Parser as it checks well-formedness. I used this link and this link for reference. I can also create XSD files using Trang referencing this post. When I try to convert these XSD files using JAXB-xjc.exe inside JDK 1.8, it creates errors like "Two declarations cause a collision in the ObjectFactory class." and "Attribute "xmlns:xsd" was already specified for element "xsd:schema"." in some of my files.
I tried explicitly specifying that the input is XML and output is XSD inside Trang but the errors still occured when converting XSD files into Java files. I also tried another tool called XsdGen but for a reason I can't recall now, it didn't offer the same functionality I wanted. When I manually convert into XSD and from XSD I am having no problems so I assume the problem is caused by some form of compatibility between Trang and JAXB.
Here is the code of validation, command line commands for Trang and xjc.exe usage:
public class Validate {
private static SAXParserFactory factory;
private static SAXParser parser;
private static XMLReader reader;
static {
factory = SAXParserFactory.newInstance();
factory.setValidating(false);
factory.setNamespaceAware(true);
try {
parser = factory.newSAXParser();
reader = parser.getXMLReader();
reader.setErrorHandler(new SimpleErrorHandler());
} catch (ParserConfigurationException | SAXException e) {
}
}
public static boolean validateXMLFromSource(String source) {
try {
reader.parse(new InputSource(source));
return true;
} catch (IOException | SAXException e) {
return false;
}
}
public static boolean validateXMLFromString(String xml) {
try {
InputStream stream = new ByteArrayInputStream(xml.getBytes("UTF-8"));
reader.parse(new InputSource(stream));
return true;
} catch (IOException | SAXException e) {
return false;
}
}
}
The command line commands look like this for Trang:
java -jar C:\path\to\trang.jar -I xml -O xsd C:\path\to\xml\my_xml_file.xml C:\path\to\output\xsd\test.xsd
This command produces the XSD files from a XML file.
Following command tries to transform said XSD files into Java classes:
"C:\path\of\JDK\Java\jdk1.8.0_172\bin\xjc" -p Test *.xsd
I will run both of these commands inside my Java code but for initial testing I am using command line now. I am also using the command above while I am inside the folder with XSD files. I also ran the xjc.exe and trang.jar without being in the same directory so that is not the problem. Deleting the part that creates the package sometimes solve the problem of some XMLs.
What I want to ask is that what is the source of these two (potentially more) problems and how to overcome them? Is there a better solution for the automatic conversion of XML->XSD->Java Classes than what I am doing now? Thanks.