(Node.js/Java)如何使XML输出美观

(Node.js/Java)如何使XML输出美观,java,node.js,xml,xml-parsing,pretty-print,Java,Node.js,Xml,Xml Parsing,Pretty Print,我一直在尝试用XML使输出变得漂亮,但无法做到这一点。我希望在node.js和java(android)中都能做到这一点 我的XML输入是: <custom.TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" tools:layout_editor_

我一直在尝试用XML使输出变得漂亮,但无法做到这一点。我希望在node.js和java(android)中都能做到这一点

我的XML输入是:

  <custom.TextView 
  android:id="@+id/textView2" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:text="TextView" tools:layout_editor_absoluteX="164dp" /> 

解析此代码后,输出为

<custom.TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content"  android:text="TextView" tools:layout_editor_absoluteX="164dp" /> 
 <custom.TextView 
  android:id="@+id/textView2" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:text="TextView" tools:layout_editor_absoluteX="164dp" /> 

而我的预期输出是

<custom.TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content"  android:text="TextView" tools:layout_editor_absoluteX="164dp" /> 
 <custom.TextView 
  android:id="@+id/textView2" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:text="TextView" tools:layout_editor_absoluteX="164dp" /> 


我该怎么做请帮帮我

虽然在这个场景中使用的方法不是很好,但您可以尝试使用这段代码获得漂亮的输出,在下面的代码中,您可以提供xml,然后它会根据您的需要对其进行美化

    var arrayEml= xml = '';

    for (var i = 0; i < xml.length; i++) {
        if ((xml[i].match(/[a-zA-Z]/)) && (xml[i + 1] == ' ') && (xml[i + 2].match(/[a-zA-Z]/))) {
            arrayEml += (xml[i] + '\n');
        } else {
            arrayEml += xml[i];
        }
        // }
    }
    var againParsing = ' ';
    for (var i = 0; i < arrayEml.length; i++) {

        if ((xml[i] = '"') && (xml[i + 1] == ' ') && (xml[i + 2].match(/[a-zA-Z]/))) {
            againParsing += (xml[i] + '\n' + "\t   ");
        } else {
            againParsing += xml[i];
        }
    }
var arrayEml=xml='';
for(var i=0;i
这有点不清楚。您如何生成此输出?您自己打印吗?它是否在编辑器中,并且您希望对其进行格式化?很少有XML序列化程序按照您显示的方式垂直堆叠属性。在Saxon中有一个是这样做的,尽管(a)它并没有完全按照您希望的方式进行,以及(b)它在您选择的平台上不可用。我想如果您真的想要这个布局,您可能必须自己编写。我已经试了两周了,基本上我在node.js中使用xmldom解析xml,解析完成了,但所有标记的格式都更改了,我如何保留格式,这是我的问题非常感谢您,完全正确解决了我的问题,非常感谢:)