一个很好的Java XML DOM实用程序 [英] A nice Java XML DOM utility

查看:77
本文介绍了一个很好的Java XML DOM实用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现自己一次又一次地编写了相同的详细DOM操作代码:

I find myself writing the same verbose DOM manipulation code again and again:

Element e1 = document.createElement("some-name");
e1.setAttribute("attr1", "val1");
e2.setAttribute("attr2", "val2");
document.appendChild(e1);

Element e2 = document.createElement("some-other-name");
e.appendChild(e2);

// Etc, the same for attributes and finding the nodes again:
Element e3 = (Element) document.getElementsByTagName("some-other-name").item(0);

现在,我不想将架构全部切换到一起,即我不想使用JDOM,JAXB或其他任何东西。只是Java的 org.w3c.dom 。原因是

Now, I don't want to switch architecture all together, i.e. I don't want to use JDOM, JAXB, or anything else. Just Java's org.w3c.dom. The reasons for this are


  1. 这是一个古老而大型的遗留系统。

  2. 使用XML在很多地方和XSLT转换了几次,以获得XML,HTML,PDF输出

  3. 我只是寻找方便,而不是一个很大的变化。



    我只是想知道是否有一个漂亮的包装库(例如使用apache commons或者google),这样我可以用类似于 jRTF

// create a wrapper around my DOM document and manipulate it:
// like in jRTF, this code would make use of static imports
dom(document).add(
  element("some-name")
    .attr("attr1", "val1")
    .attr("attr2", "val2")
    .add(element("some-other-name")),
  element("more-elements")
);

然后

Element e3 = dom(document).findOne("some-other-name");

我在这里的重要要求是我明确要在

The important requirement I have here is that I explicitly want to operate on a org.w3c.dom.Document that


  1. 已经存在

  2. 是相当大的

  3. 需要相当多的操纵

所以转换 org.w3c.dom.Document 到JDOM,dom4j等似乎是一个坏主意。如果不存在,我可能会滚动自己的,因为这个jRTF语法看起来真的很好!

So transforming the org.w3c.dom.Document into JDOM, dom4j, etc seems like a bad idea. Wrapping it with adapters is what I'd prefer.

对于XML,它似乎很容易实现,因为只有很少的节点类型。这可能会从流畅的API角度来看与jquery一样强大!

If it doesn't exist, I might roll my own, as this jRTF syntax looks really nice! And for XML, it seems quite easy to implement, as there are only few node types. This could become as powerful as jquery from the fluent API perspective!

推荐答案

我发现了一些粗略地做我所要求的工具我的问题:

I found some tools that roughly do what I asked for in my question:

  • http://code.google.com/p/xmltool/
  • http://jsoup.org/

然而,与此同时,我更倾向于滚动自己的。我真的是jquery的粉丝,我认为jquery可以映射到Java流畅的API:

However, in the mean time, I am more inclinded to roll my own. I'm really a big fan of jquery, and I think jquery can be mapped to a Java fluent API:

http://www.jooq.org/products/jOOX

这篇关于一个很好的Java XML DOM实用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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