views:

296

answers:

3

Lets say I have a DOM object (or a string containing xml). Is it in any way possible to serialize the xml in such a way that each attribute appears on a new line?

This is the output I want:

<parent>
    <anElement 
        attrOne="1"
        attrTwo="2"
        attrThree="3"
    />
</parent>

Preferred if the solution a part of the standard java api, but I suspect such a feature is not available in there, or am I wrong?

I found a property for a serializer in the .NET Framework, called NewLineOnAttributes. What I am searching for is something equivalent, but in java.

A: 

The XOM library has a Serializer class which you can override to output in whatever format you want.

Adam Batkin
A: 

I don't know of any XML API for Java that provides that specific ability. I've checked the source code for JDOM and XOM, and they all print attributes on the same line, and provide no specific hooks for overriding that.

Both XOM and JDOM do have specific classes for serializing XML (XMLOutputter and Serializer, respectively), and both classes have protected or public methods for handling the serialization of attributes, so you could, if you wanted to, subclass those classes and override the appropriate methods to control your attribute formatting as you want it.

As for the standard Java API, though, forget it, that stuff is pretty nasty.

skaffman
+1  A: 

The DecentXML parser can do this.

Aaron Digulla
The license for that library says "Neither the name of the <ORGANIZATION> nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission." Does that mean: If company A uses DecentXML in program B, company A is not allowed to market program B as a product supplied by company A? If so, this is definately not an option. (I hate texts related to legal issues. They are major code obfuscations of the English language)
Alderath
As I understand it, it means you can't advertise your product using my name without my consent.
Aaron Digulla
My bad. I skimmed through the document too quickly and incorrectly assumed that <Organization> referred to the company which makes use of the parser. While it actually refers to the organization which supplies the DecentXML library.
Alderath