Java, Validate XML against an embedded schema in WSDL
NickName:o.o Ask DateTime:2016-05-31T20:46:53

Java, Validate XML against an embedded schema in WSDL

I have a .wsdl file with an embedded schema. I want to validate an XML file/string using that .wsdl file (the same way you would validate against an .xsd). The schema is between the <types> tag. I have this so far:

public boolean validate(String xmlString) {
    try {
        // Convert to input stream
        InputStream xml = new ByteArrayInputStream(xmlString.getBytes());

        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = schemaFactory.newSchema(new File("wsdl_filepath"));

        // Validate against wsdl
        Validator validator = schema.newValidator();
        validator.validate(new StreamSource (xml));

        // XML Message is valid
        return true;

    } catch (SAXException e) {
        e.printStackTrace();
        return false;

    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
}

This doesn't work, however. This validator works if validating against an .xsd. I'm not sure how to modify it to validate against the embedded schema.

Any help would be appreciated, thank you!

Copyright Notice:Content Author:「o.o」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/37546182/java-validate-xml-against-an-embedded-schema-in-wsdl

Answers
morten 2018-07-02T08:56:25

Here is what worked for me to extract the schemas from wsld:\n\npublic static Schema makeSchema(String pathToWsdl)\n throws ParserConfigurationException, IOException, SAXException, InstantiationException, IllegalAccessException, ClassNotFoundException {\n // read wsdl document\n File wsdlFile = new File(pathToWsdl);\n DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();\n dbFactory.setNamespaceAware(true);\n DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();\n Document wsdlDoc = dBuilder.parse(wsdlFile);\n\n // read namespace declarations from wsdl document, in case they are referred from a schema\n NamedNodeMap attributes = wsdlDoc.getDocumentElement().getAttributes();\n Map<String, String> namespacesFromWsdlDocument = new HashMap<>();\n for (int i = 0; i < attributes.getLength(); i++) {\n Node n = attributes.item(i);\n if (n.getNamespaceURI() != null && n.getNamespaceURI().equals(\"http://www.w3.org/2000/xmlns/\")) {\n namespacesFromWsdlDocument\n .put(n.getLocalName(), n.getNodeValue());\n }\n }\n\n // read the schema nodes from the wsdl\n NodeList schemas = wsdlDoc.getElementsByTagNameNS(\"http://www.w3.org/2001/XMLSchema\", \"schema\");\n Map<String, DOMSource> sources = new HashMap<>();\n for (int i = 0; i < schemas.getLength(); i++) {\n // create a document for each schema and copy the source schema\n Document schema = dBuilder.newDocument();\n Element schemaElement = (Element)schema.importNode(schemas.item(i), true);\n\n // add all non-existing namespace declarations from the wsdl node\n String targetNs = schemaElement.getAttribute(\"targetNamespace\");\n for (Map.Entry<String, String> ns : namespacesFromWsdlDocument.entrySet()) {\n String name = ns.getKey();\n String value = ns.getValue();\n if (schemaElement.getAttributeNodeNS(\"http://www.w3.org/2000/xmlns/\", name) == null) {\n schemaElement.setAttributeNS(\"http://www.w3.org/2000/xmlns/\", \"xmlns:\" + name, value);\n }\n }\n\n // map schemas by their target namespace\n schema.appendChild(schemaElement);\n DOMSource domSource = new DOMSource(schema);\n sources.put(targetNs, domSource);\n }\n\n SchemaFactory factory =\n SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);\n\n // Create a ResourceResolver that can find the correct schema from the map\n DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();\n\n final DOMImplementationLS domImplementationLS = (DOMImplementationLS) registry.getDOMImplementation(\"LS\");\n factory.setResourceResolver(new LSResourceResolver() {\n @Override public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) {\n Source xmlSource = sources.get(namespaceURI);\n if (xmlSource != null) {\n LSInput input = domImplementationLS.createLSInput();\n ByteArrayOutputStream outputStream = new ByteArrayOutputStream();\n Result outputTarget = new StreamResult(outputStream);\n try {\n TransformerFactory.newInstance().newTransformer().transform(xmlSource, outputTarget);\n } catch (TransformerException e) {\n e.printStackTrace();\n }\n InputStream is = new ByteArrayInputStream(outputStream.toByteArray());\n input.setByteStream(is);\n input.setSystemId(systemId);\n return input;\n } else {\n return null;\n }\n }\n });\n\n // create the schema object from the sources\n return factory.newSchema(sources.values().toArray(new DOMSource[]{}));\n}\n",


More about “Java, Validate XML against an embedded schema in WSDL” related questions

Java, Validate XML against an embedded schema in WSDL

I have a .wsdl file with an embedded schema. I want to validate an XML file/string using that .wsdl file (the same way you would validate against an .xsd). The schema is between the &lt;types&gt; t...

Show Detail

Validate XML instance document against WSDL

I can easily validate a XML document against a XML Schema, eg. with XMLSpy or programmatically. Is it possible to do this with a WSDL file? It does not seem possible with XMLSpy or any other XML t...

Show Detail

Validate SOAP message against WSDL

Just like validating an XML file against an XML Schema Definition in Java is there a way to validate an XML file that contains a SOAP Envelope against a WSDL file?

Show Detail

Mule Schema validation when wsdl has embedded xsd

In Mule ESB I want to validate incoming SOAP/XML, using a standard Mule "schema-validation filter". Something like: &lt;mulexml:schema-validation-filter schemaLocations="xxx.xsd" name="

Show Detail

Java. Validate SOAP message against a WSDL

I need to validate a SOAP message against a WSDL? (in the same way that an XML file can be validated against a XSD schema). I am not calling any webservice; I just have a SOAP message and a WSDL, ...

Show Detail

Validate jaxws soap request against schema inside wsdl:types

I'm wanting to validate a soap request against the schema inside this wsdl. &lt;wsdl:types &gt; &lt;xsd:schema targetNamespace="http://ws.thecompany.com/FormSubmissionService20100824" ...

Show Detail

Validate WSDL schema

I have this wsdl specification I have to code against (in asp.net 3.5), but it cannot be imported by visual studio 2010. I've tried using liquid XML, soapUI, xmlspy and webservice tester, and all o...

Show Detail

How to validate a SOAP request against a WSDL (yes, WSDL, not XSD), in ruby

I have, in the context of a SOAP request, a WSDL a bunch of XSDs, referenced by the WSDL an example SOAP request I would like to validate that the SOAP request fits the WSDL and all the XSDs. I am

Show Detail

How to validate an XML against schema using JAXB?

I am working with XML and JAXB as I am unmarshalling and marshalling the XML into Java objects and vice versa. Now I am trying to validate our XML against our schema(test.xsd). Suppose if any requi...

Show Detail

Validate XML against a schema having any element

I have a schema say called BPMN20.xsd having an element called extensionElements.see sample below: &lt;xsd:element name="extensionElements" type="tExtensionElements" /&gt; &lt;xsd:complexType...

Show Detail