Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Validate XML using XSD with no namespaces

I'm trying to validate the following XML

<query>
 <colors logic="AND">
  <color main="BLUE" tone="DARK" operator="=" />
 </colors>
</query>

using the following XSD

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <xsd:complexType name="color">
     <xsd:attribute name="main" type="xsd:string" use="required"/>
     <xsd:attribute name="tone" type="xsd:string" use="required"/>
     <xsd:attribute name="operator" type="xsd:string"/>
 </xsd:complexType>
 <xsd:complexType name="colors">
     <xsd:sequence>
         <xsd:element name="color" type="color" maxOccurs="unbounded">   </xsd:element>
     </xsd:sequence>
     <xsd:attribute name="logic" type="xsd:string" use="required"/>
 </xsd:complexType>
 <xsd:complexType name="query">
     <xsd:sequence>
         <xsd:element name="colors" type="colors" maxOccurs="2"></xsd:element>
     </xsd:sequence>
 </xsd:complexType>
 <xsd:element name="query" type="query"></xsd:element>
</xsd:schema>

So... I want to validate the XML without any namespace. I can't change the XML since it is generated by another application, I only want guarantee, on the server side, that the client is sending the right request.

When I try to validate the XML against the XSD I get the following exception message:

cvc-elt.1: Cannot find the declaration of element 'query'

I already searched and came into solutions like this and this but with no success

Solution (thanks @Traroth for pointing me in the right direction)---

Here's how I'm validating it:

I have this function:

public static Document buildValidRequest(String content, Schema xsd) throws SAXParseException, SAXException, IOException, ParserConfigurationException {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    factory.setValidating(false);
    factory.setNamespaceAware(true);

    factory.setSchema(xsd);

    XMLSimpleErrorHandler errorHandler = new XMLSimpleErrorHandler();

    DocumentBuilder builder = factory.newDocumentBuilder();
    builder.setErrorHandler(errorHandler);

    StringReader reader = new StringReader(content);

    Document validXML = builder.parse(new InputSource(reader));

    if (errorHandler.getException() != null) {
        throw errorHandler.getException();
    }

    return validXML;

}

And this class to handle the errors:

public class XMLSimpleErrorHandler implements ErrorHandler {

private SAXParseException exception;

@Override
public void warning(SAXParseException e) {
    this.exception = e;
}

@Override
public void error(SAXParseException e) {
    this.exception = e;
}

@Override
public void fatalError(SAXParseException e) {
    this.exception = e;
}

public SAXParseException getException() {
    return exception;
}
}

And this method to get the Schema:

private static Schema getSchema(String xsdPath) throws SAXException, IOException {
    InputStream resourceAsStream = null;
    try {
        ServiceManager.getInstance().getLoggerManager().debug(RESTInitServlet.LOGCONTEXT, TAG, "Retrieving schema: "+xsdPath);
        resourceAsStream = getInstance().getClass().getResourceAsStream(xsdPath);
        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Source schemaSource = new StreamSource(resourceAsStream);
        Schema schema = schemaFactory.newSchema(schemaSource);
        return schema;
    } finally {
        if (resourceAsStream != null) {
            resourceAsStream.close();
        }
    }

}

Nevermind this: Weirdest thing of all: works on Tomcat 6 running on Windows 7; not working in jboss running on Linux...

like image 806
Miguel Ribeiro Avatar asked Dec 31 '25 05:12

Miguel Ribeiro


1 Answers

You should find your answer here: http://www.edankert.com/validate.html#Validate_using_external_Schema

like image 178
Alexis Dufrenoy Avatar answered Jan 02 '26 19:01

Alexis Dufrenoy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!