Nick W.
Nick W.

Reputation: 1614

PowerShell Joining Multiple XML Files

Not sure why I am having such a difficult time with this, but I cannot figure out how to join multiple XML files with an overarching wrapper of packages so that I can easily work with the data.

XML file:

<package>
    <Application>Java</Application>
    <Version>8.0.2010.9</Version>
    <InstallType>Install</InstallType>
    <Installers>
        <Windows10>
            <Architecture>
                <x86>
                    <File1>
                        <FileName>jre-8u201-windows-i586.exe</FileName>
                        <Parameters>/s</Parameters>
                    </File1>
                </x86>
                <x64>
                    <file1>
                        <FileName>jre-8u201-windows-x64.exe</FileName>
                        <Parameters>/s</Parameters>
                    </file1>
                </x64>
                <AMD64>
                    <File1>
                        <FileName></FileName>
                        <Parameters></Parameters>
                        <CustomSuccessCodes></CustomSuccessCodes>
                        <CustomErrorCodes></CustomErrorCodes>
                    </File1>
                </AMD64>
            </Architecture>
        </Windows10>
    </Installers>
</package>

What I have:

$Path = "D:\Desktop\AutoInstall\Packages\Java v8u201\package.xml"
$Xml = New-Object Xml
#$Xml.AppendChild($XML.CreateXmlDeclaration("1.0", "UTF-8", $null)) | Out-Null
$Xml.AppendChild($Xml.CreateElement("Packages")) | Out-Null

$newxml = [xml](Get-Content $Path)
$Xml.ImportNode($newxml.get_DocumentElement(), $true) | Out-Null

$newNode = $newxml.ImportNode($Xml.get_DocumentElement(), $true)
$newxml.DocumentElement.AppendChild($newNode) | Out-Null
$Xml.packages

When I type $xml.packages it shows empty when it should have this result:

Application Version  InstallType Installers
----------- -------  ----------- ----------
Java        8.2.9.23 Install     Installers

Upvotes: 3

Views: 2801

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200473

Your import goes in the wrong direction. You're importing the new XML document you created ($xml) into the XML document you loaded from the XML file. If you run $newxml.Save([Console]::Out) you'll see a node <Packages /> right before the closing </package>.

<package>
    <Application>Java</Application>
    <Version>8.0.2010.9</Version>
    <InstallType>Install</InstallType>
    <Installers>
        ...
    </Installers>
    <Packages />       <!-- right here -->
</package>

This statement would import $newxml like you wanted:

$xml.ImportNode($newxml.get_DocumentElement(), $true) | Out-Null

but you discard the result right away (Out-Null), and then do the import the other way 'round:

$newNode = $newxml.ImportNode($xml.get_DocumentElement(), $true)

This will do what you want:

$path = 'D:\Desktop\AutoInstall\Packages\Java v8u201\package.xml'

# create new XML document with <Packages> root node
$xml = New-Object Xml
$xml.AppendChild($xml.CreateElement('Packages')) | Out-Null

# load package XML from file and import it into $xml
$package = New-Object Xml
$package.Load($path)
$imported = $xml.ImportNode($package.DocumentElement, $true)
$xml.DocumentElement.AppendChild($imported) | Out-Null

$xml.Save([Console]::Out)

Upvotes: 2

Related Questions