【问题标题】:Change tabs to spaces in XML file将制表符更改为 XML 文件中的空格
【发布时间】:2017-07-31 09:44:06
【问题描述】:

在创建了许多文件之后,我将 Eclipse 配置更改为 use spaces instead of tabs。它有效:我写的新东西使用空格。但是我昨天写的文件当然还有标签。我想将它们从制表符转换为空格。

对于 Java 文件很简单:打开文件,按 CTRL+a,按 CTRL+i,完成。

但是对于 XML 文件该怎么做呢?与 Java 不同,对于 XML 文件,CTRL+a/CTRL+i 不仅可以转换制表符,还可以尝试“改进格式”,这会破坏事情:

初始代码:

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'>
<!--
    Licensed to the Apache Software Foundation (ASF) under one or more
    contributor license agreements.  See the NOTICE file distributed with
    this work for additional information regarding copyright ownership.
    The ASF licenses this file to You under the Apache License, Version 2.0
    (the "License"); you may not use this file except in compliance with
    the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
-->
<beans>
    <bean id="webscript.helloworld.get"
          class="com.example.HelloWorldWebScript"
          parent="webscript">
    </bean>
</beans>

在 CTRL+a/CTRL+i(或 CTRL+SHIFT+f)之后:

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'>
<!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor 
 license agreements. See the NOTICE file distributed with this work for additional 
 information regarding copyright ownership. The ASF licenses this file to 
 You under the Apache License, Version 2.0 (the "License"); you may not use 
 this file except in compliance with the License. You may obtain a copy of 
 the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required 
 by applicable law or agreed to in writing, software distributed under the 
 License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 
 OF ANY KIND, either express or implied. See the License for the specific 
 language governing permissions and limitations under the License. -->
<beans>
 <bean id="webscript.helloworld.get" class="com.example.HelloWorldWebScript"
  parent="webscript">
 </bean>
</beans>

问题:对于Eclipse中的一个XML文件,如何将制表符转换为空格,不做其他改动?

【问题讨论】:

  • 暂时无法检查,这是一个旧帖子,所以不确定这是否仍然有效,但 Format XML code in Eclipse 或更手动的解决方案 eclipse xml editor indent using spaces
  • @AxelH:不重复。 stackoverflow.com/questions/31747059/… 询问如何更改设置,我已经完成了。 stackoverflow.com/q/4527474/4391450 只有 Ctrl+Shift+F 答案,并且如您所知,Ctrl+Shift+F 会破坏所有格式(只需尝试上面的示例代码)。请重新阅读我的问题:我已经将 Eclipse 设置为使用空格而不是制表符,并且我想要一个仅修复间距而不进行任何其他格式化的命令。
  • 我的错,误读了您使用的快捷方式。我看到两个解决方案,使用脚本或尝试在 Eclipse 中创建新格式,这只会编辑选项卡,禁用其他所有内容。我无法测试,因为不幸的是办公室不使用 eclipse,所以不确定是否可以禁用每个格式选项。您在示例中的问题是列长度属性,设置一个巨大的值来修复它

标签: xml eclipse


【解决方案1】:

你的方法是对的,但首先,你必须自定义 XML 格式:

  • 打开Window &gt; Preferences &gt; XML &gt; XML files &gt; Editor
  • 选中format comments 并取消选中Join lines。 (这应该缩进 cmets 并删除相邻的空格等,保留现有的换行符。但如果您对结果感到不舒服,请取消选中 format comments。)
  • 选择Indent using spaces并设置indentation size

现在您可以按[SHIFT][CTRL][F] 来格式化您的 XML 文件。

【讨论】:

  • 我添加了一个截图,配置显示你的意思吗?不幸的是,除了修复缩进之外,它还连接 XML 属性行,例如在我的示例中,idclass 位于同一行。虽然我可以检查Split multiple attributes each on a new line,但在我的一些具有许多短属性的文件中,我不希望这样。我希望 Eclipse 有一个 XML 格式化程序,其行为类似于 Java 格式化程序:修复缩进而不做其他任何事情。无论如何,谢谢,这可能是最接近我梦想的格式化程序!
  • 是的,我就是这个意思。再次是的:恐怕这是你能得到的最接近的。 Eclipse 格式化程序都不是完美的! :-(
【解决方案2】:

好吧,如果您是手动操作,并且不想要自动脚本,只需在您的 XML 文件中编写一个选项卡,复制它然后 ctrl + f > 替换,将选项卡粘贴到查找内容和替换为的空间中,然后全部替换。那是如果您不想用脚本替换它。

【讨论】:

  • 这确实是一个选项。我仍然希望有一个更简单的捷径,但如果没有更好的捷径我会接受。
  • 好吧,或者,你可以找到一个脚本,我的意思是应该有很多脚本可以做到这一点,因为我认为它并不难制作,它只需要上传并通过 PHP 端下载,至少我只知道如何通过 PHP 来制作它,哈哈,或者应该有使用 .bat 的脚本,一些 ++ 编码器可能已经写好了。 :)
【解决方案3】:

Search -> Search... (或 Ctrl+H) -> File Search,

检查Regular expression

\t设置为Containing text:

*.xml设置为File name patterns

点击Replace...,设置 (根据需要放置空格)-> OK

完成。享受。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-30
    • 1970-01-01
    相关资源
    最近更新 更多