【发布时间】:2019-07-17 05:54:14
【问题描述】:
有方法抛出 'org.xml.sax.SAXParseException' 异常。当尝试验证我的数据时。 因此,为了验证我的输入数据,我使用 javax.xml.validation 标准包。
我有 xsd 架构,预计有 2 个容器 - 用户和错误 用户可以包含元素列表,在每个子容器中,有Value和objectId(非强制)
<?xml version="1.0" encoding="UTF-8"?>
<!-- edited with XMLSpy v2018 sp1 (x64 by Organization-->
<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://mynamespace" targetNamespace="http://mynamespace" elementFormDefault="qualified">
<complexType name="User_Type">
<sequence>
<element name="objectId" minOccurs="0" maxOccurs="1">
<annotation>
<documentation>(КМД)идентификатор риск-метрики, будет проигнорирован при операции добавления</documentation>
</annotation>
<simpleType>
<restriction base="string">
<maxLength value="36"/>
</restriction>
</simpleType>
</element>
<element name="Value" minOccurs="1" maxOccurs="1">
<simpleType>
<restriction base="string">
<maxLength value="1024"/>
</restriction>
</simpleType>
</element>
</sequence>
</complexType>
<complexType name="Message_Type">
<sequence>
<element name="Text" minOccurs="0">
<simpleType>
<restriction base="string">
<maxLength value="2048"/>
</restriction>
</simpleType>
</element>
</sequence>
</complexType>
<element name="Users">
<complexType>
<sequence>
<element name="User" type="tns:User_Type" minOccurs="0" maxOccurs="unbounded"/>
<element name="Error" type="tns:Message_Type" minOccurs="0" maxOccurs="1"/>
</sequence>
</complexType>
</element>
</schema>
我想验证这个输入代码 - 只有一个用户有字段。
String rm = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><Users><User><Value>Value0</Value></User><Users>";
此代码 - 是一种方法。所以,rm - 是验证 xml validationFile 包含指向 xsd 的链接 而且,我刚刚阅读了这个文件并尝试验证字符串的输入流
String rm = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><Users><User><Value>Value0</Value></User><Users>";
String xmlFile = rm
String validationFile = "xsd/headVersions/users_1.8.xsd";
InputStream stream = new ByteArrayInputStream(xmlFile.getBytes(StandardCharsets.UTF_8));
StreamSource source = new StreamSource(stream);
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource(validationFile).getFile());
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
schemaFactory.newSchema(file).newValidator().validate(source);
return true;
我有解析异常,详细说明了,
cvc-elt.1:找不到元素“用户”的声明。
【问题讨论】:
标签: java validation xsd