我从服务器获得了以下 XML 字符串:
<find-item-command xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" find-method="Criteria" item-class="com" only-id="false" xsi:schemaLocation=""> <criteria> <criterion> <descriptors> <descriptor>DPSystemEventItem</descriptor> </descriptors> <value>cluster.manager.active</value> </criterion> </criteria> </find-item-command>
但是我想在我的模块中美化它:
<find-item-command
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" find-method="Criteria" item-class="com" only-id="false" xsi:schemaLocation="">
<criteria>
<criterion>
<descriptors>
<descriptor>DPSystemEventItem</descriptor>
</descriptors>
<value>cluster.manager.active</value>
</criterion>
</criteria>
</find-item-command>
美化它的最佳打印方式是什么?
最佳答案
您可以为此创建一个使用 vkbeautify 的自定义管道在幕后。
npm install -S vkbeautify
自定义 xml
管道示例:
import * as vkbeautify from 'vkbeautify';
import { Pipe, PipeTransform } from "@angular/core";
@Pipe({
name: 'xml'
})
export class XmlPipe implements PipeTransform {
transform(value: string): string {
return vkbeautify.xml(value);
}
}
在您的 app.module 中声明自定义管道,例如:
import { AppComponent } from './app.component';
import { XmlPipe } from '...';
@NgModule({
declarations: [
AppComponent,
...,
...,
XmlPipe
],
...
然后您可以像这样在模板中使用自定义管道:
<pre>{{xmlString | xml}}</pre>
关于javascript - Angular2 typescript - 打印漂亮的 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42268268/