Format XML in Eclipse

When I write .xml files in Eclipse, I often have to specify indented code manually. for example

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:background="@color/background" android:layout_height="fill_parent" android:layout_width="fill_parent" /> 

After formatting, it looks like this.

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:background="@color/background" android:layout_height="fill_parent" android:layout_width="fill_parent" /> 

How to automate this? I cannot find formatting settings in Eclipse for XML.

+59
eclipse xml formatting
Dec 24 '10 at 17:11
source share
7 answers

Ctrl + Shift + F

Do it for yourself and you can customize it in

 Window -> Preferences -> XML -> Xml Files -> Editor 
+100
Dec 24 '10 at 17:20
source share

As stated above, Ctrl + Shift + F will be automatically formatted. However, by default, Eclipse will split attributes in a naive way. If you are like me and prefer to have all the attributes in your own lines, go to Window & rightarrow: Preferences & rightarrow; XML and rightarrow; XML files and rightarrow; Editor and validation. Separate several attributes on each new line.

+37
Mar 13 '11 at 17:35
source share

CMD + SHIFT + F for Macintosh Users

+14
Apr 15 '12 at 19:35
source share

You need to open the file using the XML editor, go to the Source view, and then do the Ctrl + Shift + F trick.

+10
Aug 02 '11 at 10:50
source share

open windows-> settings-> xml files-> editor, enter some number for the line width , say 999, and then format the xml file with Ctrl + shift + F.

You will see that the lines are correctly aligned.

+2
Jul 27 '15 at 4:52
source share

As stated above, Ctrl + Shift + F will be automatically formatted in Eclipse or STS only if we check the separation of several attributes on each new line. To check this, go to Window -> Preferences -> XML -> XML Files -> Editor.

-one
Oct 24 '17 at 19:10
source share

You can set the default XML editor in the Java Editor:

  • Right click on XML
  • Open with
  • Others ...
  • Java editor

This applies to all java formatting and control over the XML file.

-one
Jan 29 '18 at 19:56
source share