在制作一个将xml文件转换为cpp映射的工具的过程中,我将其写在批处理文件下面。 xml文件主要包含{key,value},该工具用于将这些{key,value}转换为cpp中的映射,但是它不起作用,请参考我的实际输出和预期输出,请提供错误代码帮助我?
@ECHO off
title Convert_Tool_v1.0
setlocal enableDelayedExpansion
::create output folder
@RD /S /Q "output"
md "output"
::build "array" of folders to access each of them
set folderCnt=0
for /f "eol=: delims=" %%F in ('dir /b /ad *') do (
set /a folderCnt+=1
set "folder!folderCnt!=%%F"
)
::make cpp file for each values_XXX/strings.xml
for /l %%N in (1 1 %folderCnt%) do (
::echo %%N - !folder%%N!
call :MAKE_CPP_FILE !folder%%N!
)
del ".\output\output.cpp" /s /f /q
echo convert finished
::function to make cpp files
EXIT /B %ERRORLEVEL%
:MAKE_CPP_FILE
::echo current folder is: %~1
::create file
echo.>".\output\%~1.cpp"
::check each line of xml file, convert line and write to output files
@echo off
set "replace_from_1= \<\/string\>="
set "replace_to_1=\"},"
set "replace_from_2= \<string name\="
set "replace_to_2={"
set "replace_from_3= \"\>"
set "replace_to_3=\",\""
set "source=%~1\\strings.xml"
set "target=.\output\%~1.cpp"
setlocal enableDelayedExpansion
(
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" %source%') do (
set "line=%%b"
if defined line set "line=!line:%replace_from_1%=%replace_to_1%!"
::if line doesnt contain key, then don't convert this line
if errorlevel 1 (
set "line="
)
::continue convert key,value
if defined line set "line=!line:%replace_from_2%=%replace_to_2%!"
if defined line set "line=!line:%replace_from_3%=%replace_to_2%!"
echo(!line!
)
) > %target%
endlocal
EXIT /B 0
xml文件的内容如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Version : BDG14625.003.09.15-->
<!--C1 Strings-->
<!-- Start 20180409: Added font names as strings to handle font issues, as a suggested solution -->
<string name="font_name_Regular">GMSansUI_Regular</string>
<string name="font_name_Medium">GMSansUI_Medium</string>
<string name="font_name_Light">GMSansUI_Light</string>
<!-- End 20180409: Added font names as strings to handle font issues, as a suggested solution -->
<string name="audio_accessory_audio_short_ipc">Accessory Audio</string>
<string name="audio_action_unavailable_short_ipc">Action unavailable.</string>
<string name="audio_action_unavailable_while_indexing_short_ipc">Action unavailable while indexing.</string>
...
可能的预期输出映射cpp:
{"font_name_Regular","GMSansUI_Regular"},
{"font_name_Medium","GMSansUI_Medium"},
{"font_name_Light","GMSansUI_Light"},
{"audio_accessory_audio_short_ipc","Accessory Audio"},
{"audio_action_unavailable_short_ipc","Action unavailable."},
...
实际输出为:
line:={
line:={
line:={
line:={
line:={
line:={
line:={
line:={
line:={
line:={
line:={
line:={
非常感谢您!