批处理获取XML节点数值
2023-6-9 乱云飞 代码 评论(0) 浏览(129) 标签: 批处理 VBS 获取XML 读取XML
//用bat获取XML节点 SET FILE=onvif.xml SET NODE=http_port FOR /F "tokens=2 delims=<>/" %%a IN ('type %FILE%^| find "<%NODE%>"') DO SET VALUE=%%a ECHO 端口号是:%VALUE% cmd/k --------------------------------------------------------------------------------------- //用VBS获取XML节点 '定义变量 Dim FILE, NODE, VALUE Dim oShell, oExec, oStdOut, line '设置文件名和节点名 FILE = "onvif.xml" NODE = "http_port" '使用CMD命令获取节点值 Set oShell = CreateObject("WScript.Shell") Set oExec = oShell.Exec("cmd /c type " & FILE & " | find """ & NODE & """") Set oStdOut = oExec.StdOut '读取输出结果并提取值 Do While Not oStdOut.AtEndOfStream line = oStdOut.ReadLine If InStr(line, "<" & NODE & ">") > 0 Then VALUE = Split(line, "<" & NODE & ">")(1) VALUE = Split(VALUE, "</" & NODE & ">")(0) End If Loop '输出结果 MsgBox "端口号是:" & VALUE --------------------------------------------------------------------------------------- //用VBS获取XML节点方法2,不弹出CMD窗口 Dim fso, file, stream, str, node, value ' 创建 File System Object Set fso = CreateObject("Scripting.FileSystemObject") ' 打开文件 Set file = fso.OpenTextFile("onvif.xml", 1) ' 读取文件内容 str = file.ReadAll ' 关闭文件 file.Close ' 查找节点 node = "http_port" value = "" ' 循环查找节点并获取值 For Each line In Split(str, vbCrLf) If InStr(line, "<" & node & ">") > 0 Then value = Split(line, "<" & node & ">")(1) value = Split(value, "")(0) Exit For End If Next ' 输出结果 If value <> "" Then WScript.Echo "端口号是:" & value End If