【问题标题】:how we can extract a value in a tag of a XML file?我们如何在 XML 文件的标签中提取值?
【发布时间】:2014-09-27 20:14:59
【问题描述】:

我想读取 weblogic.xml 并提取上下文根信息。这是一个例子:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE weblogic-web-app PUBLIC "-//BEA Systems, Inc.//DTD Web Application 8.1//EN" "http://www.bea.com/servers/wls810/dtd/weblogic810-web-jar.dtd">
 <weblogic-web-app>
   <context-root>
    /XYZ
   </context-root>
 </weblogic-web-app>

我已经尝试了以下命令

sed -n '/context-root/{s/.*<context-root>//;s/<\/context-root.*//;p;}' weblogic.xml

awk -F "[><]" '/context-root/{print $3}' weblogic.xml

perl -ne 'if (/context-root/){ s/.*?>//; s/<.*//;print;}' weblogic.xml

如果标签是这样的就可以了:

<context-root>/XYZ</context-root>

如何从上面的 xml 中提取标签的值?

【问题讨论】:

    标签: xml unix


    【解决方案1】:
    awk '{ gsub(/^[ \t]+|[ \t\r]+$/, ""); } /<\/context-root>/ { p = 0 }; p; /<context-root>/ { p = 1 }' file
    

    输出:

    /XYZ
    

    更新

    #!/usr/bin/awk -f
    {
        gsub(/^[ \t]+|[ \t\r]+$/, "")
    }
    match($0, /^[^<]*<\/context-root>/) {
        if (p) {
            t = substr($0, 1, index($0, "</context-root>") - 1)
            if (length(t)) print t
        }
        $0 = substr($0, RSTART + RLENGTH)
        p = 0
    }
    {
        while (match($0, /<context-root>[^<]*<\/context-root>/)) {
            t = substr($0, RSTART, RLENGTH)
            gsub(/<\/?context-root>/, "", t)
            print t
            $0 = substr($0, RSTART + RLENGTH)
        } 
    }
    p
    match($0, /<context-root>/) {
        t = substr($0, RSTART + RLENGTH)
        if (length(t)) print t
        p = 1
    }
    

    另一个版本:

    #!/usr/bin/awk -f
    function strip(t) {
        gsub(/^[ \t]+|[ \t\r]+$/, "", t)
        return t
    }
    match($0, /^[^<]*<\/context-root>/) {
        if (p) {
            t = strip(substr($0, 1, index($0, "</context-root>") - 1))
            if (length(t)) print t
        }
        $0 = substr($0, RSTART + RLENGTH)
        p = 0
    }
    {
        while (match($0, /<context-root>[^<]*<\/context-root>/)) {
            t = substr($0, RSTART, RLENGTH)
            gsub(/<\/?context-root>/, "", t)
            if (length(t)) print t
            $0 = substr($0, RSTART + RLENGTH)
        } 
    }
    p {
        print strip($0)
    }
    match($0, /<context-root>/) {
        t = strip(substr($0, RSTART + RLENGTH))
        if (length(t)) print t
        p = 1
    }
    

    输入:

        <context-root>
            A B
        </context-root>
        <context-root>C D</context-root><context-root>E F</context-root><context-root>G H
        I J</context-root>
    

    输出:

    A B
    C D
    E F
    G H
    I J
    

    【讨论】:

    • 感谢此命令有效,但我希望为 &lt;context-root&gt;/XYZ&lt;/context-root&gt;&lt;context-root&gt; /XYZ &lt;/context-root&gt; 等两种类型的标签提供通用解决方案
    • 感谢您的更新。这工作正常并解决了我的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-08
    • 1970-01-01
    • 1970-01-01
    • 2015-08-15
    • 1970-01-01
    • 2011-10-15
    • 1970-01-01
    相关资源
    最近更新 更多