java 用Java做Xml的简单方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/528664/
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
Simple way to do Xml in Java
提问by Omar Kooheji
Is there is Simple way to read and write Xml in Java?
有没有简单的方法在 Java 中读写 Xml?
I've used a SAX parser before but I remember it being unintuitive, I've looked at a couple of tutorials for JAXB and it just looks complicated.
我以前使用过 SAX 解析器,但我记得它不直观,我看过几个 JAXB 教程,但它看起来很复杂。
I don't know if I've been spoilt by C#'s XmlDocument class, but All I want to do is create an Xml Document that represents a a set of classes and their members (some are attributes some are elements).
我不知道我是否被 C# 的 XmlDocument 类宠坏了,但我想要做的就是创建一个 Xml 文档来表示一组类及其成员(有些是属性,有些是元素)。
I would look into serialization but the XML has to have the same format as the output of a c# app which I am reverse engineering into Java.
我会研究序列化,但 XML 必须具有与 ac# app 的输出相同的格式,我将其逆向工程为 Java。
回答by Milhous
回答by Marko
If you are using jdk 1.4 or newer take a look at XMLEncoderclass.
如果您使用的是 jdk 1.4 或更新版本,请查看XMLEncoder类。
回答by toolkit
Some of the more popular approaches to consider:
一些比较流行的方法可以考虑:
Java Archictecture for XML Binding
用于 XML 绑定的 Java 架构
JAXB is a specification for a standard XML binding. If you already have an XSD, it can generate your Java classes for you, and then all that's left is to use a standard API for marshalling/unmarshalling.
JAXB 是标准 XML 绑定的规范。如果您已经有了 XSD,它可以为您生成 Java 类,然后剩下的就是使用标准 API 进行编组/解组。
- Glassfish 的参考实现
- Apache 的实现JaxMe
Other binding approaches
其他绑定方法
As with JAXB, these approaches use XML-based binding configurations. They may provide more fine grained control of the unmarshalling process.
与 JAXB 一样,这些方法使用基于 XML 的绑定配置。它们可以对解组过程提供更细粒度的控制。
Roll your own
自己动手
回答by Kyle Dyer
Dom4jis a simple api for creating xml documents in java.
Dom4j是一个简单的 api,用于在 java 中创建 xml 文档。
Document document = DocumentHelper.createDocument();
Element root = document.addElement( "root" );
Element author2 = root.addElement( "author" )
.addAttribute( "name", "Toby" )
.addAttribute( "location", "Germany" )
.addText( "Tobias Rademacher" );
回答by McDowell
There is a wide choice of XML processing options for Java, though judging from the .NET documentation for XmlDocument, the Java DOM implementationis the closest out-of-the-box equivalent.
Java 有多种 XML 处理选项可供选择,但从XmlDocument的 .NET 文档来看,Java DOM 实现是最接近的开箱即用的等价物。
.NET XmlDocument:
.NET XmlDocument:
This class implements the W3C Document Object Model (DOM) Level 1 Core and the Core DOM Level 2.
此类实现了 W3C 文档对象模型 (DOM) 级别 1 核心和核心 DOM 级别 2。
Java Document:
Java文件:
See also the Document Object Model (DOM) Level 3 Core Specification.
另请参阅文档对象模型 (DOM) 级别 3 核心规范。
Sample code:
示例代码:
public static void main(String[] args) throws Exception {
File xmlFile = new File(".classpath");
// read it
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(xmlFile);
// walk it
System.out.println("Node count=" + countNodes(document));
// write it
Source source = new DOMSource(document);
Result result = new StreamResult(System.out);
TransformerFactory transformerFactory = TransformerFactory
.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.transform(source, result);
}
/** Doesn't count attributes, etc */
private static int countNodes(Node node) {
int count = 0;
NodeList kids = node.getChildNodes();
count += kids.getLength();
for (int i = 0; i < kids.getLength(); i++) {
count += countNodes(kids.item(i));
}
return count;
}
回答by Aaron Digulla
The most simple way so far is the MarkupBuilderin Groovy. Think of Groovy as a new syntax for Java. The XmlSlurpercan be used to read XML.
最简单的方法,到目前为止是的MarkupBuilder在Groovy中。将 Groovy 视为 Java 的一种新语法。该的XmlSlurper可以用于读取XML。
回答by stephendl
I think that Apache XMLBeansprovides the functionality you are after.
我认为Apache XMLBeans提供了您所追求的功能。
The Wikipedia pagegives a good overview and example usage.
在维基百科页面给出了一个很好的概述和用法示例。
回答by StaxMan
I think JAXB is only complicated if you look at wrong examples. Specifically, yes, schema-based way can get messy. But code-first, annotation-based is trivially easy.
我认为 JAXB 只有在您查看错误示例时才会复杂。具体来说,是的,基于模式的方式可能会变得混乱。但是代码优先,基于注释的方法非常简单。
Another easy alternative is XStream. And for non-binding case, StaxMate, which is an add-on for streaming Stax parsers.
回答by peter.murray.rust
I would certainly use XOM if you want a DOM-like approach and SAX (www.sax.org) if you want a SAX-like approach. I was involved in the early development of XML and SAX was developed as an event-driven approach, which is useful for some applications. DOM/XOM and SAX are complementary - sometimes you need one, sometimes the other. If you wish to build objects as you go rather than read everything into memory, use SAX. If you are happy to read everything in and then process it, use XOM.
如果您想要类似 DOM 的方法,我当然会使用 XOM,如果您想要类似 SAX 的方法,我肯定会使用 SAX (www.sax.org)。我参与了 XML 的早期开发,SAX 是作为事件驱动的方法开发的,这对某些应用程序很有用。DOM/XOM 和 SAX 是互补的——有时需要一个,有时需要另一个。如果您希望随时构建对象而不是将所有内容读入内存,请使用 SAX。如果您乐于阅读并处理所有内容,请使用 XOM。
I spent far too much time trying to get the W3C DOM to work - IMO it is poorly defined with too many ways of doing some things and not enough for others. When XOM came it revolutionised my productivity.
我花了太多时间试图让 W3C DOM 工作 - IMO 定义不明确,有太多方法可以做一些事情,而对于其他事情来说还不够。当 XOM 出现时,它彻底改变了我的工作效率。
The XOM community is very knowledgeable and focused and helpful.
XOM 社区知识渊博、专注且乐于助人。

