我正在努力通过XSD进行XML验证。我尝试了许多变体,尝试使用一种更简单的方法来更改XSD,尝试尝试指定ResourceResolver,但没有任何变化,说实话我没有任何想法。
我得到的错误
org.xml.sax.SAXParseException; lineNumber:11; columnNumber:45; src-resolve:un componente元素声明中的risolvere il nome“ ds:Signature”。 在com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203)
我使用的XSD是这个
word, td*idf
Java代码
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:import namespace="http://www.w3.org/2000/09/xmldsig#"
schemaLocation="http://www.w3.org/TR/2002/REC-xmldsig-core-20020212/xmldsig-core-schema.xsd"/>
<xs:complexType name="headerType">
<xs:sequence>
<xs:element name="doorNumber" type="xs:int"/>
<xs:element ref="ds:Signature"/>
</xs:sequence>
</xs:complexType>
任何建议都会有所帮助! 预先感谢
答案 0 :(得分:0)
好吧,经过数小时的研究,终于有一位同事找到了这种解决方案。
一个实现LSInput的类
public class LSInputImpl implements LSInput {
private Reader characterStream;
private InputStream byteStream;
private String stringData;
private String systemId;
private String publicId;
private String baseURI;
private String encoding;
private boolean certifiedText;
public Reader getCharacterStream() {
return characterStream;
}
public void setCharacterStream(Reader characterStream) {
this.characterStream = characterStream;
}
public InputStream getByteStream() {
return byteStream;
}
public void setByteStream(InputStream byteStream) {
this.byteStream = byteStream;
}
public String getStringData() {
return stringData;
}
public void setStringData(String stringData) {
this.stringData = stringData;
}
public String getSystemId() {
return systemId;
}
public void setSystemId(String systemId) {
this.systemId = systemId;
}
public String getPublicId() {
return publicId;
}
public void setPublicId(String publicId) {
this.publicId = publicId;
}
public String getBaseURI() {
return baseURI;
}
public void setBaseURI(String baseURI) {
this.baseURI = baseURI;
}
public String getEncoding() {
return encoding;
}
public void setEncoding(String encoding) {
this.encoding = encoding;
}
@Override
public boolean getCertifiedText() {
return false;
}
public boolean isCertifiedText() {
return certifiedText;
}
public void setCertifiedText(boolean certifiedText) {
this.certifiedText = certifiedText;
}
}
另一个实现LSResourceResolver的类
package test;
import org.w3c.dom.ls.LSInput;
import org.w3c.dom.ls.LSResourceResolver;
import java.io.InputStream;
import java.io.InputStreamReader;
public class CustomResolver implements LSResourceResolver {
@Override
public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) {
LSInputImpl input = new LSInputImpl();
InputStream stream = getClass().getClassLoader().getResourceAsStream(systemId);
input.setPublicId(publicId);
input.setSystemId(systemId);
input.setBaseURI(baseURI);
input.setCharacterStream(new InputStreamReader(stream));
return input;
}
}
在使用SchemaFactory的类中添加此
factory.setResourceResolver(new CustomResolver());