Home > Java > XML validation and input streaming using XSD loaded as file

XML validation and input streaming using XSD loaded as file

WBOY
Release: 2024-02-09 15:09:09
forward
1060 people have browsed it

Under the guidance of php editor strawberry, we will explore the method of XML validation and input flow using XSD loaded as a file. When we deal with XML data, validation is a very important step to ensure the correctness and completeness of the data. By using XSD (XML Schema Definition) files, we can define the structure and constraints of XML documents. By loading an XSD file and using it to validate the incoming XML stream, we can easily detect and handle non-conforming data, thereby increasing the reliability and security of our application. Next, we will detail how to use XSD files for XML validation and input stream processing.

Question content

I experience different behavior in xml validation if the xsd is loaded as a file or resource.

If I load the xsd as a file, everything is fine and validation works fine:

schemafactory sf = schemafactory.newinstance(xmlconstants.w3c_xml_schema_ns_uri);
schema schema = sf.newschema(new classpathresource("my/perfect/path/myfile.xsd").getfile());
validator validator = schema.newvalidator();
validator.validate(sourcetovalidate);
Copy after login

Instead, since I decided to include the xsd file into the jar, I loaded it as a resource but the behavior is different, when I construct the schema I get stuck in a saxparseexception and the error complains about something in These problems resolve names in xsd files into type definitions. The xsd file is absolutely correct

SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
InputStream inputStream = getClass().getClassLoader().getResourceAsStream("my/perfect/path/myFile.xsd");
Source schemaSource = new StreamSource(inputStream);
Schema schema = sf.newSchema(schemaSource); // here i get the SAXParseException
Validator validator = schema.newValidator();
validator.validate(sourceToValidate);
Copy after login

I really don't understand why loading the xsd as resourceasstream results in different behavior

I use jaxb for xml validation

Workaround

My guess is that your xsd references other xsds, and your validator doesn't know how to resolve those references.

You may need to provide lsresourceresolver to validator

classloader loader = getclass().getclassloader();
lsresourceresolver resolver = new lsresourceresolver() {
   public lsinput resolveresource(string type, string namespaceuri, string publicid, string systemid, string baseuri) {
      inputstream stream = loader.getresourceasstream(systemid);
      if (stream == null) throw new runtimeexception("could not find " + systemid);
      lsinput input = new dominputimpl();
      input.setpublicid(publicid);
      input.setsystemid(systemid);
      input.setbaseuri(baseuri);
      input.setbytestream(stream);             
      return input;
   }
};
validator validator = schema.newvalidator();
validator.setresourceresolver(resolver);
Copy after login

You also need to set the systemid on the inputsource by calling the two-argument constructor of streamsource.

String xsdPath = "my/perfect/path/myFile.xsd";
InputStream inputStream = getClass().getClassLoader().getResourceAsStream(xsdPath);
Source schemaSource = new StreamSource(inputStream, xsdPath);
Copy after login

See streamsource(inputstream, string)

The above is the detailed content of XML validation and input streaming using XSD loaded as file. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template