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 •

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

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

:: 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
    IF !ERRORLEVEL! NEQ 0 (

        :: 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
                    )
                )
            )
        )
    )
)
EXIT /B

:Trim_leading_space
SET data=%*
GOTO :EOF

Given this data in "vmxPath":

<?xml version="1.0" encoding="iso-8859-1"?>
<!-- Edited by XMLSpy® -->
<CATALOG>
  <CD>
    <TITLE>Empire Burlesque</TITLE>
    <ARTIST>Bob Dylan</ARTIST>
    <COUNTRY>USA</COUNTRY>
    <COMPANY>Columbia</COMPANY>
    <PRICE>10.90</PRICE>
    <YEAR>1985</YEAR>
  </CD>
  <CD>
    <TITLE>Hide your heart</TITLE>
    <ARTIST>Bonnie Tyler</ARTIST>
    <COUNTRY>UK</COUNTRY>
    <COMPANY>CBS Records</COMPANY>
    <PRICE>9.90</PRICE>
    <YEAR>1988</YEAR>
  </CD>
  <CD>
    <TITLE>Greatest Hits</TITLE>
    <ARTIST>Dolly Parton</ARTIST>
    <COUNTRY>USA</COUNTRY>
    <COMPANY>RCA</COMPANY>
    <PRICE>9.90</PRICE>
    <YEAR>1982</YEAR>
  </CD>
  <CD>
    <TITLE>Still got the blues</TITLE>
    <ARTIST>Gary Moore</ARTIST>
    <COUNTRY>UK</COUNTRY>
    <COMPANY>Virgin records</COMPANY>
    <PRICE>10.20</PRICE>
    <YEAR>1990</YEAR>
  </CD>
</CATALOG>

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