3

I've read a time ago about generate xml from Java using annotations, but I'm not finding a simple example now.

If I want to make a xml file like:

<x:element uid="asdf">value</x:element>

from my java class:

public class Element {
  private String uid = "asdf";
  private String value = "value";
}

Which annotations should I use to perform that? (I have a xml-schema, if this helps the generation)

--update

The javax.xml.bind.annotation package have the annotations, "but I still haven't found what I'm looking for": an exemple of usage.. :)

1

3 Answers 3

1

Found it:

import java.io.FileOutputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlRootElement;

public class JavaToXMLDemo {
  public static void main(String[] args) throws Exception {
    JAXBContext context = JAXBContext.newInstance(Employee.class);

    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    Employee object = new Employee();
    object.setCode("CA");
    object.setName("Cath");
    object.setSalary(300);

    m.marshal(object, System.out);

  }
}

@XmlRootElement
class Employee {
  private String code;

  private String name;

  private int salary;

  public String getCode() {
    return code;
  }

  public void setCode(String code) {
    this.code = code;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public int getSalary() {
    return salary;
  }

  public void setSalary(int population) {
    this.salary = population;
  }
}
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee>
    <code>CA</code>
    <name>Cath</name>
    <salary>300</salary>
</employee>

From: http://www.java2s.com/Code/JavaAPI/javax.xml.bind.annotation/javaxxmlbindannotationXmlRootElement.htm

Sign up to request clarification or add additional context in comments.

Comments

1

For the benefit of anyone else hitting this thread, I imagine you did the following:

@XmlRootElement
public class Element { 

  @XmlAttribute
  private String uid = "asdf"; 

  @XmlValue
  private String value = "value"; 
} 

For More Information

Comments

1

There are various tools that you can use to do this. XStream (http://x-stream.github.io/) is a reasonably easy tool to use that allows you to use annotations to determine the schema of XML that is created.

5 Comments

I would like JDK included tools
You'll have to go outside the JDK, What you are looking for is a tool that customizes serialization--I think that's what XStream does. Instead of serialization going to a binary unreadable mess it goes to XML. Of course that might not give you exactly what you are looking for, but it's the closest thing i can think of.
@Brito: yes, JAXB does exactly that. XStream is not strictly necessary (it's a third-party library that existed before JAXB, if I remember correctly).
@Joachim - JAXB was created first.
@Blaise: interesting, I didn't know that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.