6

XML の解析とフォーマットに JDom を使用しています。属性の長い行を複数の行に分割したいと考えています。

お気に入り :

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

の中へ :

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

JDom FAQによると、 JDomは標準の DOM および SAX イベントに変換できます。したがって、SAX または DOM をサポートし、このようなきれいなレンダリングが可能なレンダラーはどれも優れています。

前もって感謝します。

4

1 に答える 1

5

わかりました、それを行うクラスは見つかりませんでした。そこで、 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);
            }
    }
}

このシリアライザは、指定された Format のインデント ポリシーに従います。

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

これが誰かに役立つことを願っています。

于 2011-08-05T13:38:13.937 に答える