在Powershell中解析XML文件

时间:2017-10-11 09:40:47

标签: xml powershell parsexml

我正在尝试从雅虎检索汇率

class MyModelForm(forms.Form):
    pass  # Several lines of code here for the needs of the Model Form

# The following form will be called from the admin inline class only
class MyModelInlineForm(MyModelForm):
    def clean_category(self):
        category = self.cleaned_data.get('category', None)
        initial_value = getattr(
            self.fields.get('category', None), 
            'initial', 
            None
        )
        if all(
            (
                self.has_changed(),
                category.id != initial_value,
            )
        ):
            raise forms.ValidationError(
                _('You cannot change this'),
                code='Forbidden'
            )
        return category


    class Meta:
        # Copy here the Meta class of the parent model 

但我无法按要求获取数据,我只能得到:

Name     price
----     -----
resource      
resource      
resource      
resource      
resource      
.
.
..

1 个答案:

答案 0 :(得分:3)

试试这个:

$browser = New-Object System.Net.WebClient
$browser.Proxy.Credentials =[System.Net.CredentialCache]::DefaultNetworkCredentials 
$url = 'http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote'
[xml]$xml = $browser.DownloadString($url)

$resultset = New-Object System.Collections.ArrayList
foreach ($quote in $xml.list.resources.resource){
    $singlequote = New-Object System.Collections.Specialized.OrderedDictionary
    foreach($element in $quote.field){
        $singlequote.Add($element.name,$element.'#text')
    }
    $resultset += New-Object PSObject -Property $singlequote
}
$resultset | format-table 

当你得到 $ xml.list.resources.resource 时,它就像 System.Array 那样你需要分别处理每个对象。

相关问题