Format XML code in Eclipse

XmlEclipseFormatting

Xml Problem Overview


When I write .xml files in Eclipse, I often have to manually indent code. 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 can't find formatting settings in Eclipse for XML.

Xml Solutions


Solution 1 - Xml

Ctrl+Shift+F

Will do it for you, and you can configure it in

Window -> Preferences -> XML -> Xml Files -> Editor

Solution 2 - Xml

As stated above, Ctrl+Shift+F will auto-format. However, by default Eclipse will split attributes in a naive way. If you're like me and prefer to have all the attributes on their own lines, go to Window→Preferences→XML→XML Files→Editor and check Split multiple attributes each on a new line.

Solution 3 - Xml

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

Solution 4 - Xml

CMD + SHIFT + F for Macintosh users

Solution 5 - Xml

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

You ll see the lines are aligned properly.

Solution 6 - Xml

You can set the default XML editor to “Java Editor”:

  • Right-click on the XML
  • Open With
  • Other...
  • Java Editor

This applies all java formatting and controls over XML file.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsandaloneView Question on Stackoverflow
Solution 1 - XmlDenis PalnitskyView Answer on Stackoverflow
Solution 2 - XmlPaul GestwickiView Answer on Stackoverflow
Solution 3 - XmlRobert BaldockView Answer on Stackoverflow
Solution 4 - XmlmadokeView Answer on Stackoverflow
Solution 5 - XmlsudhirView Answer on Stackoverflow
Solution 6 - XmlKalyanView Answer on Stackoverflow