zoukankan      html  css  js  c++  java
  • shell解析xml文件

    centos安装xml2

    yum install xml2
    

    测试文件

        <?xml version="1.0" encoding="UTF-8" ?>
        <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
    
        <properties>
            <comment>
                PowerEngine Main Configuration
            </comment>
            <entry key="CurrentMode">Mode.DEV</entry>
    
            <entry key="Mode.DEV">
                classpath:/config/mode/dev/peconfig.xml
            </entry>
    
            <entry key="Mode.TEST">
                classpath:/config/mode/test/peconfig.xml
            </entry>
    
            <entry key="Mode.PRO">
                classpath:/config/mode/pro/peconfig.xml
            </entry>
    
            <entry key="Mode.UAT">
                classpath:/config/mode/uat/peconfig.xml
            </entry>
            <entry key="CommonConfig">
                classpath:/config/pe/*.xml
                classpath:/config/dubbo/*.xml,
                classpath:/config/trs/*.xml,
                classpath:/config/db/*.xml
            </entry>
    

    解析文件并替换内容

    xml2 < test.xml > test.txt
    sed -i 's#/properties/entry=Mode.DEV#/properties/entry=Mode.PRO#g' test.txt
    
    /properties/comment=
    /properties/comment=        PowerEngine Main Configuration
    /properties/comment=
    /properties/entry/@key=CurrentMode
    /properties/entry=Mode.DEV
    /properties/entry
    /properties/entry/@key=Mode.DEV
    /properties/entry=
    /properties/entry=		classpath:/config/mode/dev/peconfig.xml
    /properties/entry=
    /properties/entry
    /properties/entry/@key=Mode.TEST
    /properties/entry=
    /properties/entry=		classpath:/config/mode/test/peconfig.xml
    /properties/entry=
    /properties/entry
    /properties/entry/@key=Mode.PRO
    /properties/entry=
    /properties/entry=		classpath:/config/mode/pro/peconfig.xml
    /properties/entry=
    /properties/entry
    /properties/entry/@key=Mode.UAT
    /properties/entry=
    /properties/entry=		classpath:/config/mode/uat/peconfig.xml
    /properties/entry=
    /properties/entry
    /properties/entry/@key=CommonConfig
    /properties/entry=
    /properties/entry=		classpath:/config/pe/*.xml
    /properties/entry=		classpath:/config/dubbo/*.xml,
    /properties/entry=		classpath:/config/trs/*.xml,
    /properties/entry=		classpath:/config/db/*.xml
    /properties/entry=
    

    恢复为xml格式文件

    echo '<?xml version="1.0" encoding="UTF-8" ?>' > test.xml
    echo '<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">' >> test.xml
    2xml < test.txt >> test.xml
    
  • 相关阅读:
    Maven与Eclipse整合使用
    Maven学习总结(五)——聚合与继承【如果想一次构建多个项目模块,那我们就需要对多个项目模块进行聚合】
    Maven的核心概念:maven仅仅定义了抽象的生命周期,具体的任务都是交由插件完成的
    使用Maven构建项目---【重点需要掌握:Jetty和Maven两种项目骨架】
    Maven项目构建过程练习
    maven编码gbk的不可映射字符”解决办法
    Maven
    Spring Aop的方法执行简单模型
    2016年看过的书
    ExcelReader
  • 原文地址:https://www.cnblogs.com/peitianwang/p/11304598.html
Copyright © 2011-2022 走看看