2009-03-02 15 views
12

मेरे पास निम्नलिखित संरचनाओं के साथ जावा क्लास हैं (कक्षा के नाम कुछ भी नहीं दर्शाते हैं, मैं उन्हें बस बना रहा था)।जेएक्सबी एक्सएमएल आउटपुट प्रारूप प्रश्न

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_FORMATTED_OUTPUT को भी सही पर सेट किया है, लेकिन फिर भी अनुचित इंडेंटेशन प्राप्त करें।

धन्यवाद।

+0

क्या आप इसे हल करने में सक्षम थे? मुझे जेएक्सबी का उपयोग करके जेनरेट किए गए मेरे एक्सएमएल में भी यही समस्या है। क्या आप सलाह दे सकते हैं ? –

+0

@ शिवम शेखर, नहीं, समस्या अभी भी वहां है :(लेकिन यह अन्य चीजों की तुलना में कम महत्वपूर्ण है, इसलिए हमने इसे वहां छोड़ दिया – His

उत्तर

9

Indenting सापेक्ष 8 होता है,

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

में आप पाते हैं

int i = depth%8; 
+1

यह समस्या जेएक्सबी (आरआई) के मेट्रो कार्यान्वयन के लिए अद्वितीय है। जेएक्सबी जैसे अन्य कार्यान्वयन चूंकि मोक्सी उस मुद्दे को साझा नहीं करते हैं: http://www.eclipse.org/eclipselink/moxy.php –

1

मुझे नहीं लगता कि एक सीमा है। मैंने बिना किसी कठिनाइयों के बहुत गहरे घोंसले को देखा है। क्या आपके पास जगह पर कोई सफेद जगह नियंत्रण है? इसके अलावा, आपने रेट थ्रेसहोल्ड क्लास की परिभाषा प्रदान नहीं की है, जो अप्रत्याशित आउटपुट बना रहा है।

0

आप लाइन चौड़ाई निर्धारित करने की आवश्यकता - डिफ़ॉल्ट = नए OutputFormat की 72.

OutputFormat() है;

of.setLineWidth (1000);

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); 
} 
संबंधित मुद्दे