Simple way to do Xml in Java

11,082

Solution 1

I recommend XOM. Its API is clear and intuitive.

Solution 2

You should check out Xstream. There is a 2 minute tutorial that is really simple. To get the same format, you would model the classes the same.

Solution 3

If you are using jdk 1.4 or newer take a look at XMLEncoder class.

Solution 4

Some of the more popular approaches to consider:

Java Archictecture for XML Binding

JAXB is a specification for a standard XML binding. If you already have an XSD, it can generate your Java classes for you, and then all that's left is to use a standard API for marshalling/unmarshalling.

  • Reference implementation from Glassfish
  • Apache's implementation JaxMe

Other binding approaches

As with JAXB, these approaches use XML-based binding configurations. They may provide more fine grained control of the unmarshalling process.

Roll your own

Solution 5

Dom4j is a simple api for creating xml documents in java.

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

Element author2 = root.addElement( "author" )
  .addAttribute( "name", "Toby" )
  .addAttribute( "location", "Germany" )
  .addText( "Tobias Rademacher" );
Share:
11,082
Omar Kooheji
Author by

Omar Kooheji

I've given up a life of code and turned to the dark side. I now lead a team of talented software engineers to deliver quality systems.

Updated on June 13, 2022

Comments

  • Omar Kooheji
    Omar Kooheji almost 2 years

    Is there is Simple way to read and write Xml in Java?

    I've used a SAX parser before but I remember it being unintuitive, I've looked at a couple of tutorials for JAXB and it just looks complicated.

    I don't know if I've been spoilt by C#'s XmlDocument class, but All I want to do is create an Xml Document that represents a a set of classes and their members (some are attributes some are elements).

    I would look into serialization but the XML has to have the same format as the output of a c# app which I am reverse engineering into Java.