将 XML 转换为 Java 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3276149/
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
Converting XML to Java objects
提问by DD.
What's the best way to convert XML into Java objects?
将 XML 转换为 Java 对象的最佳方法是什么?
I don't want a like for like representation, but would like to pull out certain data from the XML and populate a Java object. I had a look at XStream, but didn't really like the whole "move down, move up" type of stuff. I would prefer a DOM like object when writing converters...
我不想像表示一样喜欢,但想从 XML 中提取某些数据并填充 Java 对象。我看过 XStream,但并不喜欢整个“向下移动,向上移动”类型的东西。在编写转换器时,我更喜欢类似 DOM 的对象...
采纳答案by duffymo
I know everybody loves automatic solutions like JAXB and such, but I'd recommend hand-coding javax.xml.bind.Marshaller
and javax.xml.bind.Unmarshaller
implementations to get exactly what you want without filling up your perm gen space unnecessarily. Use JDOM to parse XML and map the values into Java objects using XPath. It'll be some work to do it once, but you'll have exactly what you need and nothing more.
我知道每个人都喜欢 JAXB 之类的自动解决方案,但我建议手动编码javax.xml.bind.Marshaller
和javax.xml.bind.Unmarshaller
实现以获得您想要的东西,而不会不必要地填充您的永久空间。使用 JDOM 解析 XML 并使用 XPath 将值映射到 Java 对象。做一次需要一些工作,但您将拥有所需的一切,仅此而已。
回答by lucas1000001
回答by Thomas Owens
I'm not entirely sure if this is what you are looking for, but you can use something like XMLBeansto bind XML to Java objects. I had to use it at a previous employer. Unfortunately, it was an existing system and I only had to manipulate the objects and didn't have to produce the library that contains them. Also, I'm not sure how well it will work without an XSD (you didn't mention if you had one or not).
我不完全确定这是否是您要查找的内容,但是您可以使用XMLBeans 之类的东西将 XML 绑定到 Java 对象。我不得不在以前的雇主那里使用它。不幸的是,这是一个现有系统,我只需要操作对象,而不必生成包含它们的库。另外,我不确定没有 XSD 的效果如何(您没有提到是否有 XSD)。
回答by Mark
I like Simpleand have found it very easy to work with. It does produce a like for like representation though so maybe not quite what you are looking for.
我喜欢Simple并且发现它很容易使用。它确实产生了一个喜欢的表示,所以可能不是你正在寻找的。
If you are looking for a DOM-like solution maybe a JDomwould be suitable.
如果您正在寻找类似 DOM 的解决方案,那么JDom可能是合适的。
回答by nos
If you have an XML schema , JAXBis nice - comes as part of the JDK. Generate java classes by running e.g. xjc -p foo myschema.xsd
如果你有一个 XML 模式,JAXB很好 - 作为 JDK 的一部分。通过运行例如生成java类xjc -p foo myschema.xsd
To read an XML file and get back an object (from classes generated by the xjc tool):
要读取 XML 文件并取回对象(来自 xjc 工具生成的类):
JAXBContext context = JAXBContext.newInstance(FooObj.class);
Unmarshaller unMarshaller = context.createUnmarshaller();
FooObj param = (FooObj) unMarshaller.unmarshal(new FileInputStream("Foo.xml"));
You can do similar things if you only want parts of an XML document converted to an object, you should e.g. be able to give JAXB part of a DOM document, instead of a whole file as done above.
如果您只想将 XML 文档的一部分转换为对象,您可以做类似的事情,例如,您应该能够提供 DOM 文档的 JAXB 部分,而不是像上面所做的整个文件。
回答by Tushar Tarkas
You can use Castorfor convenient conversion of XML to Java and back.
您可以使用Castor方便地将 XML 转换为 Java 并返回。
With Castor, you can
使用 Castor,您可以
- Ease conversion: A simple Marshaller and Unmarshaller makes conversion of XML to Java and back extremely easy.
- Mapping: A mapping file can be created to restrict the amount of data to be converted from XML to Java and back.
- 轻松转换:简单的 Marshaller 和 Unmarshaller 使 XML 和 Java 的转换非常容易。
- 映射:可以创建映射文件来限制要从 XML 转换为 Java 并返回的数据量。
回答by bdoughan
JAXB is the best way to convert objects to XML, and MOXy is the best JAXB implementation.
JAXB 是将对象转换为 XML 的最佳方式,而 MOXy 是最佳的 JAXB 实现。
- JAXB is standard, included in Java SE 6
- Standard XML binding technology for JAX-WS, JAX-RS, and SCA
- Simple runtime
- JAXB 是标准的,包含在 Java SE 6 中
- JAX-WS、JAX-RS 和 SCA 的标准 XML 绑定技术
- 简单的运行时
MOXy offers the following extensions:
MOXy 提供以下扩展: