本帖最后由 flashercs 于 2019-7-27 20:56 编辑
回复 3# smss | | | $xmlfile = 'manifest.xml' | | | | $outfile = '数组信息.xml' | | | | $数字数组 = 'x', 'y', 'w', 'h', 'pivotX', 'pivotY', 'rotation', 'visibility', 'size' | | | | | | $字符数组 = 'name', 'src', 'srcExp', 'align', 'alignV', 'text', 'textExp', 'color', 'fillColor', 'strokeColor', 'package', 'class' | | | | $strxml = Get-Content -ReadCount 0 -LiteralPath $xmlfile -Encoding UTF8 -Raw | | $rexmlnode = [regex]'<[^>]+>' | | $rexmlnodename = [regex]'(?<=<)\w+' | | $rexmlattribute = [regex]'\w+="[^"]*"' | | ( | | ($rexmlnode.Matches($strxml) | ForEach-Object -Begin { Write-Host 'Matching XML node elements...begin' -ForegroundColor Green } -End { Write-Host 'Matching XML node elements...end' -ForegroundColor Green } -Process { | | $strnode = $_.Value | | | | invoke-expression "[pscustomobject]@{__nodeName__=`"$($rexmlnodename.Match($strnode).Value)`";$($rexmlattribute.Matches($strnode).Value -join ';')}" | | } ) | Group-Object -Property '__nodeName__' | ForEach-Object -Begin { Write-Host 'Grouping XML node elements...begin' -ForegroundColor Green } -End { Write-Host 'Grouping XML node elements...end' -ForegroundColor Green } -Process { | | $groupName = $_.Name | | | | foreach ($attributeName in (($_.Group | ForEach-Object { Get-Member -InputObject $_ -MemberType Properties }).Name | Select-Object -Unique | Where-Object { $_ -ne '__nodeName__' })) { | | $attributes = $_.Group.$attributeName | Where-Object { $null -ne $_ } | | if ($attributeName -in $字符数组) { | | "<Var name=`"${groupName}_${attributename}共$($attributes.Count)个`" type=`"string[]`" const=`"true`" expression=`"''`" values=`"$($attributes -replace '(?s)^.*$',"'$&'" -join ',')`"/>" | | } | | elseif ($attributename -in $数字数组) { | | "<Var name=`"${groupName}_${attributename}共$($attributes.Count)个`" type=`"number[]`" const=`"false`" values=`"$($attributes -join ',')`"/>" | | } | | } | | }) | Set-Content -LiteralPath $outfile -Encoding UTF8COPY |
|