6

我使用 JDom 进行 XML 解析/格式化。我希望将长行属性分成几行。

像 :

<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);
            }
    }
}

此序列化程序将遵循给定格式的缩进策略。

它允许仅将属性包装应用于单个命名空间(我需要该功能),并且您可以在包装它们之前指定允许的最大内联属性数。

我希望这对某人有用。

于 2011-08-05T13:38:13.937 回答