Remove the tags from within an xml file

June 6, 2011 at 07:03:32
Specs: Windows 7
So my code looks like this.

@Find "vmx" "C:\Documents and Settings\All Users\Application Data\VMware\VMware Server\hostd\vmInventory.xml" > vmxPath.txt

I am taking the xml file and taking two lines from it and inserting it into vmxPath.txt

I want to now be able to remove the xml tags from this text file. Forgive me, I am a n00b at batch scripting.

See More: Remove the tags from within an xml file

Report •

June 6, 2011 at 14:36:17
It's not very pretty:


:: Loop through text file
FOR /F "tokens=* delims=" %%A IN (vmxPath.txt) DO (

    :: Format xml tags to work with batch
    SET xml=%%A
    SET xml=!xml:^<= {!
    SET xml=!xml:^>=} !

    :: Look for tags that are not comments
    ECHO.comment !xml!|FINDSTR "\<{?">NUL

        :: Look for all tags
        ECHO.open1 !xml!|FINDSTR "\<{">NUL
        IF !ERRORLEVEL! EQU 0 (
            :: Initialize/nullify the data variable
            SET data=
            :: Loop through xml line, one word at a time
            FOR %%B IN (!xml!) DO (
                :: Look for close tags
                ECHO %%B|FINDSTR "\<{/">NUL
                IF !ERRORLEVEL! EQU 0 (
                    :: Trim leading spaces
                    CALL :Trim_leading_space !data!
                    :: Only display data that contains a value
                    IF DEFINED data ECHO Data = "!data!"
                ) ELSE (
                    :: Look for open tag 
                    ECHO %%B|FINDSTR "\<{">NUL
                    :: Concatenate the value to the data variable
                    IF !ERRORLEVEL! NEQ 0 (
                        SET data=!data! %%B

SET data=%*

Given this data in "vmxPath":

<?xml version="1.0" encoding="iso-8859-1"?>
<!-- Edited by XMLSpy® -->
    <TITLE>Empire Burlesque</TITLE>
    <ARTIST>Bob Dylan</ARTIST>
    <TITLE>Hide your heart</TITLE>
    <ARTIST>Bonnie Tyler</ARTIST>
    <TITLE>Greatest Hits</TITLE>
    <ARTIST>Dolly Parton</ARTIST>
    <TITLE>Still got the blues</TITLE>
    <ARTIST>Gary Moore</ARTIST>
    <COMPANY>Virgin records</COMPANY>

the result looks like this:

Data = "Empire Burlesque"
Data = "Bob Dylan"
Data = "USA"
Data = "Columbia"
Data = "10.90"
Data = "1985"
Data = "Hide your heart"
Data = "Bonnie Tyler"
Data = "UK"
Data = "CBS Records"
Data = "9.90"
Data = "1988"
Data = "Greatest Hits"
Data = "Dolly Parton"
Data = "USA"
Data = "RCA"
Data = "9.90"
Data = "1982"
Data = "Still got the blues"
Data = "Gary Moore"
Data = "UK"
Data = "Virgin records"
Data = "10.20"
Data = "1990"

When your only tool is a hammer, every problem looks like a nail.

Report •
Related Solutions

Ask Question