用dom4解析XML并校验schema
解析并不难,schema校验比较麻烦,以下是全部代码
private Document readXmlFromFile(File file) {
Document protocolDoc = null;
SAXReader reader = new SAXReader();
XMLErrorHandler errorHandler = new XMLErrorHandler();
SAXParserFactory factory = SAXParserFactory.newInstance();
System.out.println(file.getAbsolutePath());
factory.setValidating(true); // 要加入校验一定要写这句
factory.setNamespaceAware(true);
try {
//先读取xml文件,再验证
protocolDoc = reader.read(file);
SAXParser parser = factory.newSAXParser();
parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
"http://www.w3.org/2001/XMLSchema");
parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", "file:"
+ this.getClass().getResource("/").getPath().toString() + PROTOCOL_XSD_FILE_NAME);
SAXValidator validator = new SAXValidator(parser.getXMLReader());
validator.setErrorHandler(errorHandler);
validator.validate(protocolDoc);
XMLWriter writer = new XMLWriter(OutputFormat.createPrettyPrint());
//如果出错errorHandler会记录所有错误信息
if (errorHandler.getErrors().hasContent()) {
System.out.println("faild");
//利用 dom4j 提供的 XMLWriter 打印出所有错误信息
writer.write(errorHandler.getErrors());
logger.error("validate xml schema on File " + file.getAbsolutePath() + " fail.");
throw new ParseProtocolException("validate xml schema on File " + file.getAbsolutePath() + " fail.");
}
} catch (ParserConfigurationException e) {
logger.error("Read xml From File " + file.getAbsolutePath() + " occur ParserConfigurationException.", e);
throw new ParseProtocolException("Read xml From File " + file.getAbsolutePath()
+ " occur ParserConfigurationException.", e);
} catch (SAXException e) {
logger.error("Read xml From File " + file.getAbsolutePath() + " occur SAXException.", e);
throw new ParseProtocolException("Read xml From File " + file.getAbsolutePath() + " occur SAXException.", e);
} catch (UnsupportedEncodingException e) {
logger.error("Read xml From File " + file.getAbsolutePath() + " occur UnsupportedEncodingException.", e);
throw new ParseProtocolException("Read xml From File " + file.getAbsolutePath()
+ " occur UnsupportedEncodingException.", e);
} catch (IOException e) {
logger.error("Read xml From File " + file.getAbsolutePath() + " occur IOException.", e);
throw new ParseProtocolException("Read xml From File " + file.getAbsolutePath() + " occur IOException.", e);
} catch (DocumentException e) {
logger.error("Read xml From File " + file.getAbsolutePath() + " occur DocumentException.", e);
throw new ParseProtocolException("Read xml From File " + file.getAbsolutePath() + " occur DocumentException.",
e);
}
return protocolDoc;
}
//以下是解析过程
Document protocolDoc = readXmlFromFile(file);
//得到根节点
Element openremoteElement = protocolDoc.getRootElement();
//遍历下面的子结点
Iterator<Element> protocolItr = openremoteElement.elementIterator(PROTOCOL_ELEMENT_NAME);
while (protocolItr.hasNext()) {
Element protocolElement = protocolItr.next();
ProtocolDefinition protocolDefinition = new ProtocolDefinition();
// set protocol name
// 获取节点中的属性
protocolDefinition.setName(protocolElement.attributeValue(NAME_ATTR_NAME));
// parse attr element start
protocolDefinition.getAttrs().addAll(parseAttributs(protocolElement));
map.put(protocolDefinition.getName(), protocolDefinition);
}
return map;