0

I am trying to extract a element value from a xml output string using batch. Have used below code , it is working fine with fomatted xml lines and not on single xml string. For e.g:

Formatted xml:*

<?xml version="1.0" encoding="utf-16"?>
<DEVICE>
  <AGENT>
        <VERSION>2.0.0.2</VERSION>
        <CONNECTION>
            <LOCATION>US_NY</LOCATION>
            <SERVERIP>
                127.0.0.1
            </SERVERIP>
            <TCPPORT>
                5656
            </TCPPORT>
            <POLLINTERVAL>
                5
            </POLLINTERVAL>
        </CONNECTION>
    </AGENT>  
</DEVICE>

Batch script used:

@echo off 
setlocal enableextensions EnableDelayedExpansion 
set input="test.xml" 
for /F "tokens=2 delims=<>" %%I in ('type %input% ^|find "</password>"') do (
 set pwd=%%I echo !pwd! ) 

How to extract value from any unformatted xml string?

James Z
  • 12,209
  • 10
  • 24
  • 44
  • Batch script used: @echo off setlocal enableextensions EnableDelayedExpansion set input="test.xml" for /F "tokens=2 delims=<>" %%I in ('type %input% ^|find ""') do ( set pwd=%%I echo !pwd! ) – user9445473 Mar 05 '18 at 12:43
  • Since the file sample provided doesn't contain the target string, replication of your results in not possible. What do you mean by an "unformatted XML string"? What information do you want to retrieve from the samples you've provided? – Magoo Mar 05 '18 at 13:24
  • 1
    You should consider using a scripting language that has a native method to read and write XML. Jscript, Vbscript or Powershell. – Squashman Mar 05 '18 at 14:25

1 Answers1

0

Not sure what issue you faced
I used the below code and it works for me.

@echo off  
color a  
cls  
cd "D:\"  
type 2.txt  
echo.  
echo.  
for /f "delims=" %%x in (2.txt) do set Build=%%x  
pause >nul  
exit  

Here I have created a 2.txt file on my local drive and then used the batch script to read it. "2.txt" contains un-formatted single line xml string.

Please check if this also works for you.
If not, provide more details of issue you are facing.

James Z
  • 12,209
  • 10
  • 24
  • 44
atul goenka
  • 157
  • 4