Tôi có các lớp Java có cấu trúc sau (tên lớp không ngụ ý bất cứ điều gì, tôi chỉ đang tạo chúng).Câu hỏi về định dạng đầu ra XML JAXB
package test;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlValue;
@XmlRootElement
public class Test
{
@XmlAccessorType(XmlAccessType.FIELD)
static class Machine
{
@XmlElementWrapper(name="servers")
@XmlElement(name="server")
List<Server> servers = new ArrayList<Server>();
}
@XmlAccessorType(XmlAccessType.FIELD)
static class Server
{
Threshold t = new Threshold();
}
@XmlAccessorType(XmlAccessType.FIELD)
static class Threshold
{
RateThreshold load = new RateThreshold();
}
@XmlAccessorType(XmlAccessType.FIELD)
static class RateThreshold
{
@XmlAccessorType(XmlAccessType.FIELD)
static class Rate
{
int count;
Period period = new Period();
}
@XmlAccessorType(XmlAccessType.FIELD)
private static class Period
{
@XmlAttribute
private String type = "second";
@XmlValue
private float period;
}
Rate min = new Rate();
Rate max = new Rate();
}
@XmlElementWrapper(name="machines")
@XmlElement(name="machine")
List<Machine> machines = new ArrayList<Machine>();
public static void main(String[] args)
{
Machine m = new Machine();
Server s = new Server();
s.t.load.max.count = 10;
s.t.load.min.count = 1;
m.servers.add(s);
Test t = new Test();
t.machines.add(m);
JAXBContext jaxbContext;
Marshaller marshaller;
try
{
jaxbContext = JAXBContext.newInstance(Test.class);
marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(t, System.out);
}
catch (JAXBException e)
{
e.printStackTrace();
}
}
}
Vấn đề tôi gặp phải là với kết quả đầu ra XML do JAXB tạo ra khi marshalling một thể hiện Test. Đầu ra XML sẽ luôn trông giống như sau:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<test>
<machines>
<machine>
<servers>
<server>
<t>
<load>
<min>
<count>1</count>
<period type="second">0.0</period>
</min>
<max>
<count>10</count>
<period type="second">0.0</period>
</max>
</load>
</t>
</server>
</servers>
</machine>
</machines>
</test>
Như bạn có thể thấy, một số phần tử không được thụt lề đúng cách (có nghĩa là, các phần tử sâu nhất, đếm và khoảng thời gian). Tại sao vậy? Có điều gì sai trái với cách tôi tạo bối cảnh JAXB không? Hoặc có giới hạn tối đa đối với bao nhiêu phần tử có thể được thụt lề bởi JAXB không? Làm thế nào tôi có thể sửa lỗi này? Lưu ý rằng tôi cũng đã đặt JAXB_FORMATTED_OUTPUT thành true, nhưng vẫn nhận được sự thụt lề không đúng.
Cảm ơn.
Bạn có thể giải quyết vấn đề này không? Tôi cũng có cùng một vấn đề trong XML của tôi được tạo ra bằng cách sử dụng JAXB. Bạn có thể vui lòng tư vấn? –
@ shivam-shekhar, nope, vấn đề vẫn còn đó: (nhưng nó ít quan trọng hơn so với những thứ khác, vì vậy chúng tôi chỉ để nó ở đó – His