Long Dao
Long Dao

Reputation: 1371

Cannot get value from xml tag using Powershell

I have a xml file like that:

<?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>

What I want to do is to use powershell to get the content of the serviceUrl where build type="stage".

I wrote a piece of powershell code to do it:

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

Then, I run the command in powershell and pass the parameter:

test.ps1 stage

It is just a simple code. However, when I tried to echo $newUrl to see its value, it does not return the value of serviceUrl under the build tag with type="stage".

Anyone has some idea? I read lots of sample on the internet but can't see any different.

Thanks in advance.

Upvotes: 0

Views: 378

Answers (2)

BartekB
BartekB

Reputation: 8650

I would suggest using Select-Xml instead:

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

Upvotes: 1

JamesQMurphy
JamesQMurphy

Reputation: 4428

I believe this is what you are looking for:

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

Even after you get the <serviceUrl> node, what you really want is the innerText out of that node.

Remember also that the variable $newUrl will not be available outside of the test.ps1 script. You can either scope the variable, or you can run the script using the "dot-source" operator:

. test.ps1 stage

Notice the . at the front.

Upvotes: 2

Related Questions