如何通过cmd/PowerShell读取xml值
我有以下XML格式 如何读取如何通过cmd/PowerShell读取xml值,xml,windows,powershell,cmd,Xml,Windows,Powershell,Cmd,我有以下XML格式 如何读取Tar路径的值 <?xml version="1.0" encoding="utf-8"?> <Foo xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <Bar Path="c:\program files\bar" /> <Tar Path="c:\program f
Tar路径的值
<?xml version="1.0" encoding="utf-8"?>
<Foo xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Bar Path="c:\program files\bar" />
<Tar Path="c:\program files\tar" Include="All" />
<Updates></Updates>
</Foo>
在本例中,我需要接收值:c:\ProgramFiles\tar
在Alex的评论之后,也可以在PowerShell脚本中执行此操作
我需要将这段代码添加到现有的.cmd文件中,是否可以从.cmd调用PowerShell脚本并将结果返回到cmd?PowerShell解决方案:
[xml]$xml=获取内容$args[0]
$xml.Bar.Tar.Path
VBScript解决方案:
Set xml=CreateObject(“Msxml2.DOMDocument.6.0”)
xml.async=False
xml.load WScript.Arguments(0)
如果xml.ParseError为0,则
WScript.Echo xml.ParseError.Reason
WScript.Quit 1
如果结束
设置path=xml.SelectSingleNode(“/Tar/Bar[@path]”)
WScript.Echo path.Value
在批处理文件中调用上述脚本,如下所示:
powershell -File "C:\path\to\your.ps1" "C:\path\to\your.xml"
cscript //NoLogo "C:\path\to\your.vbs" "C:\path\to\your.xml"
或者像这样:
powershell -File "C:\path\to\your.ps1" "C:\path\to\your.xml"
cscript //NoLogo "C:\path\to\your.vbs" "C:\path\to\your.xml"
PowerShell和VBScript都将结果写入标准输出。要将其分配回批处理变量,您需要像这样为循环设置一个(将省略号替换为上述命令之一):
您可以使用:
FOR /F "delims=" %%A IN ('xidel.exe -s "C:\path\to\your.xml" -e "pathvalue:=//Tar!@Path" --output-format^=cmd') DO %%A
ECHO %pathvalue%
在同一.bat文件中使用powershell查阅xml
SET ARXIU=c:\catalunya.xml
FOR /F "tokens=* USEBACKQ" %%F IN (`powershell "[xml]$xml = Get-Content %ARXIU% ; $xml.Foo.Tar.Path"`) DO (
SET RUTA=%%F
GOTO HAVE_VALUE
)
echo Error NOT have value
GOTO :EOF
:HAVE_VALUE
echo %RUTA%
建议使用除batch/cmd以外的任何其他方法,但很不幸,我需要cmd batchvbscript、jscript、powershell?全部从命令行运行想法是我想从现有的.cmd文件运行代码,是否可以从.cmd调用powershell脚本并将结果返回到我的cmd?你能分享一个代码示例吗?我不明白如何将其接收回cmd的最后一部分。我需要做什么来放省略号@user829174上述两个命令之一。powershell-File…
或cscript//NoLogo…
。