自定义 Java XMLBuilder 与基于标准类的比较

发布于 2024-10-07 03:58:38 字数 1392 浏览 3 评论 0原文

XML 生成的最佳性能解决方案是什么?

我的目标是从代码构建一些简单的 XML。我将实现 XML Builder 的基于简单自定义 StringBuffer 的实现。从另一边来看,有几个库,例如 http://code.google.com/p/java- xmlbuilder/http://code.google.com/p/xmltool/ 它有很好的 DSL,但我猜性能较差。

由于我的目标是构建足够简单且具有出色性能的 XMLBuilder,我想我将构建自定义解决方案。它将具有以下特点:

  • 用于 XML 构造的基于 Java 的良好 DSL(基本上添加标签)
  • 基于 StringBuffer 的出色性能。
  • 添加 XML 标签时的字符串数据转义处理。
  • 自动缩进

如果我对性能期望有误,请提出建议,最好使用现成的库。

更新。 为什么我认为标准 xml 构建器的性能不是很好。

标准 XML 构建器使用 Document Builder Factory 并在幕后处理类。这些类别还经过优化以适合所有用户。例如,我不需要命名空间支持等。

<?xml version="1.0" encoding="utf-8">
<root>
 <testdata>value</testdata>
</root>
</xml>

考虑上面非常简单的 XML 代码。如果您使用标准工具进行构建,那么仅仅为了制作这个简单的 XML 就需要做很多工作。我认为最好自己使用 String 生成它。

更新2。性能要求是代码应该执行生成简单 XML 所需的尽可能多的操作,而不是更多。

更新3.感谢大家的精彩评论!现在我更好地理解了我需要什么,并且我最初的目标并没有用“性能”这个词设定得非常正确。我的真正目标是使用足够简单的解决方案和方便的 DSL 来描述 XML 结构并生成 XML 输出。

我将使用普通 Java 对象作为 XML 的 DSL,并使用 XStream 库生成 XML,这是非常简单的解决方案。

更新 4. JAXB。我讨论了 XStream 与 JAXB,发现 JAXB 比 XStream 更快。另外,我已经在我的项目中使用了 JAXB,并且我喜欢它的标准注释。我改变了主意,现在会选择 JAXB,因为 XStream 最初是在 JAXB 还不如今天那么好的时候大力开发的。

What is the best performance solution for XML generation.

My goal is to build a few simple XMLs from code. I am going to implement simple custom StringBuffer based implementation of XML Builder. From other side there are several libraries like http://code.google.com/p/java-xmlbuilder/ and http://code.google.com/p/xmltool/ which has nice DSL but I guess lack on performance.

Since my goal is build simple enough XMLBuilder with great performance I think I will build custom solution. It will featuring:

  • Nice Java-based DSL for XML constructs (adding tags basically)
  • Great StringBuffer based performance.
  • String data escape handling when adding XML tags.
  • Auto-indent

Please suggest if I am wrong on performance expectations and its probably better to use ready-made libraries.

UPDATE. Why I think the performance of standard xml builders is not very good.

Standard XML builders uses Document Builder Factory and works with classes behind the scenes. Also these classes optimized to fit all users. For example I don't need namespace support etc.

<?xml version="1.0" encoding="utf-8">
<root>
 <testdata>value</testdata>
</root>
</xml>

Consider very simple XML code above. If you build with standard tools it will involve so many work just to make this simple XML. I consider that it's better to just generate it by myself using String.

UPDATE 2. Performance requirement is that code should do as many things as required to generate simple XML and not more.

UPDATE 3. Thanks everyone for great comments! Now I understand better what I need and that my initial goal was not set very correctly with word "performance". My true goal is to use simple enough solution with convenient DSL to describe the XML structure and generate the XML output.

I will use plain Java objects as DSL for XML and generate XML using XStream library which is pretty straightforward solution.

UPDATE 4. JAXB. I discussed XStream vs JAXB and found that JAXB is faster than XStream. Plus I already use JAXB in my project and I like its standard annotations. I change my mind and will go with JAXB for now because XStream was originally heavily developed at the time when JAXB was not so good as today.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

迟月 2024-10-14 03:58:38

我会建议一些非常有争议的东西,但仍然......

使用这两个库进行分析和性能测试

如果您没有时间这样做,那么在我看来,假设某些事情很慢将是错误的选择。
因为如果事实证明它实际上并不慢,那么使用已经构建和支持的库/框架将为您节省大量时间。

另一个想法。
您需要根据现有的解决方案来测试您已完成的高性能解决方案,以检查它是否真正具有高性能。因此,我强烈建议您在开始自己的库之前先测量可用库的性能。

I will suggest something very controversial but still ...

Make profiling and performance tests with both libraries.

If you don't have time for that, assuming something is slow would be the wrong choice in my opinion.
Because if it turns out that it actually is not slow, it would save you a lot of time to use an already built and supported library/framework.

Another thought.
You will need to test your completed high performance solution against the solutions already available anyway, to check if it is really high performance. So I would strongly suggest measuring the performance of the libraries available before starting your own.

北方的韩爷 2024-10-14 03:58:38

关于:

标准 XML 构建器使用文档
构建器工厂并与类一起使用
在幕后。还有这些课程
优化以适合所有用户。为了
例如我不需要命名空间支持
等等

DOM 的替代品是 StAX (JSR-173) 它是一个非常快的 XML 流 API。有几种实现,我发现 Woodstox 性能相当好。

Regarding:

Standard XML builders uses Document
Builder Factory and works with classes
behind the scenes. Also these classes
optimized to fit all users. For
example I don't need namespace support
etc.

An alternative to DOM is StAX (JSR-173). It is a Streaming API for XML that is quite fast. There are several implementations, I have found Woodstox to be quite performant.

岁月静好 2024-10-14 03:58:38

有强大而灵活的 Groovy 的 NodeBuilder (http://groovy.codehaus.org/GroovyMarkup)。

def root = new NodeBuilder()
  .people(kind:'folks', groovy:true) {
    person(x:123,  name:'James', cheese:'edam') {
      project(name:'groovy')
      project(name:'geronimo')
    }
    person(x:234,  name:'bob', cheese:'cheddar') {
      project(name:'groovy')
      project(name:'drools')
    }
  }
XmlUtil.serialize(root, System.out)

结果是一个 XML 文档:

<?xml version="1.0" encoding="UTF-8"?>
<people kind="folks" groovy="true">
  <person x="123" name="James" cheese="edam">
    <project name="groovy"/>
    <project name="geronimo"/>
  </person>
  <person x="234" name="bob" cheese="cheddar">
    <project name="groovy"/>
    <project name="drools"/>
  </person>
</people>

There is powerful and flexible Groovy's NodeBuilder (http://groovy.codehaus.org/GroovyMarkup).

def root = new NodeBuilder()
  .people(kind:'folks', groovy:true) {
    person(x:123,  name:'James', cheese:'edam') {
      project(name:'groovy')
      project(name:'geronimo')
    }
    person(x:234,  name:'bob', cheese:'cheddar') {
      project(name:'groovy')
      project(name:'drools')
    }
  }
XmlUtil.serialize(root, System.out)

This results with an XML document:

<?xml version="1.0" encoding="UTF-8"?>
<people kind="folks" groovy="true">
  <person x="123" name="James" cheese="edam">
    <project name="groovy"/>
    <project name="geronimo"/>
  </person>
  <person x="234" name="bob" cheese="cheddar">
    <project name="groovy"/>
    <project name="drools"/>
  </person>
</people>
⒈起吃苦の倖褔 2024-10-14 03:58:38

另一种高性能建议:使用 StaxMate —— 它与底层基于 Stax 的 XML 编写器一样快,这是相当快(每秒 40 - 80 兆字节,持续)。只需确保您不使用默认的 JDK 6 Stax 实现(Sun sjsxp),而是使用更快的东西,例如 Woodstox 或 Aalto。

我强烈建议您不要编写自己的 XML 编写器;正如其他人提到的那样,它通常是有风险的(很有可能你会忘记转义的某些部分),而且并不是所有的解决方案都比现有的高效解决方案更快(并非所有现有的解决方案都是有效的;你确实需要找到那些)。最后……除非你真的想写这些东西,否则为什么不做一些更有趣、更有意义的事情呢?

但是,如果您确实想做一些超越现有编写器的事情,您可以考虑使用一个简单的编写器,并用您需要的附加功能来增强它。例如,如果您仅使用 Stax XMLStreamWriter 作为基础,那么添加简单但有效的抽象是非常容易的。或者,如果您喜欢现有的软件包,请查看是否可以向其作者(甚至代码贡献)提出改进建议。

One more high-performance suggestion: use StaxMate -- it is as fast as underlying Stax-based XML writer, which is rather fast (40 - 80 megabytes per second, sustained). Just make sure you do NOT use default JDK 6 Stax implementation (Sun sjsxp) but something faster like Woodstox or Aalto.

I would strongly recommend against writing your own XML writer; it is typically risky (good chance you will forget some part of escaping) as others have mentioned, and not all that likely to be faster than existing efficient solutions (not all existing solutions are efficient; you do need to find ones that are). And in the end... unless you really want to write these things, why not work on something more interesting and meaningful?

But if you do want to do something above and beyond existing writers you could consider using a simple writer and augmenting it with additional functionality that you need. For example, if you just use Stax XMLStreamWriter as base, it is quite easy to add simple but efficient abstractions. Or if you like existing packages, see if you can suggest improvements to their authors (or even code contributions).

薆情海 2024-10-14 03:58:38

有强大的 XmlBuilder

import com.github.underscore.XmlBuilder;

var xmlBuilder =
    XmlBuilder.create("people")
        .a("kind", "folks")
        .a("groovy", "true")
        .e("person")
        .a("x", "123")
        .a("name", "James")
        .a("cheese", "edam")
        .e("project")
        .a("name", "groovy")
        .a("self-closing", "true")
        .up()
        .e("project")
        .a("name", "geronimo")
        .a("self-closing", "true")
        .up()
        .up()
        .e("person")
        .a("x", "234")
        .a("name", "bob")
        .a("cheese", "cheddar")
        .e("project")
        .a("name", "groovy")
        .a("self-closing", "true")
        .up()
        .e("project")
        .a("name", "drools")
        .a("self-closing", "true");
System.out.println(xmlBuilder.toXml(Xml.XmlStringBuilder.Step.FOUR_SPACES));

这会产生一个 XML 文档:

<?xml version="1.0" encoding="UTF-8"?>
<people kind="folks" groovy="true">
    <person x="123" name="James" cheese="edam">
        <project name="groovy"/>
        <project name="geronimo"/>
    </person>
    <person x="234" name="bob" cheese="cheddar">
        <project name="groovy"/>
        <project name="drools"/>
    </person>
</people>

There is powerful XmlBuilder

import com.github.underscore.XmlBuilder;

var xmlBuilder =
    XmlBuilder.create("people")
        .a("kind", "folks")
        .a("groovy", "true")
        .e("person")
        .a("x", "123")
        .a("name", "James")
        .a("cheese", "edam")
        .e("project")
        .a("name", "groovy")
        .a("self-closing", "true")
        .up()
        .e("project")
        .a("name", "geronimo")
        .a("self-closing", "true")
        .up()
        .up()
        .e("person")
        .a("x", "234")
        .a("name", "bob")
        .a("cheese", "cheddar")
        .e("project")
        .a("name", "groovy")
        .a("self-closing", "true")
        .up()
        .e("project")
        .a("name", "drools")
        .a("self-closing", "true");
System.out.println(xmlBuilder.toXml(Xml.XmlStringBuilder.Step.FOUR_SPACES));

This results with an XML document:

<?xml version="1.0" encoding="UTF-8"?>
<people kind="folks" groovy="true">
    <person x="123" name="James" cheese="edam">
        <project name="groovy"/>
        <project name="geronimo"/>
    </person>
    <person x="234" name="bob" cheese="cheddar">
        <project name="groovy"/>
        <project name="drools"/>
    </person>
</people>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文