下面是我用于验证XSD的XML架构的groovy代码
import java.io.File;
import java.io.IOException;
import javax.xml.XMLConstants;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import javax.xml.transform.sax.SAXSource
import javax.xml.parsers.SAXParserFactory
import org.xml.sax.SAXException
import org.xml.sax.InputSource
import org.xml.sax.SAXParseException
import org.xml.sax.ErrorHandler
def validateXMLSchema(String xsdPath, String xmlPath) {
final List < SAXParseException > exceptions = new LinkedList < SAXParseException > ();
try {
SchemaFactory factory =
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = factory.newSchema(new File(xsdPath));
Validator validator = schema.newValidator();
validator.setErrorHandler(new ErrorHandler() {
@Override
public void warning(SAXParseException exception) throws SAXException {
exceptions.add(exception);
}
@Override
public void fatalError(SAXParseException exception) throws SAXException {
exceptions.add(exception);
}
@Override
public void error(SAXParseException exception) throws SAXException {
exceptions.add(exception);
}
});
def xmlFile = new File(xmlPath);
validator.validate(new StreamSource(xmlFile));
exceptions.each {
println 'lineNumber : ' + it.lineNumber + '; message : ' + it.message
}
} catch (IOException | SAXException e) {
println("Exception: line ${e.lineNumber} " + e.getMessage());
return false;
}
return exceptions.size() == 0;
}
以下是一些验证错误,我可以访问每封邮件的行号,并尝试查找相应的节点名称
lineNumber : 106; message : cvc-datatype-valid.1.2.1: '' is not a valid value for 'date'.
lineNumber : 248; message : cvc-enumeration-valid: Value 'Associate' is not facet-valid with respect to enumeration '[ADJSTR, ADJSMT]
是否有一种简单的方法可以使用行号找到相应错误消息的节点名称?或者我是否必须阅读该特定行并使用XmlSlurper解析它(如下所示(试图避免这种方法,因为对于生产中较大用户负载的大型XML文件,它会更慢)?
def getNodeName(xmlFile, lineNumber){
def xmlLine = xmlFile.readLines().get(lineNumber)
def node = new XmlSlurper().parseText(xmlLine.toString())
node.name()
}
答案 0 :(得分:1)
这不是很优雅,但以下getNodeName()
应该更快(full example here):
def getNodeName(xmlFile, lineNumber) {
def result = "unknown"
def count = 1
def NODE_REGEX = /.*?<(.*?)>.*/
def br
try {
br = new BufferedReader(new FileReader(xmlFile))
String line
def isDone = false
while ((! isDone) && (line = br.readLine()) != null) {
if (count == lineNumber) {
def matcher = (line =~ NODE_REGEX)
if (matcher.matches()) {
result = matcher[0][1]
}
isDone = true
}
count++
}
} finally {
// TODO: better exception handling
br.close()
}
return result
}
它只是直到有问题的行读取行,然后使用基本的正则表达式来获取名称。如果愿意,您可以在示例中使用XmlSlurper
。关键是文件IO /内存应该少得多。