我有以下xml with unit&衡量儿童元素。
<Depth>
<measure>1.00</measure>
<unit>in</unit>
<Depth>
<Width>
<measure>1.00</measure>
<unit>in</unit>
</Width>
<vendorPackHeight>
<measure>1.00</measure>
<unit>in</unit>
</vendorPackHeight>
<Weight>
<measure>7.00</measure>
<unit>LBS</unit> //invalid expected value is lb
</Weight>
当单元或度量子元素的XSD验证失败,如cvc-enumeration-error时,如果measure不是来自一组枚举值,则单位值或单位值在cvc-datatype-valid.1.2.1时失败类型不匹配我如何获得父元素?在上面的xml中,它将是Weight。
在SAXParseException中,我得到发生错误的行号。是否有可能从行号中获取元素然后获取其父元素?
答案 0 :(得分:3)
我认为在Java API中有一种标准的方法。但是,有些图书馆允许您偷看它当前所在的元素。例如,在Apache Xerces实现中,它支持通过
获取当前节点 setFilterIfMissing
在其网站上查看有关该媒体资源的文档:https://xerces.apache.org/xerces2-j/properties.html#dom.current-element-node
Xerces library默认由JDK提供,但也可以作为第三方库导入项目中。我建议添加它,如果你必须正确运行你的应用程序。下面是一些示例代码,用于将XML文档验证到XSD并获取当前节点。
getProperty("http://apache.org/xml/properties/dom/current-element-node")
输出:
import java.io.ByteArrayInputStream;
import java.io.IOException;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.apache.xerces.impl.Constants;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.SAXParseException;
public class XSDTest {
public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException {
// our XSD, which defines 1 node <TheNode> which must have decimal text content
byte [] schemaData = ("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"
+ "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">"
+ "<xs:simpleType name=\"theNodeType\">"
+ "<xs:restriction base=\"xs:decimal\"/>"
+ "</xs:simpleType>"
+ "<xs:element name=\"TheNode\" type=\"theNodeType\"/>"
+ "</xs:schema>").getBytes();
// our invalid xml
byte [] xmlData = "<TheNode>123NotADecimal</TheNode>".getBytes();
// parse schema
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Source schemaSource = new StreamSource(new ByteArrayInputStream(schemaData));
Schema schema = schemaFactory.newSchema(schemaSource);
// build our document, must use document builder to enable xerces parser properties for DOM
// Also must be a xerces implementation of the DBF, should be enabled by default in a standard java project but just to be verbose about it
// pass in the full name of the DBF impl
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(org.apache.xerces.jaxp.DocumentBuilderFactoryImpl.class.getName(), XSDTest.class.getClassLoader());
dbf.setNamespaceAware(true);
Document doc = dbf.newDocumentBuilder().parse(new ByteArrayInputStream(xmlData));
// configure our validator and parse the document.
Validator validator = schema.newValidator();
validator.setErrorHandler(new MyErrorHandler(validator));
validator.validate(new DOMSource(doc.getDocumentElement()));
}
private static class MyErrorHandler implements ErrorHandler {
private final Validator xsdValidator;
public MyErrorHandler(Validator xsdValidator) {
this.xsdValidator = xsdValidator;
}
@Override
public void warning(SAXParseException exception) throws SAXException {
System.out.println("Warning on node: " + getCurrentNode());
System.out.println(exception.getLocalizedMessage());
}
@Override
public void error(SAXParseException exception) throws SAXException {
System.out.println("Error on node: " + getCurrentNode());
System.out.println(exception.getLocalizedMessage());
}
@Override
public void fatalError(SAXParseException exception) throws SAXException {
System.out.println("Fatal on node: " + getCurrentNode());
System.out.println(exception.getLocalizedMessage());
}
private Node getCurrentNode() throws SAXNotRecognizedException, SAXNotSupportedException {
// get prop "http://apache.org/xml/properties/dom/current-element-node"
// see https://xerces.apache.org/xerces2-j/properties.html#dom.current-element-node
return (Node)xsdValidator.getProperty(Constants.XERCES_PROPERTY_PREFIX + Constants.CURRENT_ELEMENT_NODE_PROPERTY);
}
}
}
答案 1 :(得分:1)
如果您切换到Saxon作为架构验证器,您将能够找到这些信息。
当您使用Saxon验证内存中的树时,ValidationFailure对象包含对验证失败的实际节点的引用(从那里您可以显然导航到父节点)。
使用Saxon验证SAX流时,ValidationFailure对象包含一个Path,它是一个结构化对象,包含有关发生故障的节点以及每个祖先的信息。每个祖先可用的信息包括节点类型,节点名称和兄弟位置。