【问题标题】:What should I do to make java output the XML?我应该怎么做才能让java输出XML?
【发布时间】:2019-05-21 19:12:01
【问题描述】:

我正在做作业,这里有点麻烦。事情是这样的:我必须制作一个与 Java 端点通信的 C# 程序。我试图设置一个 Java Web 应用程序,用户可以在其中提供一些信息并注册该服务。起初,我的想法是让 Java 应用程序从用户那里生成一个 XML,然后 C# 应用程序可以在需要时下载该文件,然后对其进行调整。

所以我创建了一个类,我们称之为 Guest,带有一些数据字段。然后我为所有注册的客人创建了一个 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;
    }

我的访客容器:

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。

【问题讨论】:

  • 您只会获得文件中的最新访客,正如 Javadoc 所述:XML will be written to this file. If it already exists, it will be overwritten.
  • 如果我将文件添加到项目文件夹中。它仍然是空的。我在每个文件夹中都添加了以确保...它们都没有被覆盖。

标签: java xml servlets jaxb output


【解决方案1】:

您可以将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);
        }
    }
}

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 它会很棒,但我必须在“服务器”上制作文件,就像在我的 Web 项目的根目录中一样。然后,如果 localhost:8080 已打开并正在运行,我可以将该 XML 下载到我的 c# 应用程序中。好吧,Java 正在制作文件。
    • 您可以在 servlet 中动态创建 XML。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多