0

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.

enanas
  • 3
  • 4
  • Welcome to Stack Overflow! Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the [How to Ask page](https://stackoverflow.com/help/how-to-ask) for help clarifying this question. Include the desired behavior, a specific problem or error and the shortest code necessary to reproduce the issue. See also [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – aled Jul 11 '23 at 12:13
  • XSD are schemas which is just used to validate. XSD are xml format but does not contain the actual data values. It does not make any sense to convert XSD to java. – jdweng Jul 11 '23 at 12:14
  • @aled I thought that I did by adding the last sentence. I will add the code of the validation process, command line commands for Trang and JAXB, thanks. – enanas Jul 11 '23 at 12:40
  • @jdwen I have only the XML files ready initially and I want to generate Java classes from them automatically. So I only have the XML as an example of what my class should look like. Thats why I want to first transform them into XSD and then convert it into java classes. So the data inside is mostly irrelavant (except specifying data types) as I will use the created classes to parse other data coming from response. – enanas Jul 11 '23 at 12:45
  • There probably is no way to automate this, as the XML does not contain data types, correct re-occurrence or validation rules that you can have in a XSD. It is usually required to manually update the XSD that you have generated from a XML file – Dijkgraaf Jul 12 '23 at 03:30
  • @Dijkgraaf I am currently transforming XML files using [this](https://www.liquid-technologies.com/online-xml-to-xsd-converter) website and then using xjc but I am not touching the XSD files to fix them. Does that mean Trang is not checking XSD validity? – enanas Jul 12 '23 at 06:06
  • The XSD would probably validate that specific XML file instance, but might fail on other XML files that are variations that a properly created XSD file would validate correctly. – Dijkgraaf Jul 12 '23 at 08:24
  • What you are saying makes sense to me but whenever I am doing this conversion manually I don't encounter problems with parsing other XML files and thousands of responses get parsed in our server this way so I still think it is possible. – enanas Jul 12 '23 at 14:23

0 Answers0