4

I have a XML file Testing.Config with the following content:

<?xml version="1.0" encoding="utf-8"?>
<connectionStrings>
    <add name="name1" connectionString="user id=id1;password=password1;"/>
    <add name="name2" connectionString="user id=id2;password=password2;"/>
    <add name="name3" connectionString="user id=id3;password=password3;"/>
</connectionStrings>

I need parse this file and obtain id and password key-value pairs in an attribute of a particular tag identified by the provided attribute, for example "name=name1".

Example

Input:

 name=name1

Returns:

 id=id1
 password=password1
4
  • Is Unix* like shell is OK ? Commented Dec 3, 2014 at 22:33
  • Windows batch (.cmd) Commented Dec 3, 2014 at 22:34
  • Almost every version of windows has powershell.. consider doing it in powershell instead? Commented Dec 4, 2014 at 1:18
  • @Vikas Gupta. Yes, can be with both options. Commented Dec 4, 2014 at 12:10

3 Answers 3

4
@echo off

set "xml_file=test.xml"
set /p search_for=Enter name:

for /f "skip=2 tokens=3,9 delims=;= " %%a in ('find """%search_for%""" "%xml_file%"') do (

    set "name=%%~a"
    set "pass=%%b"
)

echo name : %name%
echo pass : %pass%

If all connectionStrings are on separated lines and every string is on one line.Change the location of the xml_file


You can also try the xpath.bat (better option according to me) -small script that will allow you to get a xml values by xpath expression without using external binaries:

call xpath.bat connection.xml "//add[@name = 'name1']/@connectionString"
Sign up to request clarification or add additional context in comments.

2 Comments

Is exactly what I wanted... Only a change: tokens=3,9 It should be: tokens=7,9 Because I need the value of ID Thank you!!!
Change: set /p search_for=Enter name:, by set /p "search_for=Enter name:". And """%search_for%""", by "%search_for%"
1

Since you indicated (in comments on the question) that powershell is also okay, put the following code in a script file (lets say Foo.ps1)

param
(
    [Parameter(Mandatory=$true)]
    [string] $ConfigFilePath,

    [Parameter(Mandatory=$true)]
    [string] $Name
)

([xml](Get-Content -LiteralPath $ConfigFilePath)).connectionStrings.add |
Where-Object {$_.name -eq $name} |
ForEach-Object {($_.connectionString -split ' ')[1] -split ';'}

and then run the script with parameters to get the output.

Comments

0

In case someone comes looking for this like I did... the xpath.bat linked by npocmaka works great, but not for file one network paths e.g. \server\foo\bar.xml. And I couldn't get Vikus's PS script to work for attributes. So... I managed to turn Vikus's PS into this:

param
(
    [Parameter(Mandatory=$true)]
    [string] $XMLFile,
    [Parameter(Mandatory=$true)]
    [string] $Xpath
)
[xml]$xml = Get-Content -Path $XMLFile
$value = Select-XML -xml $xml -xpath $Xpath
$value.ToString()

Which I then collapsed to this for use in a .cmd file:

set psc="(Select-XML -xml ([xml](Get-Content -Path %xmlfile%)) -xpath %xpath%).ToString()"
for /f %%a in ('powershell %psc%') do set myvar=%%a

Though it's worth noting that neither %xmlfile% nor %xpath% can have spaces. That requires escaping all the parentheses so that the powershell command doesn't have to be wrapped in double quotes:

for /F %a in ('powershell ^(Select-XML -xml ^([xml]^(Get-Content -Path "test.xml"^)^) -xpath "/Computers/Computer/Name/@id"^).ToString^(^)') do @echo %a

FWIW, they're all markedly slower that my original fragile monster:

@for /F eol^=^#^tokens^=2^,5^,7^ delims^=^<^>^" %%i in (%xmlfile%) do @(
    @if "%targetnodename% ATTR1=" == "%%i" (
        set myvar1=%%j
        set myvar1=%%k
    )
)

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.