2011-11-21 45 views
1

* *我需要一個批處理文件,它只檢索數據標記值(不帶標記名稱)並將其寫入.txt文件中。該文件可能具有比列出的更多的XML標籤。提取特定XML標記值的批處理文件

所以輸出應該是:

資本利得在US--收入差距和落後冠軍力的關鍵因素將我們的經濟系統的所有口頭禪。如果你想出去,甚至在美國的盈利能力,必須提高15%的資本利得稅**

我的文件是這樣的:**

<TABLE> 
Table 30 
<ROW> 
Multiple Rows 
<DATA> 
Capital gains are the key ingredient of income disparity in the US-- and the force 
behind the winner takes all mantra of our economic system. If you want even out 
earning power in the U.S, you have to raise the 15% capital gains tax. 
</DATA> 
</ROW> 
</TABLE> 
+4

你試過什麼?你的代碼在哪裏失敗? – jeb

回答

1

我沒有窗戶機器,所以如果語法稍微有點寬恕,但是這樣的事情可能會有所幫助,如果數據與示例中列出的一樣,但您可能要考慮使用Powershell,因爲它具有用於處理XML的出色工具:

setlocal enabledelayedexpansion 
set start_reading="0" 
set stop_reading="0" 
set your_file_name=%~1 

if EXIST "%your_file_name%.txt" del "%your_file_name%.txt" 

for /f "eol=; tokens=1 delims=" %%c in ('type "%your_file_name_here%.xml"') do (
    set line=%%c 

    @REM Determine if at start of Data Tag 
    for /f "eol=; tokens=1 delims=" %%d in ('echo !line! ^| findstr /i /c:"<DATA>"') do (
    set start_reading="1" 
) 

    @REM Determine if at end of Data Tag 
    for /f "eol=; tokens=1 delims=" %%d in ('echo !line! ^| findstr /i /c:"</DATA>"') do (
    set stop_reading="1" 
) 

    @REM stop reading DATA tag input 
    if "!stop_reading!"=="1" (
    set start_reading="0" 
) 

    @REM skips first line assumed to be <DATA> 
    if "!start_reading!"=="2" (
    echo !line! >> "%your_file_name_here%.txt" 
) 

    @REM Ready to start reading post <DATA> line 
    if "!start_reading!"=="1" (
    set stop_reading="0" 
    set start_reading="2" 
) 

) 

@REM Check results 
type "%your_file_name_here%.txt" 

讓我知道你是否需要幫助。我不得不在DOS環境下工作,DOS是他們讓我們使用的所有環境,所以我感到你的痛苦。祝你好運! :)

相關問題