XML format, one attribute per line, with JDom

I am using JDom for parsing / formatting XML. I would like long attribute strings to be split into multiple lines.

Like:

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

IN:

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

According to the JDom FAQ , JDom can be converted to standard DOM and SAX events. Thus, any renderer that supports SAX or DOM and is capable of such beautiful rendering will be great.

Thanks in advance.

+6
source share
1 answer

Well, I did not find the class that did this. Therefore, I myself implemented one of the subclasses of 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); } } } 

This serializer will follow the indentation policy for this format.

This allows you to apply attribute binding to only one namespace (I need this function), and you can specify the maximum number of built-in attributes that you allow before they are wrapped.

I hope this can be helpful to someone.

+5
source

Source: https://habr.com/ru/post/894172/


All Articles