返回列表 发帖
回复 7# smss
微信
微信:flashercs
QQ:49908356

TOP

回复 5# smss


    楼上改了 你试试
微信:flashercs
QQ:49908356

TOP

本帖最后由 flashercs 于 2019-7-27 20:56 编辑

回复 3# smss
# xml源文件
$xmlfile = 'manifest.xml'
# xml输出文件
$outfile = '数组信息.xml'
# attribute类型为number
$数字数组 = 'x', 'y', 'w', 'h', 'pivotX', 'pivotY', 'rotation', 'visibility', 'size'
# attribute类型为string
$字符数组 = '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
      # $strnodename=$rexmlnodename.Match($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
    # $groupCount = $_.Group.Count
    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
微信:flashercs
QQ:49908356

TOP

本帖最后由 flashercs 于 2019-7-27 19:21 编辑
# xml源文件
$xmlfile = 'C:\test\1.xml'
# xml输出文件
$outfile = 'C:\test\1_out.xml'
# attribute类型为string
$typestring = 'name', 'srcExp', 'src', 'align', 'alignV', 'strokeColor', 'package', 'class'
# attribute类型为number
$typenumber = 'x', 'y', 'pivotX', 'pivotY', 'rotation', 'visibility'
$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
      # $strnodename=$rexmlnodename.Match($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
    # $groupCount = $_.Group.Count
    foreach ($attributeName in (($_.Group[0] | Get-Member -MemberType Properties).Name | Where-Object { $_ -ne '__nodeName__' })) {
      $attributes = $_.Group.$attributeName | Where-Object { $null -ne $_ }
      if ($attributeName -in $typestring) {
        "<Var name=`"${groupName}_${attributename}共$($attributes.Count)个`" type=`"string[]`" const=`"true`" expression=`"''`" values=`"$($attributes -replace '(?s)^.*$',"'$&'" -join ',')`"/>"
      }
      elseif ($attributename -in $typenumber) {
        "<Var name=`"${groupName}_${attributename}共$($attributes.Count)个`" type=`"number[]`" const=`"false`" values=`"$($attributes -join ',')`"/>"
      }
    }
  }) | Set-Content -LiteralPath $outfile -Encoding UTF8COPY
微信:flashercs
QQ:49908356

TOP

返回列表