Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save data in an XML file

Tags:

java

xml

I have an application where in i need to save the data input by a user in a form in an XML file at a specified location and i need to perform this using Java . I am relatively very new to XML handling in java. I would like some suggestions as to how to start the task .

Any code snippets and links will be helpful ...

Thank You

like image 257
Flash Avatar asked Apr 07 '11 12:04

Flash


People also ask

Can we store data in XML?

With XML, the data can be stored in separate XML files. With a few lines of JavaScript code, you can read an XML file and update the data content of any HTML page.

How do I add data to an XML File?

To insert data into an XML column, use the SQL INSERT statement. The input to the XML column must be a well-formed XML document, as defined in the XML 1.0 specification. The application data type can be an XML, character, or binary type.


1 Answers

There is very good framework JAXB for this also there is Simple

But I have used this XStream

Person joe = new Person("Joe", "Walnes");
joe.setPhone(new PhoneNumber(123, "1234-456"));
joe.setFax(new PhoneNumber(123, "9999-999"));

Now, to convert it to XML, all you have to do is make a simple call to XStream:

String xml = xstream.toXML(joe);

The resulting XML looks like this:

<person>
  <firstname>Joe</firstname>
  <lastname>Walnes</lastname>
  <phone>
    <code>123</code>
    <number>1234-456</number>
  </phone>
  <fax>
    <code>123</code>
    <number>9999-999</number>
  </fax>
</person>

Also See

  • JAXB
  • where-i-can-find-a-detailed-comparison-of-java-xml-frameworks
like image 126
jmj Avatar answered Nov 15 '22 18:11

jmj