Putting each attribute on a new line during xml serialization

旧巷老猫 提交于 2019-12-31 02:46:24

问题


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.


回答1:


The DecentXML parser can do this.




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/1265255/putting-each-attribute-on-a-new-line-during-xml-serialization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!