문제

나는 다음 구조를 가진 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로 설정했지만 여전히 부적절한 압입을 얻습니다.

감사.

도움이 되었습니까?

해결책

들여 쓰기는 모듈로 8, in

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

당신은 찾았습니다

int i = depth%8;

다른 팁

과부하 중 하나 marshal() Marshaler의 방법은 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 );
}

한계가 있다고 생각하지 않습니다. 나는 어려움없이 매우 깊은 둥지를 보았다. 공백 컨트롤이 제자리에 있습니까? 또한, 당신은 RateThreshold 클래스의 정의를 제공하지 않았으며,이 클래스는 예기치 않은 출력을 만드는 것입니다.

선 너비를 설정해야합니다 - 기본값은 72입니다.

outputformat의 = new outputformat ();

of.setlinewidth (1000);

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top