简体   繁体   中英

Format XML code in Eclipse

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.

Ctrl + Shift + F

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

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

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 .

Macintosh用户的CMD + SHIFT + F

您需要使用XML编辑器打开文件,交换到“源”视图,然后执行Ctrl + Shift + F技巧。

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM