0

I have XML file like this

<?xml version="1.0" encoding="UTF-8" standalone="no"?>    
<linearlayout layout_height="match_parent" layout_width="match_parent" orientation="vertical">
  <textview layout_height="wrap_content" layout_width="match_parent" text="Hello XML!"/>
  <button layout_height="wrap_content" layout_width="wrap_content" text="Click Me"/>
</linearlayout>

and i want each attribute of element be on it's own line like this

<?xml version="1.0" encoding="UTF-8" standalone="no"?>    
<linearlayout 
  layout_height="match_parent" 
  layout_width="match_parent" 
  orientation="vertical">
  <textview 
    layout_height="wrap_content" 
    layout_width="match_parent" 
    text="Hello XML!"/>
  <button 
    layout_height="wrap_content" 
    layout_width="wrap_content" 
    text="Click Me"/>
</linearlayout>

how can i do this using java ?

Hatem
  • 11
  • 5

2 Answers2

0

press cltl+shift+alt+l in xml and select rearrange from the popup which opens.

Asad Ali
  • 309
  • 3
  • 14
0

The JDOM Java library provides a formatter to pretty print an XML docment. Just read in an XML source then output the reformated contents.

SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(new File(...));

XMLOutputter xo = new XMLOutputter(Format.getPrettyFormat());
String xmlPrettyContent = xo.outputString(doc);
System.out.println(xmlPrettyContent);

You can get an instance to the Formatter and tweak the settings to customize the output as needed.

CodeMonkey
  • 22,825
  • 4
  • 35
  • 75