【问题标题】:Angular2 typescript - print pretty XMLAngular2 typescript - 打印漂亮的 XML
【发布时间】:2020-04-28 06:42:28
【问题描述】:

我有以下从服务器获取的 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>

美化打印的最好方法是什么?

【问题讨论】:

    标签: javascript xml angular typescript pretty-print


    【解决方案1】:

    您可以为此创建一个自定义管道,在后台使用 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>
    

    【讨论】:

    • 这也对我有用。我使用 Prism.js 来获得语法高亮。
    • 找不到模块'vkbeautify',谁能帮忙
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 2011-05-06
    • 1970-01-01
    • 2012-03-25
    • 2014-11-09
    • 1970-01-01
    相关资源
    最近更新 更多