我应该怎么做才能使java输出XML?

时间:2019-05-21 19:12:01

标签: java xml servlets jaxb output

我正在做作业,在这里遇到了一些麻烦。达成协议:我必须制作一个与Java端点通信的C#程序。我试图建立一个Java Web应用程序,用户可以在其中提供一些信息并注册该服务。最初,我的想法是让Java App从用户生成XML,然后C#App可以根据需要下载文件,然后对其进行调整。

所以我做了一个让我们称之为Class的Class,它带有一些数据字段。然后,我为所有注册的来宾创建了一个GuestContainer单例类,其中包含一个ArrayList,其中包含所有来宾。如果添加了来宾,则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的错误。

2 个答案:

答案 0 :(得分:0)

您可以将GuestContainer设为XmlRootElement,然后添加所有Guest并编组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);
        }
    }
}

答案 1 :(得分:0)

如果只想将元素转储到文件中,请使用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);
    }
}