Where should I put my XSD file for use in JAXB code and for XML validation?

I created an XSD file in my Java project that defines a user editable input file (for illustration, let's say the XSD is named userinput.xsd and the user editable file is userinput.xml). When the program starts up, it uses JAXB to verify that the user has not made any mistakes in the XML file, as it detaches the file in the DOM.

I structured my project using Maven Standard Directory Layout and generated a JAXB factory object and other classes using xjc placing them in a directory called / src / main / java / my / name / space / generated / userinput (to match the XSD name) ...

I have placed the XSD file in / src / main / resources.

When I package my jar file, the file is in the root jar directory, and I can reference it in Java code like this (note in particular the resource mentioned in line 4):

JAXBContext jaxbContext = JAXBContext.newInstance("my.name.space.generated.userinput");
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(getClass().getResource("/userinput.xsd"));
unmarshaller.setSchema(schema);
JAXBElement<?> userinputType = (JAXBElement<?>) unmarshaller.unmarshal(new FileInputStream("userinput.xml"));

      

This works, but it doesn't seem to be correct, as it means that if I want to scale to multiple input files, I could end up with many .xsd files both in the resource directory in my source control and in the root jar file.

Also when I run the program from my IDE and not from the packaged jar, I need to change the fourth statement:

Schema schema = schemaFactory.newSchema(getClass().getResource("src/main/resources/userinput.xsd"));

      

Where should I put the XSD file (a) in my original control (i.e. Maven framework) and (b) in my jar file? [Note. Running xjc through my IDE puts it in the ... / generated / userinput directory, but Maven then ignores it when packing.]

I am looking for an answer that indicates that there is some kind of general methodology, so I would like it if it were possible. If this is a currently vague choice left to the developer, then please say so (preferably referenced) as I understand stackoverflow is not a place to gather opinions.

+3


source to share


1 answer


Default layout for for placing circuits in src/main/resources

. Your schema schema.xsd

will then be available as a resource as getClass().getResource("/schema.xsd")

.
Some people use src/main/schemas

or src/main/xsd

. But if you want to use schemas as resources, it's better to use the default.

I don't understand why this "doesn't seem right". what exactly is the problem ?: Many files in version control? Lots of JAR files? Why is this a problem?

If you want to structure and modulate, compile your schemas separately in native Maven modules. I usually prefer one module per spec / version (e.g. ogc-schemas .



If you need to do it getClass().getResource("src/main/resources/userinput.xsd")

in the IDE, this is a sign that you have a wrong project configuration in the IDE. Resources should be copied / seen as part of the classpath, so should only work /userinput.xsd

. This does not mean that src/main/resources

is the wrong place, it means that your project is not set up correctly.

If you are using Maven, do not rely on the IDE to generate your code. Code generation is part of the build process. Use the Maven plugin to compile the schema.

Disclaimer: I am the author as well as the OGC Schemas project mentioned above.

+2


source







All Articles