我正在生成一些需要符合给我的xsd文件的xml文件.什么是验证它们符合要求的最佳方法?
我有两个xsd文件来验证xml.但问题是我的代码只需要一个xsd.如何在以下代码中使用其他xsd?我不知道我应该在哪里放置/调用第二个xsd文件.
private void validate(File xmlF,File xsd1,File xsd2) {
try {
url = new URL(xsd.toURI().toString());// xsd1
} catch (MalformedURLException e) {
e.printStackTrace();
}
source = new StreamSource(xml); // xml
try {
System.out.println(url);
schema = schemaFactory.newSchema(url);
} catch (SAXException e) {
e.printStackTrace();
}
validator = schema.newValidator();
System.out.println(xml);
try {
validator.validate(source);
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud) 如何针对包含没有架构位置的导入的XSD架构验证XML?
XSD的片段:
<xs:schema xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types
xmlns:tns="http://schemas.microsoft.com/exchange/services/2006/types"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://schemas.microsoft.com/exchange/services/2006/types"
elementFormDefault="qualified" version="Exchange2010_SP2" id="types">
<xs:import namespace="http://www.w3.org/XML/1998/namespace"/>
...
Run Code Online (Sandbox Code Playgroud)
已经阅读并尝试过:
无法从架构中删除此导入,因为它包含xml:lang属性的引用.
在变量1中使用systemId = null触发的ResourceResolver resolveResource方法
public class ResourceResolver implements LSResourceResolver {
public LSInput resolveResource(String type, String namespaceURI,
String publicId, String systemId, String baseURI) {
//Some implementation
return new Input(publicId, systemId, resourceAsStream);
Run Code Online (Sandbox Code Playgroud)
在变体2中尝试这样:
SchemaFactory sFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
//sFactory.setResourceResolver(new ResourceResolver());
Schema schema = sFactory.newSchema(new Source[] {
new StreamSource("http://www.w3.org/XML/1998/namespace"),
new StreamSource(MailGateMQBinding.class.getResourceAsStream("/types.xsd")),
});
validator = messageSchema.newValidator();
source = new DOMSource(inDocBody);
validator.validate(source); …Run Code Online (Sandbox Code Playgroud)