powershell 通过Foreach循环解析XML值并基于它生成URL

yk9xbfzb  于 2022-11-10  发布在  Shell
关注(0)|答案(1)|浏览(102)

所以我需要做任务是从/ROOT/USERLIST/USER中提取sn&devid值,然后根据这2个值为每个设备生成一个url,我已经能够达到所需的值,但创建保证sn值与devid值的相同节点匹配的循环也是一个问题

<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>

到目前为止,我能做的是

[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:admin1234@123.123.123.123:22222/1_" + $DevID + "_1"
    write-host $url
}
rkttyhzu

rkttyhzu1#

尝试使用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

相关问题