0

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?

4
  • Using anything other than batch/cmd would be recommended Commented May 6, 2015 at 11:25
  • True, however unfortunately I need cmd batch Commented May 6, 2015 at 11:28
  • vbscript, jscript, powershell? All run from the command line Commented May 6, 2015 at 11:29
  • The idea is that I want to run the code from an existing .cmd file, is it possible to call a powershell script from the .cmd and get the result back to my cmd? can you share a code example Commented May 6, 2015 at 11:35

3 Answers 3

3

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
Sign up to request clarification or add additional context in comments.

2 Comments

I did not understand the last part of how to receive it back to my cmd. what do I need to put the the ellipsis... ?
@user829174 One of the two aforementioned commands. Either powershell -File ... or cscript //NoLogo ....
0

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%

Comments

0

You could use the XML/HTML/JSON parser for that in cmd:

xidel -s "input.xml" -e "//Tar/@Path"
c:\program files\tar

If you want to export the output to a variable, you could do:

FOR /F "delims=" %A IN ('
  xidel -s "input.xml" -e "//Tar/@Path"
') DO SET pathvalue=%A

Or let xidel do it for you:

FOR /F "delims=" %A IN ('
  xidel -s "input.xml" -e "pathvalue:=//Tar/@Path" --output-format^=cmd
') DO %A

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.