我有一个大 xml 文件要针对大 XSD 进行验证。当出现验证错误时,客户要求我用不同的数据值填充表。例如,如果学生证无效,我将显示学区、地区和学生证。在 XML 的另一部分,如果状态无效,我将显示学校名称、州和地区。要显示的数据因无效数据而异。但是应该提取其作为无效子元素的父元素的两个或三个或四个元素。
如何使用 XMLSTREAMREADER 和 Validator 提取数据?
我试过这个,我只能得到无效元素而不是其他数据......
public class StaxReaderWithElementIdentification {
private static final StreamSource XSD = new StreamSource("files\\InterchangeEducationOrganizationExension.xsd");
private static final StreamSource XML = new StreamSource("files\\InterchangeEducationOrganizationExension.xml");
public static void main(String[] args) throws Exception {
SchemaFactory factory=SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = factory.newSchema(XSD);
XMLStreamReader reader = XMLInputFactory.newFactory().createXMLStreamReader(XML);
Validator validator = schema.newValidator();
validator.setErrorHandler(new MyErrorHandler(reader));
validator.validate(new StAXSource(reader));
}
}
处理程序是:
public class MyErrorHandler implements ErrorHandler {
private XMLStreamReader reader;
public MyErrorHandler(XMLStreamReader reader) {
this.reader = reader;
}
@Override
public void error(SAXParseException e) throws SAXException {
warning(e);
}
@Override
public void fatalError(SAXParseException e) throws SAXException {
warning(e);
}
@Override
public void warning(SAXParseException e) throws SAXException {
//System.out.println(reader.getProperty(name));
System.out.println(reader.getLocalName());
System.out.println(reader.getNamespaceURI());
e.printStackTrace(System.out);
}
}
发生验证错误时,谁能帮助我如何提取其他数据?