繁体   English   中英

当一部分XSD嵌套在WSDL中时,如何将Schema添加到Marshaller

[英]how to add Schema to Marshaller when a Part of XSD is nested in WSDL

我想使用XSD来取消对WebService的请求。 问题是XSD的一部分嵌套在WSDL中,其余的则是指外部XSD。 我想始终使用在线的那个。

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

已经尝试过使用相同(负)结果: 针对从WSDL中提取的多个模式验证XML

是否有针对此问题的现成解决方案?

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

    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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM