Last active
August 29, 2015 14:01
-
-
Save mirontoli/e3cd4d5d50fcfb986079 to your computer and use it in GitHub Desktop.
Create an xml file from CSV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function New-Location { | |
[CmdletBinding()] | |
Param( | |
[Parameter(Mandatory=$true)] | |
[string]$Title, | |
[Parameter(Mandatory=$true)] | |
[int]$Level, | |
[Parameter(Mandatory=$false)] | |
[PSObject]$Parent = $null | |
) | |
return New-Object –TypeName PSObject –Prop @{ | |
Title = $Title | |
Level = $Level | |
Parent = $Parent | |
} | |
} | |
#get all locations, ignore the first line in the CSV | |
$sourceLocations = Get-Content -Path "Source.csv" | Select-Object -Skip 1 | |
#convert to a new PSObject with properties: Level (meaning column in CSV) and Title | |
# Level and Title are a "composed key for the location (term) | |
$originalLocations = $sourceLocations | Foreach-Object { | |
$row = $_.split(";") | |
$title = $row | Where-Object { $_ -ne "" } | |
$level = $row.IndexOf($title) | |
return New-Location -Title $title -Level $level | |
} | |
#find parens for every row | |
$withParents = $originalLocations | Foreach-Object { | |
$location = $_ | |
$originalIndex = $originalLocations.IndexOf($location) | |
$parentLevel = $location.Level - 1 | |
$parent = $originalLocations[0..$originalIndex] | ? { $_.Level -eq $parentLevel } | select -last 1 | |
return New-Location -Title $location.Title -Level $location.Level -Parent $parent | |
} | |
function serialize($node) { | |
$level = $node.Level | |
$title = $node.Title | |
$begin = "<Location><Title>" + $title + "</Title>" | |
$end = "</Location>" | |
$children = $withParents | ? { $_.Parent.Level -eq $level -and $_.Parent.Title -eq $title } | |
if ($children) { | |
$strings = [string] ($children | Foreach-Object { serialize $_ }) | |
$middle = "<Locations>$strings</Locations>" | |
} | |
return $begin, $middle, $end -join "" | |
} | |
$root = $withParents | Where-Object { $_.Level -eq 0 } | |
$xml = "<Root>$(serialize $root)</Root>" | |
$xml > "Destination.xml" | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Root> | |
<Location> | |
<Title>Global</Title> | |
<Locations> | |
<Location> | |
<Title>Process 1</Title> | |
<Locations> | |
<Location> | |
<Title>Process 1.1</Title> | |
<Locations> | |
<Location> | |
<Title>Process 1.1.1</Title> | |
<Locations> | |
<Location> | |
<Title>Process 1.1.1.1</Title> | |
</Location> | |
<Location> | |
<Title>Process 1.1.1.2</Title> | |
</Location> | |
<Location> | |
<Title>Process 1.1.1.3</Title> | |
</Location> | |
</Locations> | |
</Location> | |
</Locations> | |
</Location> | |
<Location> | |
<Title>Process 1.2</Title> | |
<Locations> | |
<Location> | |
<Title>Process 1.2.1</Title> | |
<Locations> | |
<Location> | |
<Title>Process 1.2.1.1</Title> | |
</Location> | |
<Location> | |
<Title>Process 1.2.1.2</Title> | |
</Location> | |
<Location> | |
<Title>Process 1.2.1.3</Title> | |
</Location> | |
<Location> | |
<Title>Process 1.2.1.4</Title> | |
</Location> | |
<Location> | |
<Title>Process 1.2.1.5</Title> | |
</Location> | |
<Location> | |
<Title>Process 1.2.1.6</Title> | |
</Location> | |
</Locations> | |
</Location> | |
<Location> | |
<Title>Process 1.2.2</Title> | |
<Locations> | |
<Location> | |
<Title>Process 1.2.2.1</Title> | |
</Location> | |
<Location> | |
<Title>Process 1.2.2.2</Title> | |
</Location> | |
<Location> | |
<Title>Process 1.2.2.3</Title> | |
</Location> | |
<Location> | |
<Title>Process 1.2.2.4</Title> | |
</Location> | |
<Location> | |
<Title>Process 1.2.2.5</Title> | |
</Location> | |
<Location> | |
<Title>Process 1.2.2.6</Title> | |
</Location> | |
<Location> | |
<Title>Process 1.2.2.7</Title> | |
</Location> | |
</Locations> | |
</Location> | |
</Locations> | |
</Location> | |
<Location> | |
<Title>Process 1.3</Title> | |
</Location> | |
<Location> | |
<Title>Process 1.4</Title> | |
</Location> | |
<Location> | |
<Title>Process 1.5</Title> | |
</Location> | |
<Location> | |
<Title>Process 1.6</Title> | |
</Location> | |
<Location> | |
<Title>Process 1.7</Title> | |
</Location> | |
</Locations> | |
</Location> | |
<Location> | |
<Title>Process 2</Title> | |
<Locations> | |
<Location> | |
<Title>Process 2.2</Title> | |
</Location> | |
<Location> | |
<Title>Process 2.3</Title> | |
</Location> | |
<Location> | |
<Title>Process 2.4</Title> | |
</Location> | |
<Location> | |
<Title>Process 2.5</Title> | |
</Location> | |
<Location> | |
<Title>Process 2.6</Title> | |
<Locations> | |
<Location> | |
<Title>Process 2.6.1</Title> | |
<Locations> | |
<Location> | |
<Title>Process 2.6.1.1</Title> | |
</Location> | |
<Location> | |
<Title>Process 2.6.1.2</Title> | |
</Location> | |
<Location> | |
<Title>Process 2.6.1.3</Title> | |
</Location> | |
<Location> | |
<Title>Process 2.6.1.4</Title> | |
</Location> | |
<Location> | |
<Title>Process 2.6.1.5</Title> | |
</Location> | |
<Location> | |
<Title>Process 2.6.1.6</Title> | |
</Location> | |
<Location> | |
<Title>Process 2.6.1.7</Title> | |
</Location> | |
<Location> | |
<Title>Process 2.6.1.8</Title> | |
</Location> | |
</Locations> | |
</Location> | |
<Location> | |
<Title>Process 2.6.2</Title> | |
</Location> | |
<Location> | |
<Title>Process 2.6.3</Title> | |
</Location> | |
<Location> | |
<Title>Process 2.6.4</Title> | |
</Location> | |
<Location> | |
<Title>Process 2.6.5</Title> | |
<Locations> | |
<Location> | |
<Title>Process 2.6.5.1</Title> | |
</Location> | |
<Location> | |
<Title>Process 2.6.5.2</Title> | |
</Location> | |
<Location> | |
<Title>Process 2.6.5.3</Title> | |
</Location> | |
<Location> | |
<Title>Process 2.6.5.4</Title> | |
</Location> | |
<Location> | |
<Title>Process 2.6.5.5</Title> | |
</Location> | |
<Location> | |
<Title>Process 2.6.5.6</Title> | |
</Location> | |
</Locations> | |
</Location> | |
</Locations> | |
</Location> | |
</Locations> | |
</Location> | |
<Location> | |
<Title>Process 3</Title> | |
</Location> | |
<Location> | |
<Title>Process 4</Title> | |
</Location> | |
<Location> | |
<Title>Process 5</Title> | |
</Location> | |
<Location> | |
<Title>Process 6</Title> | |
</Location> | |
<Location> | |
<Title>Process 7</Title> | |
</Location> | |
<Location> | |
<Title>Process 8</Title> | |
</Location> | |
<Location> | |
<Title>Process 9</Title> | |
</Location> | |
<Location> | |
<Title>Process 10</Title> | |
</Location> | |
</Locations> | |
</Location> | |
</Root> |
We can make this file beautiful and searchable if this error is corrected: No commas found in this CSV file in line 0.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
First Node;GlobalProcess;PrimaryProcess;Process;Activity | |
Global;;;; | |
;Process 1;;; | |
;;Process 1.1;; | |
;;;Process 1.1.1; | |
;;;;Process 1.1.1.1 | |
;;;;Process 1.1.1.2 | |
;;;;Process 1.1.1.3 | |
;;Process 1.2;; | |
;;;Process 1.2.1; | |
;;;;Process 1.2.1.1 | |
;;;;Process 1.2.1.2 | |
;;;;Process 1.2.1.3 | |
;;;;Process 1.2.1.4 | |
;;;;Process 1.2.1.5 | |
;;;;Process 1.2.1.6 | |
;;;Process 1.2.2; | |
;;;;Process 1.2.2.1 | |
;;;;Process 1.2.2.2 | |
;;;;Process 1.2.2.3 | |
;;;;Process 1.2.2.4 | |
;;;;Process 1.2.2.5 | |
;;;;Process 1.2.2.6 | |
;;;;Process 1.2.2.7 | |
;;Process 1.3;; | |
;;Process 1.4;; | |
;;Process 1.5;; | |
;;Process 1.6;; | |
;;Process 1.7;; | |
;Process 2;;; | |
;;Process 2.2;; | |
;;Process 2.3;; | |
;;Process 2.4;; | |
;;Process 2.5;; | |
;;Process 2.6;; | |
;;;Process 2.6.1; | |
;;;;Process 2.6.1.1 | |
;;;;Process 2.6.1.2 | |
;;;;Process 2.6.1.3 | |
;;;;Process 2.6.1.4 | |
;;;;Process 2.6.1.5 | |
;;;;Process 2.6.1.6 | |
;;;;Process 2.6.1.7 | |
;;;;Process 2.6.1.8 | |
;;;Process 2.6.2; | |
;;;Process 2.6.3; | |
;;;Process 2.6.4; | |
;;;Process 2.6.5; | |
;;;;Process 2.6.5.1 | |
;;;;Process 2.6.5.2 | |
;;;;Process 2.6.5.3 | |
;;;;Process 2.6.5.4 | |
;;;;Process 2.6.5.5 | |
;;;;Process 2.6.5.6 | |
;Process 3;;; | |
;Process 4;;; | |
;Process 5;;; | |
;Process 6;;; | |
;Process 7;;; | |
;Process 8;;; | |
;Process 9;;; | |
;Process 10;;; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment