Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Copy nodes, based on nested nodes, between xml files.

$
0
0

Hi,

I have 2 XML files. I need to copy some nodes from one to the other. This will be determined by a node nested within those nodes.

This will be run from a batch script, which needs to pass over the variable to search for. I originally tried to accomplish this in the batch script itself, but it got very messy, very quickly and I don't even know if it is possible.

Here is an example:

So the batch script has the variable %cata% which is Category A and it needs to pass that to the Powershell script first.

Then the Powershell script needs to search for all <Category> nodes tagged Category A. It then needs to copy all<Item> nodes that include these.

XML 1:

<?xml version="1.0" standalone="yes"?><Program><Item><ID>01</ID><Title>Item 1</Title><var1 /><var2 /><Category>Category A</Category><var3 /><var4 /></Item><Item><ID>02</ID><Title>Item 2</Title><var1 /><var2 /><Category>Category A</Category><var3 /><var4 /></Item><Item><ID>03</ID><Title>Item 3</Title><var1 /><var2 /><Category>Category B</Category><var3 /><var4 /></Item><Settings>
       Various Settings</Settings></Program>

XML 2:

<?xml version="1.0" standalone="yes"?><Program><Item><ID>04</ID><Title>Item 4</Title><var1 /><var2 /><Category>Category A</Category><var3 /><var4 /></Item><Item><ID>05</ID><Title>Item 5</Title><var1 /><var2 /><Category>Category A</Category><var3 /><var4 /></Item><Settings>
       Various Settings</Settings></Program>

After the script has run, XML 2 should look something like this:


<?xml version="1.0" standalone="yes"?><Program><Item><ID>01</ID><Title>Item 1</Title><var1 /><var2 /><Category>Category A</Category><var3 /><var4 /></Item><Item><ID>02</ID><Title>Item 2</Title><var1 /><var2 /><Category>Category A</Category><var3 /><var4 /></Item><Item><Item><ID>04</ID><Title>Item 4</Title><var1 /><var2 /><Category>Category A</Category><var3 /><var4 /></Item><Item><ID>05</ID><Title>Item 5</Title><var1 /><var2 /><Category>Category A</Category><var3 /><var4 /></Item><Settings>
       Various Settings</Settings></Program>


Now I would be very happy just to get that part done, but there is more.

Each of these items has a corresponding folder named <Title>.39DJOU008(string of varying numbers/letters)JJ8SBD9 (residing in a different directory)

Each node that is copied from the xml file, also needs the corresponding folder copying to a new destination.

This is what I have come up with so far, am I on the right tracks?

$oldxml = Get-ChildItem c:\xml\oldxml.xml
$newxml = Get-ChildItem c:\xml2\newxml.xml
{
(Get-Content $newxml) -copy '<Item>.*<Category>Category A</Category>.*</Item>' | Out-File $newxml -encoding utf8
}

The copying of the folders is not essential, I can probably accomplish that in the batch file, especially if Powershell can output a txt document listing the <Title>'s that it copied.

Thanks




Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>