I would like to use a XSD to valiade a request for a WebService. The Problem is that a Part of the XSD is nested in the WSDL and the rest refers to a external XSD. I want to use always the one which is online.
The only idea I have is to extract the whole XSD element from the WSDL and create a new temporary XSD from both parts manually with a DocumentBuilder?
Already tried with the same(negativ) result: Validating XML against multiple schemas extracted from WSDL
Is there any out-of-the-box solution for this problem?
End up with this solution... Most stuff was copy pasted from other soltions sorry I have no time to add the sources.
pathToWSDL = Is the path to the WSDL
pathToWSDLRootPath root path of the WSDL, I need to add those to the imports because the schemaLocation where no relative paths in my case
...
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document wsdlDoc = db.newDocument();
URL oracle = new URL(pathToWSDL);
BufferedReader in = new BufferedReader(new InputStreamReader(oracle.openStream()));
Source source = new StreamSource(in);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.transform(source, new DOMResult(wsdlDoc));
NodeList schemaNodes = wsdlDoc.getElementsByTagNameNS(XMLConstants.W3C_XML_SCHEMA_NS_URI, "schema");
int nrSchemas = schemaNodes.getLength();
Source[] schemas = new Source[nrSchemas];
for (int i = 0; i < nrSchemas; i++) {
DOMSource domSource = new DOMSource(schemaNodes.item(i));
//Update Schema location by adding path
NodeList noteList = domSource.getNode().getChildNodes();
for (int j = 0; j < noteList.getLength(); j++) {
if("xsd:import".equals(noteList.item(j).getNodeName())){
NamedNodeMap nodeMap = noteList.item(j).getAttributes();
for (int attIndex = 0; attIndex < nodeMap.getLength(); attIndex++) {
if("schemaLocation".equals(nodeMap.item(attIndex).getNodeName())){
nodeMap.item(attIndex).setNodeValue(pathToWSDLRootPath + nodeMap.item(attIndex).getNodeValue());
}
}
}
}
//Show dump
//StreamResult result = new StreamResult(System.out);
//transformer.transform(domSource, result);
schemas[i] = domSource;
}
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(schemas);
return schema;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.