用Java格式化XML文件

时间:2015-07-27 11:19:05

标签: java xml jaxb

美好的一天! 我无法弄清楚如何制作正确的XML文件。

以下是我尝试过的代码:

var EventItem = {
            EventTypeId: $scope.eventTypeId,
            StartDate: $scope.startDate,
            EndDate: $scope.EndDate,
            Description: $scope.EventName
        };

产品下的ID和许可证是否必须按照以下方式对齐:

package com.ems.business.model;

import java.util.Date;

import javax.xml.bind.annotation.XmlRootElement;

//import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.annotate.JsonProperty;


@XmlRootElement(name = "EventsMaster")
//@JsonIgnoreProperties(ignoreUnknown = true)
public class EventsMstr implements java.io.Serializable  {

    private static final long serialVersionUID = 1L;

    private Long id;
    private Long eventTypeId;
    private Date startDate;
    private Date endDate;
    private String description;

    public EventsMstr() {
    }
        public EventsMstr(Long id, Long eventTypeId, Date startDate, Date endDate,
            String description) {

        this.id = id;
        this.eventTypeId = eventTypeId;
        this.startDate = startDate;
        this.endDate = endDate;
        this.description = description;
    }

    @JsonProperty("ID")
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @JsonProperty("EventTypeId")
    public Long getEventTypeId() {
        return eventTypeId;
    }

    public void setEventTypeId(Long eventTypeId) {
        this.eventTypeId = eventTypeId;
    }

    @JsonProperty("StartDate")
    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    @JsonProperty("EndDate")
    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    @JsonProperty("Description")
    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public String toString() {
        return "EventsMstr [id=" + id + ", startDate=" + startDate
                + ", endDate=" + endDate + ", description=" + description
                + "]";
    }
}

如果是的话...怎么做?

1 个答案:

答案 0 :(得分:1)

您可以使用此函数格式化任何XML字符串。试试这个。使用您的逻辑创建一个原始xml并将其传递给此函数,它将处理缩进和所有。

    public static String formatXMLString(String unformattedXml) throws Exception {
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(new InputSource(new StringReader(unformattedXml)));
        OutputFormat format = new OutputFormat(document);
        format.setLineWidth(65);
        format.setIndenting(true);
        format.setOmitXMLDeclaration(true);
        format.setIndent(2);
        Writer out = new StringWriter();
        XMLSerializer serializer = new XMLSerializer(out, format);
        serializer.serialize(document);
        return out.toString();
    } catch (IOException e) {
        throw e;
    }
}

<强>更新

我们可以使用JAXB取消对Object的取消并获得格式化的XML。

public static void main(String[] args) throws Exception {
    StringWriter writer = new StringWriter();
    Project project = new Project("1", "POP");
    JAXBContext contect = JAXBContext.newInstance(project.getClass());
    Marshaller marshaller = contect.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    marshaller.marshal(project, writer);
    System.out.println( writer.toString());

}