How to format a XML file with long list of attributes?

Posted on

Problem :

I am currently working with XML files that contain elements with a long list of attributes. When I try to re-indent the whole XML file using xmllint --format myfile.xml only the elements are indented, the attributes still remain in one single line.

Is there some (command line, Windows or GNU/Linux) program that supports re-formatting of XML including the attributes?

For example a XML like:

  <node attribute1="some long attribute value" attribute2="some long attribute value" attribute3="..."/>

should be output similar to:

  <node attribute1="some long attribute value"
        attribute2="some long attribute value"

Solution :

While your well-intentioned search for a “command-line tool” is perfectly fine, unfortunately I think any truly viable solution to this problem is going to involve writing some code yourself, or at least leveraging existing code (such as an XML parser/generator). In the comments, have posted a few things you can try that may or may not get you the exact formatting style you’re looking for. If it’s really close but not quite, your best bet is probably to edit the source code of the program that “almost” gets there. Of course, if you don’t know any programming languages, that can be a bit of an impasse.

I am calling all of these solutions “possible” because, even if they format attributes the way you want them to, they might format something else in a way you don’t want, and then you’re back to square one. Since you didn’t state your full transformation requirements, I can only guess at which of these would be acceptable to you, so you’ll just have to try them one by one until you either stumble upon something that works, or create it yourself by leveraging existing code and writing a small-ish program.

Possible Solutions Without Programming


Command Line

  • XMLTidy (last updated 2005; may be stale)
  • Dom4j (included here because the “programming” is already given to you in its entirety in the SO answer)

Possible Solutions With Programming

  • Write your own custom program using one of the following:
  • Xerces
  • Dom4j
  • Sax
  • libxml
  • The underlying libraries that Eclipse uses to parse XML, which probably would allow you to programmatically format XML and set up the customized formatting rules as you see in the Eclipse options

… which processes the XML in exactly the way you want it to.

See Also this SO question.

Lastly, note that if you want community help about delving head-first into writing a program involving one of these technologies, you should ask the question specific to one chosen XML parser/generator/library on StackOverflow and not on SuperUser. We primarily deal with questions about usage of existing programs, here.

Leave a Reply

Your email address will not be published. Required fields are marked *