Parent XML - Powershell

Hey

<root>
	<Unit id="1">
		<name>Firm</name>

		<parentUnit></parentUnit>

	</Unit>

	<Unit id="2">
		<name>Boston</name>

		<parentUnit>1</parentUnit>

	</Unit>
		

	<Unit id="3">
		<name>HR</name>

		<parentUnit>2</parentUnit>

	</Unit>
		
	<Unit id="4">
		<name>NY</name>

		<parentUnit>1</parentUnit>

	</Unit>

	<Unit id="5">
		<name>Internal</name>

		<parentUnit>3</parentUnit>

	</Unit>
</root>

Open in new window


Any easy way to return? (parent / child) - Powershell (XML)

Firm
Firm, Boston
Firm, Boston, HR
Firm, NY
Firm, Boston, HR, Internal
LVL 1
mikeydkAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

aikimarkCommented:
$xml=[xml]'<root><Unit id="1"><name>Firm</name><parentUnit></parentUnit></Unit><Unit id="2"><name>Boston</name><parentUnit>1</parentUnit></Unit><Unit id="3"><name>HR</name><parentUnit>2</parentUnit></Unit><Unit id="4"><name>NY</name><parentUnit>1</parentUnit></Unit><Unit id="5"><name>Internal</name><parentUnit>3</parentUnit></Unit></root>'

$xml.SelectSingleNode('root').childnodes | % -begin{$parentpath = @{}} -Process{$x = ($parentpath[$_.Parentunit] + ', ' + $_.name) -replace '^, ', '';$parentpath.Add($_.id, $x)}

$parentpath.GetEnumerator() | sort -Property Name

Open in new window

0
mikeydkAuthor Commented:
Thanks ;)

Is it possible to return the values as an array? (and not a string?)

Thanks in advance

Mike
0
aikimarkCommented:
$xml=[xml]'<root><Unit id="1"><name>Firm</name><parentUnit></parentUnit></Unit><Unit id="2"><name>Boston</name><parentUnit>1</parentUnit></Unit><Unit id="3"><name>HR</name><parentUnit>2</parentUnit></Unit><Unit id="4"><name>NY</name><parentUnit>1</parentUnit></Unit><Unit id="5"><name>Internal</name><parentUnit>3</parentUnit></Unit></root>'

$xml.SelectSingleNode('root').childnodes | % -begin{$parentpath = @{}} -Process{$x = ($parentpath[$_.Parentunit] + ',' + $_.name) -replace '^,', '';$parentpath.Add($_.id, $x)}

$parentpath.GetEnumerator() | sort -Property Name | %{$a = ($_.value -split ","); $a}

Open in new window

If you want to prove that $a is an array/list, change the last line to this to see a pipe-delimited display of the elements
$parentpath.GetEnumerator() | sort -Property Name | %{$a = ($_.value -split ","); $a -join "|"}

Open in new window

0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
My suggestion is looking slightly different, and directly generates an array.
$parentPath = @{}
$xml.root.Unit | sort ParentUnit, ID |
  % { $parentpath[$_.ID] = @(if ($_.ParentUnit -gt 0) {$parentPath[$_.ParentUnit]} else {@()}) + $_.name }

$parentpath.GetEnumerator() | sort name

Open in new window

It also grants to process in the proper sequence, so the XML items do not need to be ordered correctly. Depending on how the XML is generated, that might be important or irrelevant.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mikeydkAuthor Commented:
aikimark> Seems to be working - just don't like the split-function
Qlemo> for some strange reason - some of the results return $null?
0
aikimarkCommented:
just don't like the split-function
Why?
Does it matter?
0
mikeydkAuthor Commented:
aikimark > Some of the names contains comma...
0
aikimarkCommented:
Please post a REPRESENTATIVE sample of the XML data.
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Do you get $null with your example XML posted above? Can you provide another example, so I can test with more data?
0
aikimarkCommented:
two solutions to the problem
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.