0

I have 3 config files like ConfigA.xml, ConfigB.xml and Config_template.xml

ConfigA.xml

<?xml version="1.0" encoding="utf-16"?>
<properties>
    <jenkins.model.BuildDiscarderProperty>
      <strategy class="hudson.tasks.LogRotator">
        <daysToKeep>30</daysToKeep>
        <numToKeep>-1</numToKeep>
        <artifactDaysToKeep>-1</artifactDaysToKeep>
        <artifactNumToKeep>-1</artifactNumToKeep>
      </strategy>
    </jenkins.model.BuildDiscarderProperty>
    <hudson.model.ParametersDefinitionProperty>
      <parameterDefinitions>
        <hudson.model.StringParameterDefinition>
          <name>parameter</name>
          <description>parameter description.</description>
          <defaultValue>parmeter value is not Set</defaultValue>
        </hudson.model.StringParameterDefinition>
        <hudson.model.StringParameterDefinition>
          <name>parameter2</name>
          <description></description>
          <defaultValue></defaultValue>
        </hudson.model.StringParameterDefinition>
        <hudson.model.StringParameterDefinition>
          <name>parameter3</name>
          <description>parameter description.</description>
          <defaultValue></defaultValue>
        </hudson.model.StringParameterDefinition>
        <hudson.model.StringParameterDefinition>
          <name>parameter4</name>
          <description></description>
          <defaultValue></defaultValue>
        </hudson.model.StringParameterDefinition>
        <hudson.model.StringParameterDefinition>
          <name>parameter5</name>
          <description>parameter description.</description>
          <defaultValue>parmetervalue</defaultValue>
        </hudson.model.StringParameterDefinition>
      </parameterDefinitions>
    </hudson.model.ParametersDefinitionProperty>
  </properties>

ConfigB.xml

<?xml version="1.0" encoding="utf-16"?>
<properties>
    <jenkins.model.BuildDiscarderProperty>
      <strategy class="hudson.tasks.LogRotator">
        <daysToKeep>10</daysToKeep>
        <numToKeep>-1</numToKeep>
        <artifactDaysToKeep>-1</artifactDaysToKeep>
        <artifactNumToKeep>-1</artifactNumToKeep>
      </strategy>
    </jenkins.model.BuildDiscarderProperty>
    <hudson.model.ParametersDefinitionProperty>
    </hudson.model.ParametersDefinitionProperty>
  </properties>

Config_template.xml

<?xml version="1.0" encoding="utf-16"?>
<flow-definition plugin="workflow-job@1316.vd2290d3341a_f">
  <actions>
    <org.jenkinsci.plugins.pipeline.modeldefinition.actions.DeclarativeJobAction plugin="pipeline-model-definition@2.2141.v5402e818a_779" />
    <org.jenkinsci.plugins.pipeline.modeldefinition.actions.DeclarativeJobPropertyTrackerAction plugin="pipeline-model-definition@2.2141.v5402e818a_779">
      <jobProperties />
      <triggers />
      <parameters />
      <options />
    </org.jenkinsci.plugins.pipeline.modeldefinition.actions.DeclarativeJobPropertyTrackerAction>
  </actions>
  <description />
  <properties>
    <jenkins.model.BuildDiscarderProperty>
      <strategy class="hudson.tasks.LogRotator">
        <daysToKeep>xxxxxxx</daysToKeep>
        <numToKeep>xxxx</numToKeep>
        <artifactDaysToKeep>xxxxxx</artifactDaysToKeep>
        <artifactNumToKeep>xxxxxxxx</artifactNumToKeep>
      </strategy>
    </jenkins.model.BuildDiscarderProperty>
    <hudson.model.ParametersDefinitionProperty>
      <parameterDefinitions>
        <hudson.model.StringParameterDefinition>
          <name>xxxxxxxxx</name>
          <description>xxxxxxxxxxxxxx</description>
          <defaultValue>xxxxxxxxxxxxxx</defaultValue>
        </hudson.model.StringParameterDefinition>
      </parameterDefinitions>
    </hudson.model.ParametersDefinitionProperty>
  </properties>
</flow-definition>

I have to read each config file(ConfigA and ConfigB) and compare with Config_template.xml file and replace the matching nodes in templete file or else remove the child node from template file.

Example1

In ConfigA file jenkins.model.BuildDiscarderProperty and parameterDefinitions nodes are exit so I need to Append those nodes into the Config_template.xml

Example2

In ConfigB file we have only jenkins.model.BuildDiscarderProperty node so I just want to copy matching node and remove the unmatched nodes in the Config_template.xml

I have tried some solutions from here [link:https://stackoverflow.com/questions/64209684/append-or-add-new-xml-node-from-one-file-to-another-file-using-powershell] and [link:https://stackoverflow.com/questions/50599070/copy-xml-node-from-one-file-and-append-it-to-another-xml-file] but it was not giving my required output The code which I tried

#destination file
[xml] $serverFile = Get-Content   -Path 'path/to/Config_template.xml'
#source file
[xml] $localFile = Get-Content -Path 'path/to/ConfigA'

$parent = $serverFile.SelectSingleNode("/properties")
# enumerate source nodes
foreach ($localNode in $localFile.SelectNodes("/properties/hudson.model.ParametersDefinitionProperty")) {
    # check if node exists in destination
    $serverNode = $parent.SelectSingleNode("/properties/hudson.model.ParametersDefinitionProperty']")
    # remove if exists
    if ($serverNode) {
        [void]$parent.RemoveChild($serverNode)
    }
    # append from source
    [void]$parent.AppendChild($serverFile.ImportNode($serverNode, $true))
}
$serverFile.Save('path/to/Converted_Config.xml')
Hemanth
  • 41
  • 4
  • Can you show what code you've already written and describe what's not working? You're more likely to get a useful answer if you post a specific technical issue with some existing code rather than asking for help writing something from scratch - see https://stackoverflow.com/help/how-to-ask for more details... – mclayton Jul 26 '23 at 12:53

1 Answers1

0

You xml is not Well formed because it has more than one element at the root. Because of this, the easiest way is to change template so it does not have template

Template File

<?xml version="1.0" encoding="utf-16"?>
<actions>
    <org.jenkinsci.plugins.pipeline.modeldefinition.actions.DeclarativeJobAction plugin="pipeline-model-definition@2.2141.v5402e818a_779" />
    <org.jenkinsci.plugins.pipeline.modeldefinition.actions.DeclarativeJobPropertyTrackerAction plugin="pipeline-model-definition@2.2141.v5402e818a_779">
      <jobProperties />
      <triggers />
      <parameters />
      <options />
    </org.jenkinsci.plugins.pipeline.modeldefinition.actions.DeclarativeJobPropertyTrackerAction>
  </actions>
  <description />

Then you can just append properties to the file after skipping first line of xml which is the Ident line

$templateFilename = "c:\temp\test.xml"
$inputFilename = "c:\temp\test1.xml"
$outputFilename = "c:\temp\test2.xml"

$template = Get-Content -Path $templateFilename
$properties = Get-Content -Path $inputFilename | Select-Object -Skip 1 #skip ident
$template + $Properties | Out-File -FilePath $outputFilename
jdweng
  • 33,250
  • 2
  • 15
  • 20