Java Validate XML using XSD with no namespaces

tiranalive

New Member
I'm trying to validate the following XML\[code\]<query> <colors logic="AND"> <color main="BLUE" tone="DARK" operator="=" /> </colors></query>\[/code\]using the following XSD\[code\]<?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>\[/code\]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:\[code\]cvc-elt.1: Cannot find the declaration of element 'query'\[/code\]I already searched and came into solutions like this and this but with no successSolution (thanks @Traroth for pointing me in the right direction)---Here's how I'm validating it:I have this function:\[code\]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;}\[/code\]And this class to handle the errors:\[code\]public class XMLSimpleErrorHandler implements ErrorHandler {private SAXParseException exception;@Overridepublic void warning(SAXParseException e) { this.exception = e;}@Overridepublic void error(SAXParseException e) { this.exception = e;}@Overridepublic void fatalError(SAXParseException e) { this.exception = e;}public SAXParseException getException() { return exception;}}\[/code\]And this method to get the Schema:\[code\]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(); } }}\[/code\]Nevermind this: Weirdest thing of all: works on Tomcat 6 running on Windows 7; not working in jboss running on Linux...
 
Back
Top