XML到CSV数据导出

时间:2019-04-27 15:00:01

标签: powershell

我有100个zip文件,每个zip文件都有10-20 xml。我需要将特定标签元素导出到csv。 例如,我有20个zip文件,每个文件含10个xml(20 * 10 = 200),一个csv中应显示200行。

示例XML代码:

    <?xml version='1.0' encoding='UTF-8'?>
    <bookstore>
      <book category="children">
        <title>Harry Potter</title>
        <author>J K. Rowling</author>
        <year>2019</year>
        <price>20.99</price>
      </book>
    </bookstore>

我需要将标题和价格导出到csv中。

3 个答案:

答案 0 :(得分:1)

尝试类似这样的东西:

[Void][Reflection.Assembly]::LoadWithPartialName('System.IO.Compression.FileSystem')  

#take all zip files
Get-ChildItem "C:\temp\test1" -file -Filter "*.zip" | %{

        #take only xml file
        [IO.Compression.ZipFile]::OpenRead($_.FullName).Entries | where Fullname -like "*.xml" | %{

        #extract xml only
        $PathXMLFileextracted="C:\temp\" + $_.name
        [System.IO.Compression.ZipFileExtensions]::ExtractToFile($_, $PathXMLFileextracted, $true)

        #take content of xml
        [XML] $xml=get-content $PathXMLFileextracted

        #remove new xml created (for clean)
        Remove-Item $PathXMLFileextracted

        #take necessary data
        $xml.bookstore.book | select title, price
        }


} | export-csv "c:\temp\result.csv" -NoTypeInformation # export result to csv

答案 1 :(得分:0)

这是一个开始。关键是强制转换为[xml]。没有“ import-xml” cmdlet。  在导出到csv之前,您需要从powershell $ xml对象中选择所需的内容。 $ xml.save('file.xml')导出回xml。

PS /Users/js> [xml]$xml = get-content file.xml
PS /Users/js> $xml

xml                            bookstore
---                            ---------
version="1.0" encoding="UTF-8" bookstore

PS /Users/js> $xml | export-csv file.csv
PS /Users/js> get-content file.csv
"xml","bookstore"
"version=""1.0"" encoding=""UTF-8""","System.Xml.XmlElement"

答案 2 :(得分:0)

所以...这个...

[xml]$XmlDoc = @'
<?xml version='1.0' encoding='UTF-8'?>
<bookstore>
    <book category="children">
    <title>Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2019</year>
    <price>20.99</price>
    </book>
</bookstore>
'@


$XmlDoc.bookstore.book | 
Select-Object -Property title, price

# Results

title        price
-----        -----
Harry Potter 20.99



$XmlDoc.bookstore.book | 
Select-Object -Property title, price | 
Export-Csv -Path 'E:\Temp\BookList.csv'

Import-Csv -Path 'E:\Temp\BookList.csv'

# Results

title        price
-----        -----
Harry Potter 20.99