简体   繁体   English

如何使用PowerShell将具有不同元素/节点的xml文件合并到单个文件中

[英]How to merge xml files with different elements/nodes into a single file using PowerShell

I am trying to merge 3 xml files into 1 using PowerShell. 我正在尝试使用PowerShell将3个xml文件合并为1个。 I have tried a number of ways using Select-Xml, CreateElement followed by AppendChild etc but can't seem to understand how to get the elements I want to be included correctly into s new XML Document. 我尝试了多种方法,分别使用Select-Xml,CreateElement和AppendChild等,但是似乎无法理解如何获取要正确包含在新XML文档中的元素。 I either get an empty resultant file or System.Xml.Element in the nodes. 我或者在节点中得到一个空的结果文件或System.Xml.Element。

The three files have the contents: File A 这三个文件具有内容:文件A

<?xml version="1.0" encoding="utf-8"?>
<ResourceResponse>
<Status>Success</Status>
 <Resource id="12345" name="aName" description="Microsoft Windows 2008" location="" instanceId="10003" typeId="1">
 <Agent id="23456" address="x.x.x.x" port="2144" version="x.x.x" unidirectional="false" />
 <Ip address="x.x.x.x" netmask="255.0.0.0" mac="00:00:00:00:00:00" />
 <Ip address="x.x.x.x" netmask="255.255.240.0" mac="00:50:56:03:40:E2" />
 <ResourceInfo key="fqdn" value="aName.here.lo" />
 </Resource>
</ResourceResponse>

File B 文件B

<?xml version="1.0" encoding="utf-8"?>
<ResourcePrototypeResponse>
<Status>Success</Status>
<ResourcePrototype instanceId='10164' resourceTypeId='3' id='10213' name='TCP Socket'/>
</ResourcePrototypeResponse>

File C 文件C

<?xml version="1.0" encoding="utf-8"?>
      <Resource name="SocketHTTP">
            <ResourceConfig key="hostname" value="localhost"/>
            <ResourceConfig key="port" value="80"/>
            <ResourceConfig key="sotimeout" value="10"/>
            <ResourceConfig key="service.log_track.enable" value="false"/>
            <ResourceConfig key="service.log_track.level" value="Error"/>
            <ResourceConfig key="service.log_track.include" value=""/>
            <ResourceConfig key="service.log_track.exclude" value=""/>
            <ResourceInfo key="autoIdentifier" value="SocketHTTP"/>
        </Resource>

I would like the created file to look like: 我希望创建的文件看起来像:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<CreateResourceRequest>
<Resource id="12345" name="aName" description="Microsoft Windows 2008" location="" instanceId="10087" typeId="1">
 <Agent id="23456" address="x.x.x.x" port="2144" version="x.x.x" unidirectional="false"/>
  <Ip address="x.x.x.x" netmask="255.255.255.0"/>
  <Ip address="x.x.x.x" netmask="255.0.0.0"/>
 <ResourceInfo key="fqdn" value="aName.here.lo"/>
 </Resource>
 <ResourcePrototype instanceId="10164" resourceTypeId="3" id="10213" name="TCP Socket"/>
      <Resource name="SocketHTTP">
            <ResourceConfig key="hostname" value="target.here.lo"/>
            <ResourceConfig key="port" value="80"/>
            <ResourceConfig key="sotimeout" value="10"/>
            <ResourceConfig key="service.log_track.enable" value="false"/>
            <ResourceConfig key="service.log_track.level" value="Error"/>
            <ResourceConfig key="service.log_track.include" value=""/>
            <ResourceConfig key="service.log_track.exclude" value=""/>
            <ResourceInfo key="autoIdentifier" value="SocketHTTP"/>
        </Resource>

</CreateResourceRequest>

Thanks in advance, I'm stumped.... 在此先感谢,我很困惑。

You need something like this: 您需要这样的东西:

function Add-XmlFromFile ([String]$Path,
                          [String]$XPath,
                          [System.Xml.XmlElement]$ParentElement) {

    [System.Xml.XmlElement]$ChildElement = `
        (Select-Xml -Path $Path -XPath $XPath).Node

    [System.Xml.XmlElement]$ImportedElement = `
        $ParentElement.OwnerDocument.ImportNode($ChildElement, $true)

    return $ParentElement.AppendChild($ImportedElement)
}

# Creating new XML object.
$Xml = New-Object Xml
$XmlDeclaration = $Xml.CreateXmlDeclaration("1.0", "UTF-8", $null)
$Xml.AppendChild($XmlDeclaration) | Out-Null

# Creating parent element.
$CreateResourceRequest = $Xml.CreateElement("CreateResourceRequest")

# Adding child elements to parent.
Add-XmlFromFile -Path "File A.xml" `
    -XPath "//ResourceResponse/Resource" `
    -ParentElement $CreateResourceRequest | Out-Null
Add-XmlFromFile -Path "File B.xml" `
    -XPath "//ResourcePrototypeResponse/ResourcePrototype" `
    -ParentElement $CreateResourceRequest | Out-Null
Add-XmlFromFile -Path "File C.xml" `
    -XPath "//Resource" `
    -ParentElement $CreateResourceRequest | Out-Null

# Appending parent to XML object.
$Xml.AppendChild($CreateResourceRequest) | Out-Null

# Saving XML object.
$Xml.OuterXml | Out-File -FilePath "File D.xml" -Encoding "UTF8"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用python将两个不同的xml文件的选定行合并为单个xml文件? - How to merge only selected lines of two different xml files into single xml file using python? 使用 Powershell 从多个 XML 文件中的元素获取数据以输出到另一个单一的 XML 文件 - Get data from elements within multiple XML files for output to another, single XML file using Powershell 如何在PowerShell中将位于不同文件夹路径中的多个XML文件合并为一个XML文件? - How to merge multiple XML files located in different folder paths into one XML file in PowerShell? 如何在Java或任何其他工具中将具有不同节点结构的多个xml文件合并为单个xml文件? - How to merge multiple xml files with different node structure into a single xml file in java or any other tool? 使用xslt将多个xml文件合并为一个xml文件 - Merge multiple xml files to one single xml file using xslt 如何在同一个 XML 文件中合并两个不同的子节点 - How to merge two different child nodes in the same XML file 如何使用 C# 将 xml 文件合并为一个具有两个特定节点的文件 - How to merge xml files into one file with two specific nodes using C# 如何使用xslt合并xml文件并删除空元素 - How to merge xml files using xslt and remove empty elements 如何使用 C#.NET 将具有相同格式数据的多个 XML 文件合并为一个包含所有数据的单个 XML 文件? - How to merge multiple XML files with data in same format in it to a single XML file with all data together using C#.NET? 如何通过powershell,batchfile根据节点将XML文件剪切并将其移动到其他位置 - How to cut xml files and move them to a different location based on nodes via powershell , batchfile
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM