【问题标题】:Cannot get value from xml tag using Powershell无法使用 Powershell 从 xml 标签中获取值
【发布时间】:2014-07-27 01:22:11
【问题描述】:

我有一个这样的 xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<settings>
    <serviceUrlFiles>
        <file>Licensed/Mobile/android/Phone/res/xml/preferences.xml</file>
        <file>Licensed/Mobile/iOS/FIService/Resources/Settings.bundle/Root.plist</file>
        <file>Licensed/Mobile/javascript/src/shared/config/settings.json</file>
            <file>Framework/Mobile/iOS/FIToolkit/FIToolkit/NativeSOAPService/MCProcessSOAPRequest.m</file>
    </serviceUrlFiles>
    <builds>
        <build type="internal.qa">
            <serviceUrl regexp="[a-zA-Z0-9_\-\.]*://[a-zA-Z0-9_\.-]*/RichChannel[0-9a-zA-Z]*/Service.svc">https://10.41.69.77/RichchannelWS/service.svc</serviceUrl>
        </build>
        <build type="client.qa">
            <serviceUrl regexp="[a-zA-Z0-9_\-\.]*://[a-zA-Z0-9_\.-]*/RichChannel[0-9a-zA-Z]*/Service.svc">https://10.41.69.77/RichchannelWS/service.svc</serviceUrl>
        </build>
        <build type="stage">
            <serviceUrl regexp="[a-zA-Z0-9_\-\.]*://[a-zA-Z0-9_\.-]*/RichChannel[0-9a-zA-Z]*/Service.svc">https://10.41.69.77/RichchannelWS/service.svc</serviceUrl>
        </build>
        <build type="release">
            <serviceUrl regexp="[a-zA-Z0-9_\-\.]*://[a-zA-Z0-9_\.-]*/RichChannel[0-9a-zA-Z]*/Service.svc">https://10.41.69.77/RichchannelWS/service.svc</serviceUrl>
        </build>
    </builds>
</settings>

我想做的是使用powershell获取build type="stage"的serviceUrl的内容。

我写了一段powershell代码来做到这一点:

$apktype = $args[0]
$xml = [xml](Get-Content .\Licensed\Mobile\JenkinsBuildScripts\prebuildsetting_url_ver.xml)
$newUrl = $xml.settings.builds.build | ? {$_.type -eq $apktype} | select serviceUrl

然后,我在powershell中运行命令并传递参数:

test.ps1 stage

这只是一个简单的代码。但是,当我尝试回显 $newUrl 以查看其值时,它不会返回 type="stage" 的 build 标记下的 serviceUrl 的值。

有人有什么想法吗?我在互联网上阅读了很多示例,但看不出有什么不同。

提前致谢。

【问题讨论】:

    标签: xml powershell xml-parsing powershell-2.0


    【解决方案1】:

    我相信这就是您正在寻找的:

    $newUrl = ($xml.settings.builds.build | ? {$_.type -eq $apktype} |select -expand serviceUrl).innerText
    

    即使在您获得&lt;serviceUrl&gt; 节点之后,您真正想要的还是该节点之外的innerText。

    还请记住,变量 $newUrl 在 test.ps1 脚本之外将不可用。您可以确定变量的范围,也可以使用“点源”运算符运行脚本:

    . test.ps1 stage
    

    注意前面的.。

    【讨论】:

    • 太棒了,它有效。非常感谢。我还找到了另一种方法: $build = $xml.settings.builds.build | ? {$_.type -eq $apktype} $serviceUrl = $build.serviceUrl.'#text'
    【解决方案2】:

    我建议改用Select-Xml:

    function Get-ServiceUrl {
    param (
        [string]$BuildType = 'stage',
        [string]$Path = '.\Licensed\Mobile\JenkinsBuildScripts\prebuildsetting_url_ver.xml'
    )
    
        $Nodes = Select-Xml -Path $Path -XPath "//build[@type = '$BuildType']/serviceUrl"
        foreach ($Node in $Nodes) {
            $Node.Node.InnerText
        }
    
    }
    
    Get-ServiceUrl
    Get-ServiceUrl -BuildType release
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多