[英]What should I do to make java output the XML?
我正在做作业,我在这里遇到了一些麻烦。 这是交易:我必须制作一个与Java端点通信的C#程序。 我试图建立一个Java Web应用程序,用户可以在其中提供一些信息并注册该服务。 起初,我的想法是让Java App从用户生成XML,然后C#app可以根据需要下载文件,然后进行调整。
所以我创建了一个类,让我们称之为Guest,带有一些数据域。 然后我为所有已注册的guest虚拟机创建了一个GuestContainer单例类,其中包含一个包含所有guest虚拟机的ArrayList。 如果添加了guest虚拟机,GuestContainer应该从中创建XML文件。 一切似乎都有效,除了Java没有制作任何文件......
import java.io.File;
import java.io.Serializable;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "guest")
public class Guest{
private String name;
private String city;
private String phoneNumber;
@XmlElement(name = "name")
public String getName() {
return name;
}
@XmlElement(name = "city")
public String getCity() {
return city;
}
@XmlElement(name = "phoneNumber")
public String getPhoneNumber() {
return phoneNumber;
}
public Guest(String name, String city, String phoneNumber) {
this.name = name;
this.city = city;
this.phoneNumber = phoneNumber;
}
我的GuestContainer:
package Model;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.*;
import javanet.staxutils.XMLStreamEventWriter;
import javax.xml.bind.*;
public class GuestContainer{
private ArrayList<Guest> guests;
private static GuestContainer container;
private JAXBContext context;
private Marshaller _m;
public static GuestContainer getInstance() throws JAXBException{
if (container == null) {
container = new GuestContainer();
}
return container;
}
private GuestContainer() throws JAXBException{
this.guests = new ArrayList<Guest>();
this.context = JAXBContext.newInstance(Guest.class);
this._m = this.context.createMarshaller();
this._m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
}
public void AddGuest(Guest guest) throws JAXBException,
FileNotFoundException{
guests.add(guest);
MakeFile();
}
private void MakeFile() throws PropertyException, JAXBException,
FileNotFoundException{
for (Guest guest : this.guests) {
///guest.ToXML(file);
_m.marshal(guest, new File("guests.xml"));
}
}
所以我希望得到一个XML输出,其中所有的客户都在那里,但不幸的是,我没有任何XML的错误。
您可以将GuestContainer
XmlRootElement
,然后添加所有Guest
并在GuestContainer
之后编组GuestContainer
:
@XmlRootElement(name = "root")
public class GuestContainer
{
private ArrayList<Guest> guests;
private static GuestContainer container;
@XmlTransient
private JAXBContext context;
@XmlTransient
private Marshaller _m;
public static GuestContainer getInstance() throws JAXBException
{
if (container == null)
{
container = new GuestContainer();
}
return container;
}
private GuestContainer() throws JAXBException
{
this.guests = new ArrayList<Guest>();
this.context = JAXBContext.newInstance(GuestContainer.class);
this._m = this.context.createMarshaller();
this._m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
}
public void addGuest(Guest guest) throws JAXBException, FileNotFoundException
{
guests.add(guest);
}
@XmlElement(name="guest")
public ArrayList<Guest> getGuests()
{
return guests;
}
public void makeFile() throws PropertyException, JAXBException, IOException
{
_m.marshal(this, new File("guests.xml"));
}
public static void main(String[] args)
{
try
{
GuestContainer.getInstance().addGuest(new Guest("testName", "testCity", "testPhone"));
GuestContainer.getInstance().addGuest(new Guest("test2", "testVillage", "testFax"));
GuestContainer.getInstance().addGuest(new Guest("testAbc", "testTown", "testMail"));
GuestContainer.getInstance().makeFile();
}
catch(Exception ex)
{
System.out.println(ex);
}
}
}
如果您只想将元素转储到文件中,请使用FileOutputStream
并附加元素:
private void makeFile() throws PropertyException, JAXBException, IOException
{
try (FileOutputStream out = new FileOutputStream("d:/tmp/guests.xml", true))
{
if(guests.size()==1)
_m.setProperty(Marshaller.JAXB_FRAGMENT, false);
else
_m.setProperty(Marshaller.JAXB_FRAGMENT, true);
_m.marshal(guests.get(guests.size() - 1), out);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.