2011-08-03 9 views
5

XML ayrıştırma/biçimlendirme için JDom kullanıyorum. Uzun satırların çeşitli satırlara bölünmesini istiyorum.Format XML, satır başına bir özellik, JDom ile

gibi: Into the

<node att1="Foo" att2="Bar" att3="Foo" /> 

: JDom FAQ göre

<node 
    att1="Foo" 
    att2="Bar" 
    att3="Foo" /> 

, JDom standart DOM ve SAX olayları dönüştürülebilir. Yani SAX veya DOM'ı destekleyen ve bu kadar güzel oluşturma yeteneğine sahip herhangi bir renderer harika olurdu.

Şimdiden teşekkürler.

cevap

4

Tamam, Bunu yapan bir sınıf bulamadım. Yani verilen Format girinti politikasını takip edecek org.jdom.output.XMLOutputter

import java.io.IOException; 
import java.io.Writer; 
import java.util.*; 

import org.jdom.Attribute; 
import org.jdom.Element; 
import org.jdom.output.XMLOutputter; 


/** This outputter prints each attributes in a new line */ 
public class OneAttributePerLineOutputter extends XMLOutputter { 

    // ---------------------------------------------------- 
    // Attribute 
    // ---------------------------------------------------- 

    /** Limit wrapping attribute for one namespace */ 
    String namespace = null; 

    /** Number of inline attributes before wrapping */ 
    private int nbInlineAttribs; 

    // ---------------------------------------------------- 
    // Constructor 
    // ---------------------------------------------------- 

    /** 
    * @param namespace Limit wrapping attributes to one namespace. If null, all attributes are concerned 
    * @param nbInlineAttribs Allow a given number of inline elements before wrapping to several lines 
    */ 
    public OneAttributePerLineOutputter(
      String namespace, 
      int nbInlineAttribs) 
    { 
     this.namespace = namespace; 
     this.nbInlineAttribs = nbInlineAttribs; 
    } 

    // ---------------------------------------------------- 
    // Helpers 
    // ---------------------------------------------------- 

    static private int elementDepth(Element element) { 
     int result = 0; 
     while(element != null) { 
      result++; 
      element = element.getParentElement(); 
     } 
     return result; 
    } 

    // ---------------------------------------------------- 
    // Overridden methods 
    // ---------------------------------------------------- 

    @Override protected void printAttributes(
      Writer writer, 
      List attribs, 
      Element parent, 
      NamespaceStack ns) throws IOException 
    {  
        // Loop on attributes 
      for (Object attribObj : attribs) { 

       Attribute attrib = (Attribute) attribObj; 

       // Check namespace 
       if ((this.namespace == null) || 
        (this.namespace.equals(attrib.getNamespaceURI()))) 
       { 
        // Reached max number of inline attribs ? 
        if (attribs.size() > this.nbInlineAttribs) { 

         // New line 
         writer.append("\n"); 

         // Indent 
         for (int i=0; i < elementDepth(parent); i++) { 
          writer.append(this.getFormat().getIndent()); 
         } 
        } 
       } 

       // Output single atribute 
       List list = new ArrayList<Object>(); 
       list.add(attrib); 
       super.printAttributes(writer, list, parent, ns); 
      } 
    } 
} 

Bu serileştirici bir alt sınıfında gibi bir kendim uygulamıştır.

Bu özellik, yalnızca bir ad alanına (bu özelliğe ihtiyacım vardı) özellik sarma uygulamasına izin verir ve bunları sarmadan önce izin verdiğiniz satır içi niteliklerin maksimum sayısını belirtebilirsiniz.

Umarım bu birileri için yararlı olabilir.

+0

+1, kendi çıkış yazıcım da benim seçtiğim yoldur. Ve JDom'u biraz, refactoring ihtiyacında bulduk. Vaktiniz varsa, buna katkıda bulunabilirsiniz - SF'de taahhüt sahibi için özel isteyin. –

İlgili konular