Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8149501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:51:55+00:00 2026-06-06T14:51:55+00:00

I have an XML file and I need to extract testname from all the

  • 0

I have an XML file and I need to extract

testname

from all the instances of

<con:testSuite name="testname" 

within the XML file.

I am not quite sure how to approach this, or whether this is possible in batch.

Here is what I have thought so far:

1) Use FINDSTR and store every line that has

<con:testSuite name=

in a variable or a temporary file, like this:

FINDSTR /C:"<con:testSuite name=" file.xml > tests.txt

2) Somehow use that file or variable to extract the strings

Note that there might be more than one instance of the matching string in the same line.

I am a novice at batch and any help is appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T14:51:56+00:00Added an answer on June 6, 2026 at 2:51 pm

    Parsing XML is very painful with batch. Batch is not a good text processor to begin with. However, with some amount of effort you can usually extract the data you want from a given XML file. But the input file could easily be rearranged into an equivalent valid XML form that will break your parser.

    With that disclaimer out of the way…

    Here is a native batch solution

    @echo off
    setlocal disableDelayedExpansion
    set input="test.xml"
    set output="names.txt"
    
    if exist %output% del %output%
    for /f "delims=" %%A in ('findstr /n /c:"<con:testSuite name=" %input%') do (
      set "ln=%%A"
      setlocal enableDelayedExpansion
      call :parseLine
      endlocal
    )
    type %output%
    exit /b
    
    :parseLine
    set "ln2=!ln:*<con:testSuite name=!"
    if "!ln2!"=="!ln!" exit /b
    for /f tokens^=2^ delims^=^" %%B in ("!ln2!") do (
      setlocal disableDelayedExpansion
      >>%output% echo(%%B
      endlocal
    )
    set "ln=!ln2!"
    goto :parseLine
    

    The FINDSTR /N option is only there to guarantee that no line begins with a ; so that we don’t have to worry about the pesky default FOR “EOL” option.

    The toggling of delayed expansion on and off is to protect any ! characters that may be in the input file. If you know that ! never appears in the input, then you can simply setlocal enableDelayedExpansion at the top and remove all other setlocal and endlocal commands.

    The last FOR /F uses special escape sequences to enable the specification of a double quote as a DELIM character.

    Answer to additional question in comment

    You cannot simply put the additional constraint in the existing FINDSTR command because it will return the entire line that has a match. Remember you said yourself, “there might be more than one instance of the matching string in the same line”. The first name might start with the correct prefix, and the 2nd name on the same line might not. You only want to keep the one that starts appropriately.

    One solution is to simply change the echo(%%B >>%output% line as follows:

    echo(%%B|findstr "^lp_" >>%output%
    

    The FINDSTR is using a regular expression meta-character ^ to specify that the string must start with lp_. The quotes have already been removed at this point, so we don’t have to worry about them.

    However, you may run into a situation in the future where you must include " in your search string. Plus it might be marginally faster to include the lp_ screen in the initial FINDSTR so that :parseLine is not called unnecessarily.

    FINDSTR requires that search string double quotes are escaped with a back slash. But the Windows CMD processor also has its own rules for escaping. Special characters like > need to be either quoted or escaped. The original code used quotes, but you want to include a quote in the string, and that creates unbalanced quotes in your command. Windows batch generally likes quotes in pairs. At least one of the quotes must be escaped for CMD as ^". If the quote needs to be escaped for both CMD and FINDSTR, then it looks like \^".

    But any special characters within the string that are no longer functionally quoted from a CMD perspective must be escaped using ^ as well.

    Here is one solution that escapes all special characters. It looks awful and is very confusing.

    for /f "delims=" %%A in ('findstr /n /c:^"^<con:testSuite^ name^=\^"lp_^" %input%') do (
    

    Here is another solution that looks much better, but it is still confusing to keep track of what is escaped for CMD and what is escaped for FINDSTR.

    for /f "delims=" %%A in ('findstr /n /c:"<con:testSuite name=\"lp_^" %input%') do (
    

    One way to keep things a bit simpler is to convert the search into a regular expression. A single double quote can be searched using [\"\"]. It is a character class expression that matches either a quote or a quote – silly I know. But it keeps quotes paired so that CMD is happy. Now you don’t have to worry about escaping any characters for CMD, and you can concentrate on the regex search string.

    for /f "delims=" %%A in ('findstr /nr /c:"<con:testSuite name=[\"\"]lp_" %input%') do (
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this XML file I need to extract the HTML Code from mono
I have an XML file, I need to extract values from it, and put
I have an XML file consisting of Name/Value pairs that I need in a
I’m trying to extract the name and satelite coordinations from a XML file and
I need to extract information from iTunes Music Library.xml file. Since the xml is
In my Android app I need to extract data from a xml file (the
I have string of date from xml file of such kind: '2010-09-09T22:33:44.OZ' I need
I have an XML file which I need to parse using PHP and send
i have an xml file sitemap.xml as shown below ..i need to add one
HI I have an xml file with 500KB size which i need to send

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.