gpt4 book ai didi

java - 当 XSD 的一部分嵌套在 WSDL 中时如何将架构添加到 Marshaller

转载 作者:行者123 更新时间:2023-12-01 12:33:00 24 4
gpt4 key购买 nike

我想使用 XSD 来验证对 WebService 的请求。问题是 XSD 的一部分嵌套在 WSDL 中,其余部分引用外部 XSD。我想始终使用在线的。

我唯一的想法是从 WSDL 中提取整个 XSD 元素,并使用 DocumentBuilder 手动从这两个部分创建一个新的临时 XSD?

已经尝试过相同(负面)结果: Validating XML against multiple schemas extracted from WSDL

这个问题有现成的解决方案吗?

最佳答案

最终得到这个解决方案...大多数内容都是从其他解决方案复制粘贴的,抱歉我没有时间添加源。

    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;

关于java - 当 XSD 的一部分嵌套在 WSDL 中时如何将架构添加到 Marshaller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25794623/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com