简体   繁体   English

用Java以编程方式创建W3C DOM的有效方法是什么?

[英]What is an efficient way to create a W3C DOM programatically in Java?

Although I know how to build a DOM the long, arduous way using the DOM API, I'd like to do something a bit better than that. 尽管我知道如何使用DOM API以漫长而艰巨的方式构建DOM,但我想做些更好的事情。 Is there a nice, tidy way to build hierarchical documents with, say, an API that works something like Hibernate's Criteria API? 有没有一种很好的,整洁的方法来构建分层文档,例如,使用类似于Hibernate的Criteria API的API? So that I can chain calls together like this, for example: 这样我就可以像这样将调用链接在一起:

Document doc = createDocumentSomehow ();
doc.createElement ("root").createElements (
    doc.newElement ("subnode")
        .createElement ("sub-subnode")
            .setText("some element text")
            .addAttribute ("attr-name","attr-value"),
    doc.newElement ("other_subnode"));

Ideally, this would result in XML like this: 理想情况下,这将导致XML如下所示:

<root>
  <subnode>
    <sub-subnode attr-name = "attr-value">some element text</sub-subnode>
  <other_subnode />
</root>

Basically, I'd like something where the Java itself isn't nearly four times longer than the document I'm generating. 基本上,我希望Java本身的长度不比我生成的文档长四倍。 Does it exist? 是否存在?

You definitely want to use JDom : http://www.jdom.org/docs/apidocs/ . 您肯定要使用JDomhttp : //www.jdom.org/docs/apidocs/ It can be used as you described as many methods return a reference to this . 正如您所描述的,可以使用它,因为许多方法都返回this的引用。 Here is some code our teacher showed us for this XML document. 这是老师为我们展示的有关此XML文档的一些代码。 Haven't tested it, but the teacher is great i believe in him: 还没有测试过,但是老师很棒,我相信他:

<adressbuch aktualisiert="1.4.2008">
    <adresse>
        <vorname> Hugo </vorname>
        <nachname> Meier </nachname>
        <telefon typ="mobil">0160/987654 </telefon>
    </adresse>
</adressbuch>

Code: 码:

new Document(
     new Element ("adressbuch")
     .setAttribute("aktualisiert", "1.4.2008")
     .addContent(
         (Element) new Element("adresse")
         .addContent(
                     (Element) new Element("vorname")
                     .addContent("Hugo"))
         .addContent(
                     (Element) new Element("nachname")
                     .addContent("Meier"))
         .addContent(
                     (Element) new Element("telefon")
                     .setAttribute("typ", "mobil")
                     .addContent("0160/987654"))));

From the API manual, it looks like the casts he did aren't necassary. 从API手册看来,他所做的演员表并不是不必要的。 Maybe he just did it for documentation purposes. 也许他只是出于文档目的。

I highly recommend Elliotte Rusty Harold's XOM API. 我强烈推荐Elliotte Rusty Harold的XOM API。

It inter-operates with the W3C API, in that you can convert between XOM and DOM. 它可以与W3C API互操作,因为您可以在XOM和DOM之间进行转换。 The API guarantees a well-formed structure at all times. API始终保证结构良好。 It's performant, robust, and follows consistent design principles. 它性能强大,健壮,并遵循一致的设计原则。

Try looking at the Quick Start guide for DOM4J. 尝试查看DOM4J的快速入门指南。 It makes getting the XML out pretty easy too. 这也使XML的发布变得非常容易。 I've included a relevant snippet: 我提供了一个相关代码段:

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

public class Foo {

    public Document createDocument() {
        Document document = DocumentHelper.createDocument();
        Element root = document.addElement( "root" );

        Element author1 = root.addElement( "author" )
            .addAttribute( "name", "James" )
            .addAttribute( "location", "UK" )
            .addText( "James Strachan" );

        Element author2 = root.addElement( "author" )
            .addAttribute( "name", "Bob" )
            .addAttribute( "location", "US" )
            .addText( "Bob McWhirter" );

        return document;
    }
}

如果您愿意在Java应用程序中使用Groovy,则可以使用MarkupBuilder进行敏捷XML创建

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM