xml引发异常

e37o9pze  于 2021-07-09  发布在  Java
关注(0)|答案(3)|浏览(446)

我有一个xml文件,它是来自webservice的响应,它包含了各种名称空间。当我尝试用适当的xsd验证它时,它抛出“org.xml.sax.saxparseexception:cvc elt.1:找不到元素soap的声明-env:envelope“.”所有命名空间的命名空间声明都在响应中声明。下面是我的代码

try {
        DocumentBuilderFactory xmlFact = DocumentBuilderFactory.newInstance();
            SchemaFactory schemaFactory = SchemaFactory
            .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            SAXSource mainInputStream = new SAXSource(new InputSource(new FileInputStream(new File("FIXEDINCOME_v3_0.xsd"))));
            SAXSource importInputStream1 =new SAXSource(new InputSource(new FileInputStream(new File("Rating.xsd"))));
            SAXSource importInputStream2 = new SAXSource(new InputSource(new FileInputStream(new File("Datatypes.xsd"))));
            Source[] sourceSchema = new SAXSource[]{mainInputStream , importInputStream1, importInputStream2};
            Schema schema = schemaFactory.newSchema(sourceSchema);  
            xmlFact.setNamespaceAware(true);
            xmlFact.setSchema(schema);
            DocumentBuilder builder = xmlFact.newDocumentBuilder();
            xmlDOC = builder.parse(new InputSource(new StringReader(inputXML)));
            NamespaceContext ctx = new NamespaceContext() {
                public String getNamespaceURI(String prefix) {
                    String uri;
                    if (prefix.equals("ns0"))
                        uri = "http://namespace.worldnet.ml.com/EDS/Standards/Common/Service_v1_0/";
                    else if (prefix.equals("ns1"))
                        uri = "http://namespace.worldnet.ml.com/EDS/Product/Services/Get_Product_Data_Svc_v3_0/";
                    else if (prefix.equals("ns2"))
                        uri = "http://namespace.worldnet.ml.com/DataSOA/Product/Objects/FixedIncome/FixedIncome_v3_0/";
                    else if (prefix.equals("ns3")) {
                        uri = "http://namespace.worldnet.ml.com/DataSOA/Product/Objects/Rating/Rating_v2_0/";
                    } else if (prefix.equals("SOAP-ENV")) {
                        uri = "http://schemas.xmlsoap.org/soap-envelope/";
                    } else
                        uri = null;

                    return uri;
                }

                // Dummy implementation - not used!
                public Iterator getPrefixes(String val) {
                    return null;
                }

                // Dummy implemenation - not used!
                public String getPrefix(String uri) {
                    return null;
                }
            };

            XPathFactory xpathFact = XPathFactory.newInstance();
            xPath = xpathFact.newXPath();
            xPath.setNamespaceContext(ctx);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
pbwdgjma

pbwdgjma1#

您为验证提供的模式集不包括soap模式。您可以在模式集合中包含soap模式,或者,如果您不关心验证soap Package 器,只需获取实际的body content元素并从那里运行验证。

ogq8wdun

ogq8wdun2#

您尝试过为soap env使用以下uri吗?
http://schemas.xmlsoap.org/soap/envelope/

fwzugrvs

fwzugrvs3#

我认为问题不在于检测soap env前缀的名称空间定义。验证器需要xsd文件,该文件定义了该命名空间中使用的元素,以便验证soap-env:envelope element,所以您需要告诉验证器该模式位于何处。
我认为解决方案是在xml响应中添加以下内容:

<SOAP-ENV:Envelope
  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:xsi="http://schemas.xmlsoap.org/soap/envelope/"
  xsi:schemaLocation="http://schemas.xmlsoap.org/soap/envelope/      
           http://schemas.xmlsoap.org/soap/envelope/">

或者,从web上下载该模式,将其作为xsd文件保存到本地文件系统,并将其添加到 sourceSchema 数组。第一种方法应该是首选的,因为它会导致更可移植的代码(和xml)。

相关问题