Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 249887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:27:54+00:00 2026-05-11T21:27:54+00:00

I need to build an XML document from a Java object hierarchy. Both the

  • 0

I need to build an XML document from a Java object hierarchy. Both the Java classes and the XML format are fixed. So I can’t use an XML serializer like XStream: it bases the XML format on the Java classes. Likewise, a Java XML binding technology like JAXB won’t work, since it creates Java classes from the XML schema [ed: but see below]. I need a manual approach.

The low-tech StringBuilder route results in fragile and buggy code (at least for me!).

An API like JAXP or JDOM leads to much more robust code, but these are pretty verbose.

Groovy has an elegant MarkupBuilder:

def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
xml.records() {
  car(name:'HSV Maloo', make:'Holden', year:2006) {
    country('Australia')
    record(type:'speed', 'Production Pickup Truck with speed of 271kph')
  }
  car(name:'P50', make:'Peel', year:1962) {
    country('Isle of Man')
    record(type:'size', 'Smallest Street-Legal Car at 99cm wide and 59 kg')
  }
}

Other languages (eg. Ruby) have even better ones, though I want to stay with pure Java. There do seem to be some new XML builders for Java, such as practicalxml and James Murty’s xmlbuilder.

What are the more elegant approaches for building XML documents in Java?

Summary:

Jon Doe suggested dom4j and jdom.

CurtainDog recommended using JAXB anyway, and jherico clued me in that this was a pertinant suggestion: you could then use Dozer to map between my current JavaBeans and the JAXB JavaBeans.

thaggie recommends JIBX and agreed with CurtainDog and jherico that binding technologies are actually practical.

StaxMan recommends StaxMate.

Of the stuff I’ve looked at, practicalxml and James Murty’s xmlbuilder seem to be the most concise builders, though are rather newish. The binding technologies like JAXB seem to offer extra safety/automation. Of the mainstream choices, dom4j seems decent, though still kind of verbose. It offers a “fluent interface” (mutators return a reference to the mutated object so they can be chained together), which I like:

public Document createDocument() {
    Document document = DocumentHelper.createDocument();
    Element root = document.addElement( "root" );
    Element author2 = root.addElement( "author" )
      .addAttribute( "name", "Toby" )
      .addAttribute( "location", "Germany" )
      .addText( "Tobias Rademacher" );
    Element author1 = root.addElement( "author" )
      .addAttribute( "name", "James" )
      .addAttribute( "location", "UK" )
      .addText( "James Strachan" );
    return document;
}

For conciseness, you could wrap a thin facade over this API to provide terse synonyms for some of these methods (eg, attr() instead of addAttribute()).

Thanks all!

P.S.: Stephan Schmidt worked on a Java MarkupBuilder, though seems not to have published it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T21:27:54+00:00Added an answer on May 11, 2026 at 9:27 pm

    dom4j or jdom are probably the most elegant, you can write code how you like it. Dom4j has builders if I recall, and yes the code is more verbose.

    Element.addElement("x").setAttribute("x", "y").xxxxx;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 137k
  • Answers 137k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Take a look here, someone did a lot of experimenting… May 12, 2026 at 7:25 am
  • Editorial Team
    Editorial Team added an answer I don't think you can assign a region of a… May 12, 2026 at 7:25 am
  • Editorial Team
    Editorial Team added an answer The difficulty comes from the time constraint more than anything… May 12, 2026 at 7:25 am

Related Questions

I built a template-based document generator using the Open XML SDK (1.0) , the
I am putting an XSL together than will create a NAnt build script using
I want to allow users to upload files with transaction information to my application.
I work a lot with external Web Services, and I need to create an

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.