Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 379 Vote(s) - 3.61 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Parse XML file for attribute from batch file

#1
I am parsing a XML file like below:

<?xml version="1.0"?>
<!--
-->
<configuration>
<settings>
<connections>
<connection name="name1" value="connection1" type="abc"/>
<connection name="name2" value="connection2" type="def"/>
</connections>
</settings>
</configuration>

From the batch file, I prompt the user for connection name. I want to parse the XML get a connection with the specified name and get its value. So If user gives name1, I want to select connection1. I had the below code from

[To see links please register here]


I am not familiar with for loop in (especially delimits, tokens) batch file, so I am not sure how this works and how to make it work for me.

(for /F "tokens=1,2 delims== " %%a in (connection.config) do (
if "%%~b" neq "" set %%a=%%~b
if /I "!name!" equ "%name%" echo !value!
))


Reply

#2
It works, if you use the right tokens and delimiters:

@echo off&setlocal
for /F tokens^=2^,3^,5delims^=^<^"^= %%a in (connection.config) do (
if "%%a" equ "connection name" echo(%%b %%c
)

Output is:

name1 connection1
name2 connection2
Reply

#3
@ECHO OFF
SETLOCAL
SET "name=name1"
SET "connection="
SET "type="

for /F "tokens=5,7delims==/ " %%a in (
'findstr /c:"<connection name=\"%name%\"" ^<connection.config'
) do SET connection=%%~a&SET type=%%~b

ECHO connection=%connection%
ECHO TYPE =%type%

Finding the data line which contains the literal string "<connection name=\"%name%\"" (where `\"` escapes `"`) then set connection to the 5th (and type for good measure) from the seventh token of the data line

<connection name="name1" value="connection1" type="abc"/>

using `=`, `/` and `[space]` as delimiters.
Reply

#4
Here's the [**xpath.bat**][1] -small script that will allow you to get a xml values by xpath expression without using external binaries:

call xpath.bat "connection.config" "//connection/@name"
call xpath.bat "connection.config" "//connection/@value"

to assign this to a variable:

for /f "tokens=* delims=" %%# in ('xpath.bat "connection.config" "//connection/@value"') do set "connection_value=%%#"
echo %connection_value%

[1]:

[To see links please register here]



Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through