JAXB XML输出格式问题 [英] JAXB XML output format questions
问题描述
我有以下结构的Java类(类名不暗示任何东西,我只是在制作它们)。
包装测试;
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
公共类测试
{
@XmlAccessorType(XmlAccessType.FIELD)
静态类机器
{
@ XmlElementWrapper(name =servers)
@XmlElement(name =server)
List< Server> servers = new ArrayList< Server>();
}
@XmlAccessorType(XmlAccessType.FIELD)
静态类服务器
{
阈值t =新阈值();
}
@XmlAccessorType(XmlAccessType.FIELD)
静态类阈值
{
RateThreshold load = new RateThreshold();
}
@XmlAccessorType(XmlAccessType.FIELD)
静态类RateThreshold
{
@XmlAccessorType(XmlAccessType.FIELD)
静态类费率
{
int count;
期间=新的期间();
}
@XmlAccessorType(XmlAccessType.FIELD)
私有静态类句点
{
@XmlAttribute
private String type =second ;
@XmlValue
私人浮动期;
}
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);
测试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();
}
}
}
我遇到的问题是在编组Test实例时使用JAXB生成的XML输出。 XML输出将始终如下所示:
<?xml version =1.0encoding =UTF-8独立= 是 >?;
< 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>
如您所见,某些元素没有正确缩进(即最深的元素,数量)和期间)。这是为什么?我创建JAXB上下文的方式有问题吗?或者JAXB递归缩进的元素数量是否有最大限制?我怎么能解决这个问题?请注意,我还将JAXB_FORMATTED_OUTPUT设置为true,但仍然得到不正确的缩进。
谢谢。
com.sun.xml.bind.v2.runtime .output.IndentingUTF8XmlOutput
你找到
int i = depth%8;
I have Java classes with the following structure (the class names do not imply anything, I was just making them up).
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();
}
}
}
The problem I am having is with the XML output generated by JAXB when marshalling a Test instance. The XML output would always look like the following:
<?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>
As you can see, some elements are not being indented properly (that is, the deepest elements, count and period). Why is that? Is there something wrong with the way I created the JAXB context? Or is there a maximum limit to how many elements that can be indented recursively by JAXB? How could I fix this? Note that I have also set JAXB_FORMATTED_OUTPUT to true, but still get the improper indentation.
Thanks.
Indenting occurs modulo 8, in
com.sun.xml.bind.v2.runtime.output.IndentingUTF8XmlOutput
you find
int i = depth%8;
这篇关于JAXB XML输出格式问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!