0

I would like to create a XML file in this format:

<msgs>
    <msg>
        <type>sense</type>
        <cid>168</cid>
        <ip>192.168.140.150</ip>
        <errs/>
        <id>002EC0FEFF83EA97</id>
        <sensors>
            <sensor sid='002EC0FEFF8FFF27' desc='' batt='6.60' sig='-55' scount='0' rdate='15/05/2015 21:47:04' mdate='18/04/2016 08:06:25' />
            <sensor sid='002EC0FEFF8FFF27' desc='' batt='6.60' sig='-55' scount='0' rdate='15/05/2015 21:47:04' mdate='18/04/2016 08:07:25' />
            <sensor sid='002EC0FEFF8FFF27' desc='' batt='6.60' sig='-55' scount='0' rdate='15/05/2015 21:47:04' mdate='18/04/2016 08:08:25' />
        </sensors>
    </msg>
</msgs>

Once the XML is created, I want to save this file in a folder

L C
  • 438
  • 2
  • 10
  • 28
harsh sharma
  • 75
  • 3
  • 11

1 Answers1

0

Using "JAXB" you can convert java object to xml and then write it to file.

Sample code for setting values to Student object and printing out put to console

JAXBContext context = JAXBContext.newInstance(Student.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

Student object = new Student();
object.setId(11);
object.setName("Bob");
m.marshal(object, System.out);