2011-08-03 2 views
5

XML解析/書式設定にJDomを使用します。 長い行の属性を複数の行に分割したいと思います。フォーマットXML、1行あたり1属性、JDom付き

同様:中へ

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

JDom FAQによると

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

、JDomには、標準のDOMとSAXのイベントに変換することができます。したがって、SAXやDOMをサポートしていて、レンダリングが可能なレンダラなら、素晴らしいレンダリングが可能です。

ありがとうございます。

答えて

4

私はそれをしたクラスは見つかりませんでした。 は、だから私は、与えられたフォーマットのインデントポリシーに従います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); 
      } 
    } 
} 

このシリアライザのサブクラスとして1を自分で実装しました。

属性ラッピングを単一の名前空間にのみ適用することができます(この機能が必要でした)。ラップする前に許可するインライン属性の最大数を指定できます。

私はこれが誰かにとって有用であると願っています。

+0

+1、自分のアウトプットターを書くことも私が選んだ方法です。 JDomは、リファクタリングを必要としていることが少しわかりました。時間があるなら、あなたはそれに貢献するかもしれません - SFのcommiter privを求める。 –

関連する問題