[英]how to add Schema to Marshaller when a Part of XSD is nested in WSDL
I would like to use a XSD to valiade a request for a WebService. 我想使用XSD来取消对WebService的请求。 The Problem is that a Part of the XSD is nested in the WSDL and the rest refers to a external XSD.
问题是XSD的一部分嵌套在WSDL中,其余的则是指外部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? 我唯一的想法是从WSDL中提取整个XSD元素,并使用DocumentBuilder从两个部分手动创建一个新的临时XSD?
Already tried with the same(negativ) result: Validating XML against multiple schemas extracted from WSDL 已经尝试过使用相同(负)结果: 针对从WSDL中提取的多个模式验证XML
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.