删除与正则表达式工具XML标记 [英] Remove XML tags with Regex tools

查看:127
本文介绍了删除与正则表达式工具XML标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的XML文件中的一个片段

Here is a snippet of my XML file

<layoutItems>
            <behavior>Edit</behavior>
            <field>ID</field>
</layoutItems>
<layoutItems>
            <page>lastViewedAccount</page>
            <showLabel>false</showLabel>
            <showScrollbars>false</showScrollbars>
            <width>100%</width>
</layoutItems>
<layoutItems>
            <behavior>Required</behavior>
            <field>Name</field>
</layoutItems>

我要删除的部分,中间即

I want to remove the section in the middle i.e

<layoutItems>
            <page>lastViewedAccount</page>
            <showLabel>false</showLabel>
            <showScrollbars>false</showScrollbars>
            <width>100%</width>
</layoutItems>

这部分可以在任何地方的文件中与其他标记一起出现。

This section can appear anywhere inside the file along with other tags.

什么是使用一些字符串操作工具来删除这个的最佳方式?我一直在试图与的sed ,但没有成功,我的运气。任何帮助将是AP preciated。

What is the best way of using some string manipulation tool to remove this? I have been been trying my luck with sed but no success. Any help would be appreciated.

推荐答案

请注意:你应该提供尽可能多的信息,你可以。说到一般解析 HTML ,的 XML ,的 JSON 等用的正则表达式它的不可以是一个好主意,用一个总的 HTML的解析 - 和的 XML解析 - 工具!以下 SED code可以帮助你的平均时间。所以,还请注意:可能的失败与其他文件和其他结构! 不要在生产中不能使用!我假设保修!

Please note: you should provide as much information as you can. Speaking generally parsing of html, xml, json and so on with regex it not a good idea, use always a html-parsing- and xml-parsing -tool! The following sed code may help you in the mean time. And so please also note: it may FAIL with other files and other structures! Do not use in production! I assume NO warranty!

sed -r '/<layoutItems>/{:ka;N;s#(</layoutItems>)#\1#;Tka;s/lastViewedAccount//;T;d}' file 


Inputfile中有2 lastViewedAccount 标签:

    <?xml version="1.0" encoding="UTF-8"?>
    <Layout xmlns="http://test.com/2006/04/metadata">
        <emailDefault>false</emailDefault>
        <headers>PersonalTagging</headers>
        <headers>PublicTagging</headers>
        <layoutSections>
            <customLabel>false</customLabel>
            <detailHeading>false</detailHeading>
            <editHeading>true</editHeading>
            <label>Account Information</label>
            <layoutColumns>
                <layoutItems>
                    <page>lastViewedAccount</page>
                    <showLabel>false</showLabel>
                    <showScrollbars>false</showScrollbars>
                    <width>100%</width>
                </layoutItems>
                <layoutItems>
                    <behavior>Edit</behavior>
                    <field>OwnerId</field>
                </layoutItems>
                <layoutItems>
                    <behavior>Required</behavior>
                    <field>Name</field>
                </layoutItems>
                <layoutItems>
                    <behavior>Edit</behavior>
                    <field>ParentId</field>
                </layoutItems>
                <layoutItems>
                    <behavior>Edit</behavior>
                    <field>AccountNumber</field>
                </layoutItems>
                <layoutItems>
                    <page>lastViewedAccount</page>
                    <showLabel>false</showLabel>
                    <showScrollbars>false</showScrollbars>
                    <width>100%</width>
                </layoutItems>
                <layoutItems>
                    <behavior>Edit</behavior>
                    <field>Site</field>
                </layoutItems>
            </layoutColumns>
      </layoutSections>
    </Layout>


OUTPUTFILE, lastViewedAccount 标签中删除:

    <?xml version="1.0" encoding="UTF-8"?>
    <Layout xmlns="http://test.com/2006/04/metadata">
        <emailDefault>false</emailDefault>
        <headers>PersonalTagging</headers>
        <headers>PublicTagging</headers>
        <layoutSections>
            <customLabel>false</customLabel>
            <detailHeading>false</detailHeading>
            <editHeading>true</editHeading>
            <label>Account Information</label>
            <layoutColumns>
                <layoutItems>
                    <behavior>Edit</behavior>
                    <field>OwnerId</field>
                </layoutItems>
                <layoutItems>
                    <behavior>Required</behavior>
                    <field>Name</field>
                </layoutItems>
                <layoutItems>
                    <behavior>Edit</behavior>
                    <field>ParentId</field>
                </layoutItems>
                <layoutItems>
                    <behavior>Edit</behavior>
                    <field>AccountNumber</field>
                </layoutItems>
                <layoutItems>
                    <behavior>Edit</behavior>
                    <field>Site</field>
                </layoutItems>
            </layoutColumns>
      </layoutSections>
    </Layout>

这篇关于删除与正则表达式工具XML标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆