XML 与 XSD JAVA
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3893263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XML with XSD JAVA
提问by umesh
I have been assigned a work to validate a XML against an XSD and if everthing passed will parse the XML so that can import the same in to my system.
我被分配了一项针对 XSD 验证 XML 的工作,如果传递的所有内容都将解析 XML,以便可以将其导入到我的系统中。
My Qyestion is whats the best way to validate an XML against the XSD and which is the best API for parsing the XML in to my domain object.
我的 Qyestion 是针对 XSD 验证 XML 的最佳方法,也是将 XML 解析为我的域对象的最佳 API。
Looking for valuable suggestions
寻求宝贵建议
回答by bdoughan
Part 1 - Validate XML
第 1 部分 - 验证 XML
You can use the javax.xml.validationAPIs for this.
您可以为此使用javax.xml.validationAPI。
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
URL schemaURL = // The URL to your XML Schema;
Schema schema = sf.newSchema(schemaURL);
Validator validator = schema.newValidator();
validator.validate(xml);
Part 2 - OXM
第 2 部分 - OXM
Regarding the second part of your question, the best API for parsing XML into the domain object is JAXB. JAXB is a specificationwith multiple implementations. I lead the MOXy JAXBimplementation that contains useful extensions such as XPath based mapping.
关于问题的第二部分,将 XML 解析为域对象的最佳 API 是 JAXB。 JAXB 是一个具有多种实现的规范。我领导MOXy JAXB实现,其中包含有用的扩展,例如基于 XPath 的映射。
You could always do the validation during the conversion of XML to objects:
在将 XML 转换为对象期间,您始终可以进行验证:
JAXBContext jc = JAXBContext.newInstance(Customer.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
URL schemaURL = // The URL to your XML Schema;
Schema schema = sf.newSchema(schemaURL);
unmarshaller.setSchema(schema);
JAXBElement<Customer> element = (JAXBElement<Customer>) unmarshaller.unmarshal(xml);
Customer customer = elemnt.getValue();
回答by Stan Kurilin
You can use DOMor SAXparsers for these operation.
EDITHere some example with sax parsing
编辑这里有一些 sax 解析的例子
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(true);
factory.setNamespaceAware(true);
SAXParser parser = factory.newSAXParser();
parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
"http://www.w3.org/2001/XMLSchema");
XMLReader reader = parser.getXMLReader();
reader.setErrorHandler(new ErrorHandler(){
public void warning(SAXParseException e) throws SAXException {
System.out.println(e.getMessage());
}
public void error(SAXParseException e) throws SAXException {
System.out.println(e.getMessage());
}
public void fatalError(SAXParseException e) throws SAXException {
System.out.println(e.getMessage());
}
});
reader.parse(new InputSource("document.xml"));
(I'd took source from here)
(我从这里获取了来源)
回答by Mike G
JAXB is the Java standard XML parsing library: http://www.oracle.com/technetwork/articles/javase/index-140168.html. It comes bundled with Java SE 6.
JAXB 是 Java 标准 XML 解析库:http: //www.oracle.com/technetwork/articles/javase/index-140168.html。它与 Java SE 6 捆绑在一起。
Have a look at the tutorial. It's easy to register a schema file with your Unmarshaler/Marshaler and validate.
回答by franklins
You can use Castor, that generate Java classes for your XSD. So you can convert your XML to objects and vice versa in few lines of code.
您可以使用 Castor,它为您的 XSD 生成 Java 类。因此,您可以在几行代码中将 XML 转换为对象,反之亦然。