I have the following XML format
How can I read the value of Tar Path
<?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>
In this example I need to receive the value: c:\\program files\\tar
Following Alex's comment, it is also possible to do it within a PowerShell script.
I need to add this piece of code into an existing .cmd file, is it possible to call a PowerShell script from the .cmd and get the result back to the cmd?
PowerShell solution:
[xml]$xml = Get-Content $args[0]
$xml.Bar.Tar.Path
VBScript solution:
Set xml = CreateObject("Msxml2.DOMDocument.6.0")
xml.async = False
xml.load WScript.Arguments(0)
If xml.ParseError <> 0 Then
WScript.Echo xml.ParseError.Reason
WScript.Quit 1
End If
Set path = xml.SelectSingleNode("/Tar/Bar[@Path]")
WScript.Echo path.Value
Call the above scripts in your batch file like this:
powershell -File "C:\path\to\your.ps1" "C:\path\to\your.xml"
or like this:
cscript //NoLogo "C:\path\to\your.vbs" "C:\path\to\your.xml"
Both the PowerShell and the VBScript write the result to STDOUT. To assign that back to a batch variable you need a for
loop like this (replace the ellipsis with one of the commands above):
for /f "tokens=*" %%p in ('...') do set pathvalue=%%p
You could use Xidel for that:
FOR /F "delims=" %%A IN ('xidel.exe -s "C:\path\to\your.xml" -e "pathvalue:=//Tar!@Path" --output-format^=cmd') DO %%A
ECHO %pathvalue%
Consult xml using powershell inside the same .bat file
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%
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.