12

我有具有以下结构的 Java 类(类名没有任何含义,我只是在编造它们)。

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();
        }
    }
}

我遇到的问题是在编组测试实例时 JAXB 生成的 XML 输出。XML 输出将始终如下所示:

<?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>

如您所见,某些元素没有正确缩进(即最深的元素、计数和周期)。这是为什么?我创建 JAXB 上下文的方式有问题吗?或者 JAXB 可以递归缩进的元素数量是否有最大限制?我该如何解决这个问题?请注意,我还将 JAXB_FORMATTED_OUTPUT 设置为 true,但仍然得到不正确的缩进。

谢谢。

4

4 回答 4

9

缩进以 8 为模,在

com.sun.xml.bind.v2.runtime.output.IndentingUTF8XmlOutput

你发现

int i = depth%8;
于 2010-07-19T10:10:15.063 回答
2

编组器方法的重载之一marshal()接受 XMLStreamWriter,因此您可以通过编写自己的格式化 XML 流编写器来绕过 JAXB 参考实现的脑残格式化机制。你最终会做这样的事情:

public static void SaveContainer( Container container, OutputStream stream ) throws ...
{
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    XMLStreamWriter writer = factory.createXMLStreamWriter( stream, "UTF-8" );
    writer = new MyAwesomeCoolFormattingXMLStreamWriter( writer );
    marshaller.marshal( container, writer );
}
于 2013-07-12T10:52:16.483 回答
1

我不认为有限制。我见过非常深的嵌套,没有任何困难。你有任何空白控制吗?此外,您还没有提供 RateThreshold 类的定义,它是创建意外输出的类。

于 2009-11-09T16:25:15.363 回答
-1

您需要设置线宽 - 默认为 72。

OutputFormat of = new OutputFormat();

of.setLineWidth(1000);

于 2013-06-20T11:21:38.527 回答