我正在尝试使用SAX解析和验证SOAP请求。需要两个XSD,一个用于SOAP信封(http://schemas.xmlsoap.org/soap/envelope/)和我定义的一个。我找不到一种方法来正确验证针对这两个XSD的请求。
这是我用来解析请求并根据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请求?
修改
正如提议的那样,我创建了3rdpty.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="test@origin.com"/>
<urn:destAccount uri="test@destination.com"/>
</urn:getPresenceQuery>
</soapenv:Body>
</soapenv:Envelope>
其他想法?
答案 0 :(得分:0)
编写一个导入另外两个的驱动程序模式文档;验证驱动程序。