shell解析xml文件

Posted peitianwang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

以上是关于shell解析xml文件的主要内容,如果未能解决你的问题,请参考以下文章

无法在 PowerShell 中完全解析 XML

groovy怎么解析xml文件

Java文件解析xml文件转成 map

怎么用c语言解析xml文件

Java DocumentBuilderFactory(javax.xml)通过XPath解析xml文件

Java里面怎么解析xml文件的内容