Java program to validate xml with xsd
Given a JAXB class, we can an object of that class. Then, we pass that object to the marshalObjectToString method. Next, we get a string, and we give it to the validate method where the validation takes place.
If the string has errors or does not conform to the XSDs, the codes return an Exception. Otherwise, the application runs past that call to the validate method. Which technique to use then? It depends on your requirements.
Otherwise, use XML files, especially for background processing. Update 2: i have checked the xml files with xml spy. No errors there. Hope somebody see where iam wrong. I get the fllowing error: org. My java code is faulty or 2. Simon Simon 1 1 gold badge 9 9 silver badges 25 25 bronze badges. Add a comment. Active Oldest Votes. Sign up or log in Sign up using Google. Sign up using Facebook.
Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Podcast Making Agile work for data science. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Ask Question. Asked 11 years, 10 months ago. Active 2 years, 10 months ago. Viewed 67k times. Line 1, 3 3 gold badges 14 14 silver badges 37 37 bronze badges.
Melanie Melanie 1, 2 2 gold badges 16 16 silver badges 41 41 bronze badges. Add a comment. Active Oldest Votes. Thanks a lot for this comprehensive answer! I'll implement that this afternoon and let you know how it worked. I do need to create the Schema object since I have no idea how the file that are going to be validated will be built. I don't want to rely on their declaration. I'm almost there. Now my the validator does include the included file and the content of the main file.
But I have an exception when loading an import file, content not allowed in prolog It's with a file that is imported.
If I load that file directly build the schema from it instead of the main , I don't get this error. Any idea what can cause this kind of exception in that condition?
My best guess, is that your files use import paths relative to your working directory, while my files use import paths relative to the root of my classpath, which are not automatically resolved. Show 5 more comments. I have used in my Java 8 the following code which allows to specify an embedded schema path other than the root path: import com.
DOMInputImpl; import org. LSInput; import org. LSResourceResolver; import java. InputStream; import java. Gordon Daugherty Gordon Daugherty 81 1 1 silver badge 4 4 bronze badges.
I had to make some modifications to this post by AMegmondoEmber My main schema file had some includes from sibling folders, and the included files also had some includes from their local folders.
0コメント