一个不错的 Java XML DOM 实用程序

发布于 2024-11-28 19:07:30 字数 1480 浏览 0 评论 0原文

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

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。这样做的原因是 这

  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");

我这里的重要要求是我明确想要对

  1. 已经存在的
  2. org.w3c.dom.Document 进行操作相当大,
  3. 需要相当多因此

,将 org.w3c.dom.Document 转换为 JDOM、dom4j 等似乎是一个坏主意。我更喜欢用适配器包裹它。

如果它不存在,我可能会推出自己的,因为这个 jRTF 语法看起来非常好!对于 XML 来说,它似乎很容易实现,因为节点类型很少。从 Fluent API 的角度来看,这可能会变得像 jquery 一样强大!

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);

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. It's about an old and big legacy system
  2. The XML is used in many places and XSLT transformed several times to get XML, HTML, PDF output
  3. I'm just looking for convenience, not a big change.

I'm just wondering if there is a nice wrapper library (e.g. with apache commons or google) that allows me to do things like this with a fluent style similar to 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")
);

and then

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. already exists
  2. is pretty big
  3. needs quite a bit of manipulation

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.

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!

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

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

发布评论

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

评论(3

安穩 2024-12-05 19:07:30

为了详细阐述我的评论,Dom4J 让您非常接近您想要的:

final Document dom = DocumentHelper.createDocument().addElement("some-name")
        .addAttribute("attr1", "val1")
        .addAttribute("attr2", "val2")
        .addElement("some-other-name").getDocument();
System.out.println(dom.asXML());

输出:

<?xml version="1.0" encoding="UTF-8"?>
<some-name attr1="val1" attr2="val2"><some-other-name/></some-name>

我知道它不是本机 DOM,但它非常相似,并且为 Java 开发人员提供了非常好的功能(元素迭代器、实时元素列表等)

To elaborate my comment, Dom4J gets you pretty close to what you wanted:

final Document dom = DocumentHelper.createDocument().addElement("some-name")
        .addAttribute("attr1", "val1")
        .addAttribute("attr2", "val2")
        .addElement("some-other-name").getDocument();
System.out.println(dom.asXML());

Output:

<?xml version="1.0" encoding="UTF-8"?>
<some-name attr1="val1" attr2="val2"><some-other-name/></some-name>

I know it's not native DOM, but it's very similar and it has very nice features for Java developers (element iterators, live element lists etc.)

故事灯 2024-12-05 19:07:30

我发现一些工具大致可以满足我在问题中的要求:

但是,与此同时,我更倾向于自己推出。我真的是 jquery 的忠实粉丝,我认为 jquery 可以映射到 Java Fluent API:

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

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

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

昔梦 2024-12-05 19:07:30

嗯,这可能很愚蠢,但是为什么不自己实现这个小 API呢?我相信您非常了解 DOM API,并且不会花费太多时间来实现您想要的。

顺便说一句,请考虑使用 XPath 来操作文档(您也可以在此基础上实现您的迷你 api)。

Well, this is maybe silly but why don't you implement that little API on your own? I'm sure you know DOM API pretty well and it won't take much time to implement what you want.

Btw consider using XPath for manipulation with document (you can also implement your mini-api over this one).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文