首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将XML转换为PSObject

将XML转换为PSObject
EN

Stack Overflow用户
提问于 2010-07-14 10:29:18
回答 3查看 48.2K关注 0票数 24

注意:我正在使用ConvertTo-XML,无法使用Export-Clixml

我创建了一个简单的PSObject:

$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时遇到了问题。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-07-14 10:37:08

使用XPath可以很容易地做到这一点。尽管PowerShell通常使处理XML变得非常简单,但在这种情况下,我认为严格使用PowerShell语法的格式会非常粗糙。

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

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

编辑:要对包含一个或多个对象元素的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
票数 19
EN

Stack Overflow用户

发布于 2017-07-01 02:15:30

我的变体有无限的深度。

请参见示例。

function ConvertFrom-Xml {
<#
.SYNOPSIS
    Converts XML object to PSObject representation for further ConvertTo-Json transformation
.EXAMPLE
    # JSON->XML
    $xml = ConvertTo-Xml (get-content 1.json | ConvertFrom-Json) -Depth 4 -NoTypeInformation -as String
.EXAMPLE
    # XML->JSON
    ConvertFrom-Xml ([xml]($xml)).Objects.Object | ConvertTo-Json
#>
    param([System.Xml.XmlElement]$Object)

    if (($Object -ne $null) -and ($Object.Property -ne $null)) {
        $PSObject = New-Object PSObject

        foreach ($Property in @($Object.Property)) {
            if ($Property.Property.Name -like 'Property') {
                $PSObject | Add-Member NoteProperty $Property.Name ($Property.Property | % {ConvertFrom-Xml $_})
            } else {
                if ($Property.'#text' -ne $null) {
                    $PSObject | Add-Member NoteProperty $Property.Name $Property.'#text'
                } else {
                    if ($Property.Name -ne $null) {
                        $PSObject | Add-Member NoteProperty $Property.Name (ConvertFrom-Xml $Property)
                    }
                }
            } 
        }   
        $PSObject
    }
}
票数 4
EN

Stack Overflow用户

发布于 2017-10-16 00:09:05

我通常将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;  
        }
    } 
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3242995

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档