将XML转换为PSObject

时间:2010-07-14 02:29:18

标签: xml powershell

注意:我使用的是ConvertTo-XML,无法使用Export-Clixml

我创建了一个简单的PSObjec t:

$a = New-Object PSObject -Property @{
    Name='New'
    Server = $null
    Database = $null
    UserName = $null
    Password = $null
}

然后我使用ConvertTo-XML将其转换为XML:

$b = $a | Convertto-XML -NoTypeInformation

XML看起来像这样:

<?xml version="1.0"?>
<Objects>
  <Object>
    <Property Name="Password" />
    <Property Name="Name">New</Property>
    <Property Name="Server" />
    <Property Name="UserName" />
    <Property Name="Database" />
  </Object>
</Objects>

我无法找出点符号或XPath查询来提取属性/元素并将$b转换回原来的PSObject

3 个答案:

答案 0 :(得分:16)

您可以使用XPath轻松完成此操作。尽管PowerShell通常使得使用XML非常简单,但在这种情况下,我认为使用严格的PowerShell语法的格式非常粗略。

filter XmlProperty([String]$Property) {
    $_.SelectSingleNode("/Objects/Object/Property[@Name='$Property']").InnerText
}

$Name = $b | Xmlproperty Name
$Server = $b | XmlProperty Server
# etc...

编辑:通常对包含一个或多个Object元素的XML文档执行此操作,您可以执行以下操作:

function ConvertFrom-Xml($XML) {
    foreach ($Object in @($XML.Objects.Object)) {
        $PSObject = New-Object PSObject
        foreach ($Property in @($Object.Property)) {
            $PSObject | Add-Member NoteProperty $Property.Name $Property.InnerText
        }
        $PSObject
    }
}

ConvertFrom-Xml $b

答案 1 :(得分:2)

我的变体,无限深度。

参见示例。

nginx:
    build: ./docker/nginx
    container_name: nginx
    ports:
      - '80:80'
    volumes:
      - "${PROJECT_ROOT}:/var/www/html:ro"
    networks:
      - server
    extra_hosts:
      - 'test.local:127.0.0.1'
    depends_on:
      - php
    restart: always

答案 2 :(得分:1)

我通常将xml解析为哈希表但使用我从here抓取的convertto函数我将该函数转换为pscustom对象

function xmlNodeToPsCustomObject ($node){
    $hash = @{}
    foreach($attribute in $node.attributes){
        $hash.$($attribute.name) = $attribute.Value
    }
    $childNodesList = ($node.childnodes | ?{$_ -ne $null}).LocalName
    foreach($childnode in ($node.childnodes | ?{$_ -ne $null})){
        if(($childNodesList | ?{$_ -eq $childnode.LocalName}).count -gt 1){
            if(!($hash.$($childnode.LocalName))){
                $hash.$($childnode.LocalName) += @()
            }
            if ($childnode.'#text' -ne $null) {
                $hash.$($childnode.LocalName) += $childnode.'#text'
            }
            $hash.$($childnode.LocalName) += xmlNodeToPsCustomObject($childnode)
        }else{
            if ($childnode.'#text' -ne $null) {
                $hash.$($childnode.LocalName) = $childnode.'#text'
            }else{
                $hash.$($childnode.LocalName) = xmlNodeToPsCustomObject($childnode)
            }
        }   
    }
    return $hash | ConvertTo-PsCustomObjectFromHashtable
}

function ConvertTo-PsCustomObjectFromHashtable { 
    param ( 
        [Parameter(  
            Position = 0,   
            Mandatory = $true,   
            ValueFromPipeline = $true,  
            ValueFromPipelineByPropertyName = $true  
        )] [object[]]$hashtable 
    ); 

    begin { $i = 0; } 

    process { 
        foreach ($myHashtable in $hashtable) { 
            if ($myHashtable.GetType().Name -eq 'hashtable') { 
                $output = New-Object -TypeName PsObject; 
                Add-Member -InputObject $output -MemberType ScriptMethod -Name AddNote -Value {  
                    Add-Member -InputObject $this -MemberType NoteProperty -Name $args[0] -Value $args[1]; 
                }; 
                $myHashtable.Keys | Sort-Object | % {  
                    $output.AddNote($_, $myHashtable.$_);  
                } 
                $output
            } else { 
                Write-Warning "Index $i is not of type [hashtable]"; 
            }
            $i += 1;  
        }
    } 
}
相关问题