簡體   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