5

I want to create one XML file from one list of objects. Objects are having some attributes, so the tags will be the attribute names and the respective data will be inside the tag.

This is example:

I have one List myEquipmentList, that contains 100 objects of the class Equipment. Now, the attributes in the class of Equipment are id, name, size, measures, unit_of_measure etc.

Now I want to create XML which will be something like this.

<Equipment id=1>``
<name>Ruler</name>
<size>1000</size>
<measures>length</measures>
<unit_of_measure>meter</unit_of_measure>
</Equipment>

Any ideas?

Alex Kulinkovich
  • 4,408
  • 15
  • 46
  • 50
Pranav
  • 85
  • 1
  • 2
  • 5

4 Answers4

4

you can create a class with the list of objects, then serialise the list to xml and finally deserialise xml to a list.

Please see this link - Very useful: How to convert List of Object to XML doc using XStream

Community
  • 1
  • 1
Pita
  • 498
  • 5
  • 11
  • 21
  • Thanks Priya, I think this option will be easy and simple.. Thanks alot@ – Pranav Jul 13 '12 at 11:45
  • No Problem. Please do accept the answer if you think I have answered your question :) – Pita Jul 13 '12 at 11:53
  • I don't think it has been accepted properly yet. its not appearing on my profile. – Pita Jul 13 '12 at 12:04
  • I got one message at bottom, like was this post helpful to u I selected yes..Anything else I need to do? – Pranav Jul 13 '12 at 12:07
  • Well yeah, to the left of the question, where the numbers are, below that there is a arrow kind of thing which you can mark as answered. it should go green once you selected it. – Pita Jul 13 '12 at 12:29
4

Read about JAXB.

You could have a class like this that would generate the XML you want:

@XmlRootElement
public class Equipment {
  private Long id;
  private String name;
  private Integer size;
  ...etc...

  @XmlAttribute
  public Long getId() {
     return id;
  }

  public void setId(Long id) {
     this.id = id;
  }

  @XmlElement
  public String getName() {
    return name;
  }

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

  ... etc...

}

You'll find plenty of info on JAXB on google on searching on stackoverflow.

http://jaxb.java.net/

http://jaxb.java.net/tutorial/

These look like a couple of simple tutorials:

http://www.mkyong.com/java/jaxb-hello-world-example/

http://www.vogella.com/articles/JAXB/article.html

MattR
  • 6,908
  • 2
  • 21
  • 30
  • Hi, Can you please give me some relevant link about JAXB so that I can map this answer and I will get about it. Currently I am not getting anything from this answer.... – Pranav Jul 13 '12 at 11:59
  • @user1502359 googling JAXB resulted in nothing? – Woot4Moo Jul 13 '12 at 12:07
  • Thanks alott MattR!! Surely these articles(especially the last 2 links) are very helpful and will give me chance to learn new thing!! – Pranav Jul 13 '12 at 12:25
1

One of the easiest ways to do this is simply iterate over the list and use strings to write the XML. Nothing special, very quick and easy.

Francis Upton IV
  • 19,322
  • 3
  • 53
  • 57
  • thanks for the quick reply.... but I did not get your solution clearly.. Can you please elaborate it a little... – Pranav Jul 13 '12 at 11:41
0

I tend to use a library called Simple XML Serialization over JAXB, and I have to say it's pretty simple, yet extremely flexible.

There's good comparison between Simple and JAXB here.

andyroberts
  • 3,458
  • 2
  • 37
  • 40