Home > Blockchain >  Parse XML Values and generate url based on it via foreach loop
Parse XML Values and generate url based on it via foreach loop

Time:11-08

So that task i need to do is to extract the sn & devid value from /root/userlist/user and then generate a url based on this 2 value for each device , i have been able to reach the required values but it was also an issue to create the loop which guarantee that the sn value match the same node of devid value

<ret value="0">
        <stwebsvr url="ws://123.123.123:1234/login?user=admin,pwd=admin,type=1"/>
        <power value="0"/>
        <areaList>
        <area name="office" id="1" pid="-1"/>
        </areaList>
        <userlist>
        <user name="000001" personid="000001" areaid="2" videoip="123.123.123.123" videoport="22101" sid="1" devpoint="1" devid="2" sn="R101-00015" type="4" subtype="1">
        <point pointid="0" bsid="0" bstype="10" pointName="workStatus"/>
        <point pointid="5" bsid="0" bstype="11" pointName="talk"/>
        <point pointid="1" bsid="1" bstype="8" pointName="channel1"/>
        <point pointid="2" bsid="0" bstype="9" pointName="gps"/>
        <point pointid="3" bsid="0" bstype="12" pointName="alarm"/>
        <point pointid="6" bsid="0" bstype="13" pointName="callStatus"/>
        <point pointid="7" bsid="0" bstype="14" pointName="videoRecordStatus"/>
        <point pointid="8" bsid="0" bstype="15" pointName="audioRecordStatus"/>
        <point pointid="4" bsid="0" bstype="16" pointName="voiceRecgStatus"/>
        <point pointid="9" bsid="0" bstype="17" pointName="closeWindStatus"/>
        <point pointid="10" bsid="0" bstype="18" pointName="batteryStatus"/>
        <point pointid="11" bsid="0" bstype="19" pointName="powerOffStatus"/>
        <point pointid="12" bsid="0" bstype="20" pointName="lowPowerStatus"/>
        </user>
        <user name="000006" personid="000006" areaid="2" videoip="123.123.123.123" videoport="22101" sid="1" devpoint="1" devid="3" sn="R101-00008" type="4" subtype="1">
        <point pointid="0" bsid="0" bstype="10" pointName="workStatus"/>
        <point pointid="5" bsid="0" bstype="11" pointName="talk"/>
        <point pointid="1" bsid="1" bstype="8" pointName="channel1"/>
        <point pointid="2" bsid="0" bstype="9" pointName="gps"/>
        <point pointid="3" bsid="0" bstype="12" pointName="alarm"/>
        <point pointid="6" bsid="0" bstype="13" pointName="callStatus"/>
        <point pointid="7" bsid="0" bstype="14" pointName="videoRecordStatus"/>
        <point pointid="8" bsid="0" bstype="15" pointName="audioRecordStatus"/>
        <point pointid="4" bsid="0" bstype="16" pointName="voiceRecgStatus"/>
        <point pointid="9" bsid="0" bstype="17" pointName="closeWindStatus"/>
        <point pointid="10" bsid="0" bstype="18" pointName="batteryStatus"/>
        <point pointid="11" bsid="0" bstype="19" pointName="powerOffStatus"/>
        <point pointid="12" bsid="0" bstype="20" pointName="lowPowerStatus"/>
        </user>

what i have been able to do so far

[xml]$van = Get-Content "C:\Users\Desktop\cam.xml"

$camers = @(Select-Xml -Path C:\Users\Desktop\cam.xml -XPath "//user")
$users = $van.ret.userlist
$CamID = $van.ret.userlist.user.sn
$DevID = $van.ret.userlist.user.devid

foreach ($camer in $camers)
{
    write-host $user
    $url = "BWC Name: "   $CamID   "  "   "rtsp://admin:[email protected]:22222/1_"   $DevID   "_1"
    write-host $url
}

CodePudding user response:

Try Xml Linq :

using assembly System.Xml
using assembly System.Xml.Linq 

$inputFilename = "c:\temp\test.xml"
$table = [System.Collections.ArrayList]::new()
$xDoc = [System.Xml.Linq.XDocument]::Load($inputFilename)
$users = $xDoc.Descendants("user").Foreach([System.Xml.Linq.XElement])
foreach($user in $users)
{
   $newRow = New-Object -TypeName psobject
   $name = $user.Attribute("name").Value
   $newRow | Add-Member -NotePropertyName name -NotePropertyValue $name
   $devid = $user.Attribute("devid").Value
   $newRow | Add-Member -NotePropertyName devid -NotePropertyValue $devid
   $sid = $user.Attribute("sid").Value
   $newRow | Add-Member -NotePropertyName sid -NotePropertyValue $sid
   $table.Add($newRow) | out-Null
}
$table | Format-Table
  • Related