将数据保存在XML文件中

时间:2022-01-05 09:59:24

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 .

我有一个应用程序,我需要将用户输入的数据保存在指定位置的XML文件中,我需要使用Java执行此操作。我对java中的XML处理相对比较新。我想就如何开始这项任务提出一些建议。

Any code snippets and links will be helpful ...

任何代码片段和链接都会有所帮助......

Thank You

谢谢

4 个解决方案

#1


17  

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

有一个非常好的框架JAXB,这也有简单

But I have used this XStream

但我使用过这个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:

现在,要将其转换为XML,您所要做的就是对XStream进行简单的调用:

String xml = xstream.toXML(joe);

The resulting XML looks like this:

生成的XML如下所示:

<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

另见

#2


1  

There are many open source libraries, but I would simply use JAXB, the standard. Although I have to say the XStream library suggested by other answerers looks very promising, too!

有许多开源库,但我只是使用标准的JAXB。虽然我不得不说其他回答者建议的XStream库看起来也很有前途!

#3


1  

I would start by looking at the XStream library. It's very simple to convert POJOs (plain old java objects) to and from XML. I have a blog post detailing some of the gotchas.

我将从查看XStream库开始。将POJO(普通的旧Java对象)与XML进行转换非常简单。我有一篇博文,详细介绍了一些问题。

#4


0  

Consider using xstream (http://x-stream.github.io/). XStream's API is very simple:

考虑使用xstream(http://x-stream.github.io/)。 XStream的API非常简单:

YourObjectGraph yourData=buildYourData();
XStream xstream=new XStream();
String yourXML=xstream.toXml(yourData);
// do something with your shiny XML

Importing is just as easy:

导入同样简单:

YourObjectGraph yourData=(YourObjectGraph)xstream.fromXml(yourXml);

#1


17  

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

有一个非常好的框架JAXB,这也有简单

But I have used this XStream

但我使用过这个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:

现在,要将其转换为XML,您所要做的就是对XStream进行简单的调用:

String xml = xstream.toXML(joe);

The resulting XML looks like this:

生成的XML如下所示:

<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

另见

#2


1  

There are many open source libraries, but I would simply use JAXB, the standard. Although I have to say the XStream library suggested by other answerers looks very promising, too!

有许多开源库,但我只是使用标准的JAXB。虽然我不得不说其他回答者建议的XStream库看起来也很有前途!

#3


1  

I would start by looking at the XStream library. It's very simple to convert POJOs (plain old java objects) to and from XML. I have a blog post detailing some of the gotchas.

我将从查看XStream库开始。将POJO(普通的旧Java对象)与XML进行转换非常简单。我有一篇博文,详细介绍了一些问题。

#4


0  

Consider using xstream (http://x-stream.github.io/). XStream's API is very simple:

考虑使用xstream(http://x-stream.github.io/)。 XStream的API非常简单:

YourObjectGraph yourData=buildYourData();
XStream xstream=new XStream();
String yourXML=xstream.toXml(yourData);
// do something with your shiny XML

Importing is just as easy:

导入同样简单:

YourObjectGraph yourData=(YourObjectGraph)xstream.fromXml(yourXml);