Table of Contents
Question content
Workaround
Home Java XML validation and input streaming using XSD loaded as file

XML validation and input streaming using XSD loaded as file

Feb 09, 2024 pm 03:09 PM

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)