2013-08-30 98 views
0

我试图使用SAX解析和验证SOAP请求。两个XSD是必需的,一个用于SOAP信封(http://schemas.xmlsoap.org/soap/envelope/)和我定义的那个。我无法找到正确验证针对这两个XSD的请求的方法。针对2个XSD验证XML

下面是我用来解析请求并根据soapenv.xsd验证它的代码。它工作正常。如果我指定了我的XSD,验证将失败,并显示“无法找到元素声明soapenv:Envelope'”。

SAXParserFactory factory = SAXParserFactory.newInstance(); 
factory.setNamespaceAware(true); 
factory.setValidating(true); 

SAXParser saxParser = factory.newSAXParser();  
saxParser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema"); 
saxParser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", MyClass.class.getResourceAsStream("/xml/soapenv.xsd")); 

InputSource is = new InputSource(MyClass.class.getResourceAsStream("/xml/request.xml")); 
XMLReader reader = saxParser.getXMLReader(); 
reader.setContentHandler(new MyHandler()); 
reader.setErrorHandler(new MyErrorHandler()); 
reader.parse(is); 

如何指定第二个XSD?

有没有更好的方法来解析和验证SOAP请求?

编辑

的提议,我创建thirdpty.xsd是进口我的两个XSD文件。

<?xml version="1.0" encoding="UTF-8"?> 
<xs:schema targetNamespace="thirdparty:general" 
xmlns="thirdparty:general" 
xmlns:xs="http://www.w3.org/2001/XMLSchema"> 

    <xs:import schemaLocation="D:\ucfed\ValidateWSDL\src\xml\soapenv.xsd" 
     namespace="http://schemas.xmlsoap.org/soap/envelope/"/> 

    <xs:import schemaLocation="D:\ucfed\ValidateWSDL\src\xml\Presence.xsd" 
     namespace="thirdparty:presence"/>  
</xs:schema> 

我指定这个新的XSD的验证:

saxParser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", MyClass.class.getResourceAsStream("/xml/thidpty.xsd")); 

但尽管如此,只有SOAP信封XSD用于验证。如果从我的其他XSD中修改一个元素,则验证不会检测到它。

这里是XML我试图验证

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="thirdparty:presence"> 
    <soapenv:Header/> 
    <soapenv:Body> 
     <urn:getPresenceQuery> 
      <urn:origAccount uri="[email protected]"/> 
      <urn:destAccount uri="[email protected]"/> 
     </urn:getPresenceQuery> 
    </soapenv:Body> 
</soapenv:Envelope> 

其他的想法?

回答

0

编写一个驱动程序模式文件,其中导入另外两个;对驾驶员进行验证。