使用SAX和Java生成XML [英] Generating XML using SAX and Java

查看:95
本文介绍了使用SAX和Java生成XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都知道使用SAX框架(或类似的东西)和Java编写XML的好教程(或有一个很好的例子)?搜索在有用结果方面收效甚微。我正在尝试从Android应用程序导出,并希望避免尽可能多的内存开销。

Anyone know of a good tutorial (or have a good example) for writing XML using the SAX framework (or something similar) and Java? Searching has yielded very little in terms of useful results. I'm trying to export from an Android app and am looking to avoid as much memory overhead as possible.

推荐答案

有一个非常通过 SAX框架(不是SAX解析器,而是SAX框架)直接从POJO生成生成 XML的有用技术。此技术可用于生成XML文档

There's a very useful technique for generating XML directly from POJOs via the SAX framework (not a SAX parser, but the SAX framework). This technique could be used to generate an XML document.

从任意数据结构生成XML

http://download.oracle.com/javaee/1.4/tutorial/doc/JAXPXSLT5。 html

Generating XML from an Arbitrary Data Structure
http://download.oracle.com/javaee/1.4/tutorial/doc/JAXPXSLT5.html

基本上,您为POJO添加方法或为POJO编写实用程序类,将它们转换为SAX事件发射器(通常像SAX解析器一样发出事件)将解析XML文档时)。现在,您的SAX事件生成器看起来像SAX解析器的输出端,并且可以被赋予SAX解析器将采用的任何内容处理程序,例如pretyy打印XML的内容处理程序。但它也可以提供给DOM解析器以生成DOM树或提供给XSLT引擎以生成HTML或执行真正的XSL转换,而无需首先从POJO生成中间XML文档。

Essentially, you add methods to your POJO or write utility class for your POJOs that turn them into SAX event emitters (emitting events like a SAX parser normally would when parsing an XML document). Now your "SAX event generator" looks like the output side of a SAX parser and can be given any content handler that a SAX parser would take, such as one that pretyy prints XML. But it could also be feed to a DOM parser to generate a DOM tree or feed to an XSLT engine to generate HTML or do a true XSL translation without having to first generate an intermediate XML document from the POJOs.

例如,Person类可能包含 emitXML()方法,其中包含以下行:

For example, a Person class might have an emitXML() method that include these lines:

handler.startElement(nsu, PERSON_TAG, PERSON_TAG, NO_ATTRIBUTES);

handler.startElement(nsu, FIRSTNAME_TAG, FIRSTNAME_TAG, atts);
handler.characters(this.firstName.toCharArray(), 
        0,
        this.firstName.length());
handler.endElement(nsu, FIRSTNAME_TAG, FIRSTNAME_TAG);

... emit more instance variables

... emit child object like: homeAddress.emitXML(handler, ...);

handler.endElement(nsu, PERSON_TAG, PERSON_TAG);

更新:

其他一些参考:

  • Transform Legacy Data to XML Using JAXP
    http://www.devx.com/java/Article/16925
  • Transforming Flat Files To XML With SAX and XSLT
    http://www.developer.com//xml/article.php/2108031/Transforming-Flat-Files-To-XML-With-SAX-and-XSLT.htm

对评论的几个回复:


这是真的,但上面描述的XMLStreamWriter接口更加用户友好。 - Michael Kay 3小时前

This is true, but the XMLStreamWriter interface described above is much more user-friendly. – Michael Kay 3 hours ago

是的,但我想我不清楚。我可以轻松遍历层次结构并使用 XMLStreamWriter 直接将XML文档输出到流。 但是,文章展示了一种强大的技术来遍历层次结构并生成SAX事件,而不是直接输出XML文档。现在我可以插入不同内容处理程序来执行不同的操作或生成不同版本的XML。 我们还可以将对象层次结构提供给任何接受SAX解析器的工具,如XSLT引擎。它实际上只是利用了SAX框架建立的访问者模式:我们将遍历层次结构与输出XML分开。输出XML的部分(内容处理程序)当然应该使用 XMLStreamWriter ,如果它们的目的是编写XML流。

Yes, but I guess I wasn't clear. I could easy traverse the hierarchy and use XMLStreamWriter to directly output an XML document to a stream. However, the articles show a powerful technique to traverse the hierarchy and generate SAX events, instead of outputting an XML document directly. Now I can plug-in different content handlers that do different things or generate different versions of the XML. We could also feed our object hierarchy to any tool that accepted a SAX parser, like an XSLT engine. Its really just taking advantage of the visitor pattern established by the SAX framework: we separate traversing the hierarchy from output the XML. The parts that output the XML, the content handlers, should certainly use an XMLStreamWriter if their purpose is to write an XML stream.

例如,在我们的程序中,我们通过分布式组件之间的网络套接字发送XML消息,我们还使用XSLT生成HTML页面。以前,我们遍历我们的层次结构以生成XML文档(字符串),然后将该XML文档写入网络套接字或将该文档提供给XSLT引擎(基本上只是再次解析它)。使用这种技术后,我们基本上可以将对象层次结构(使用此SAX适配器)直接提供给XSLT引擎,而无需中间XML字符串。能够使用一个内容处理程序为网络流生成紧凑的XML表示并使用另一个生成漂亮打印的XML文档以写入日志文件也很方便。

For example, on our program, we sent XML messages over network sockets between distributed components and we also used XSLT to generate our HTML pages. Previously, we traversed our hierarchy to generate a XML document (a string) and then either wrote that XML document to a network socket or fed that document to the XSLT engine (which essentially just parsed it again). After using this technique, we could essentially feed our object hierarchy (using this SAX adapter) directly to the XSLT engine without needing the intermediate XML string. It was also convenient to be able to use one content handler to generate a compact XML representation for the network stream and use a different one to generate a pretty-printed XML document for writing to a log file.


此外,使用SAX解析器API编写XML是对API的滥用,恕我直言。 - Puce 49分钟前

Besides, using SAX parser API to write XML is a misuse of the API, IMHO. – Puce 49 mins ago

或许,但我认为这取决于您的需求。如果OP的要求只是写出一个特定的XML文档,那么这肯定是矫枉过正的。但是,我认为值得一提的是,如果OP在他的项目中以其他方式使用XML而他没有提及。推销替代想法没有坏处。

Perhaps, but I think it depends on your needs. If OP's requirement is just to write out an a specific XML document, then this is definitely overkill. However, I thought it worth mentioning if the OP uses XML in other ways on his project that he didn't mention. There's no harm in pitching an alternative idea.

称之为误用可能有点强,但我同意你有权获得你的意见。它记录在Oracle教程中,因此不被Sun / Oracle工程师视为滥用。我们的项目非常成功,可以帮助我们满足我们的要求而没有明显的缺点,因此我将在我的工具箱中保留这种方法,以便将来有用。

Calling it misuse may be a bit strong, but I agree you're entitled to your opinion. Its documented in an Oracle tutorial, so its not considered abuse by the Sun/Oracle engineers. It was highly successful on our project to help us meet our requirements with no significant downsides, so I'll be keeping this approach in my toolbox for when its useful in the future.

这篇关于使用SAX和Java生成XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆