XML parsing multi values?

This topic contains 0 replies, has 1 voice, and was last updated by Profile photo of Forums Archives Forums Archives 5 years, 5 months ago.

  • Author
    Posts
  • #5126

    by Dwayne Dibbley at 2012-09-19 10:01:21

    i have the following xml


    < ?xml version="1.0" encoding="utf-8" standalone="no" ?>
























    whay i am trying to do is for each field, loop through all the coordinates and call a function the the values of startX startZ endX endZ

    so i can open the xml and loop through them all with

    $myxml = [xml](Get-Content .\fieldData.xml)
    $myxml.fieldData.map.field | foreach { $_.coordinates }

    and results in

    startX startZ endX endZ
    ------ ------ ---- ----
    693 92 707 96
    693 97 722 101
    688 102 737 106
    688 107 752 111

    question is how do i get the startx startz etc to call a function?

    also i would like it to result in the following so the multiple coordinates can be associated with the field:

    field 1
    startX startZ endX endZ
    field 2
    startX startZ endX endZ

    Thanks

    by surveyor at 2012-09-20 04:31:53

    Hi Dwayne,
    maybe this makes it a bit clearer:
    Clear-Host

    $myxml = [xml]@"
    < ?xml version="1.0" encoding="utf-8" standalone="no" ?>
























    "@

    function test-function ($Name,$Coordinates) {
    $Name
    foreach ($c in $Coordinates) {
    "{0} {1} {2} {3}" -f $c.StartX,$c.StartZ,$c.EndX,$c.EndZ
    }
    }

    $myxml.fieldData.map.field | ForEach-Object { test-function $_.name $_.coordinates }

    by Dwayne Dibbley at 2012-09-21 10:20:11

    Many thanks for that i have now added a drawing element to the function based on the field content as follows:

    Clear-Host

    Add-Type -AssemblyName System.Drawing

    $filename = "$home\foo.png"
    $bmp = new-object System.Drawing.Bitmap 3000,3000
    $g = [System.Drawing.Graphics]::FromImage($bmp)

    $myxml = [xml](Get-Content .\fieldData.xml)

    function test-function ($Name,$Coordinates,$Fruit) {
    $Name + " " + $Fruit
    $pencolour = "Black"
    if ($Fruit -eq "grass") {
    $pencolour = "Green"
    }
    foreach ($c in $Coordinates) {
    "{0} {1} {2} {3}" -f $c.StartX,$c.StartZ,$c.EndX,$c.EndZ
    $g.drawRectangle([System.Drawing.Pens]::$pencolour,$c.StartX,$c.StartZ,$c.EndX-$c.StartX,$c.EndZ-$c.StartZ)
    $g.FillRectangle([System.Drawing.Brushes]::$pencolour,$c.StartX,$c.StartZ,$c.EndX-$c.StartX,$c.EndZ-$c.StartZ)
    }
    }

    $myxml.fieldData.map.field | ForEach-Object { test-function $_.name $_.coordinates $_.fruit.name}

    $bmp.Save($filename)

    Invoke-Item $filename

You must be logged in to reply to this topic.