从批处理中解析xml [英] parsing xml from batch
问题描述
我需要解析此xml文件,并通过Windows Batch使用字段中的输入.我需要将所有值都放入变量中,我花了好几个小时不停地动脑筋,但无法提出任何有用的信息.有人可以帮忙吗?一个简单有用的提示就足够了.
i need to parse this xml file and use the input from the fields via a windows batch.i need to put all the values in variables, i am banging my head for so many hours but cant come up with anything useful. can someone please help ? A simple useful hint would suffice.
我需要一个遵循常规编程格式的答案,首先需要看到tag,然后是,然后如果有多个,我必须循环解析它们.
i need an answer which follows normal programming format as in first i need to see tag and then the and then if there are multiple i have to parse them in a loop.
<USERS>
<USER>
<USERNAME>FT_NRIAPIUSER </USERNAME>
<PASSWROD>XXXXXXXXXXXXX</PASSWROD>
<GROUPNAME>-</GROUPNAME>
<POLICYNAME>-</POLICYNAME>
<REMARKS>-</REMARKS>
</USER>
<USER>
<USERNAME>FT_SelfAdmin01</USERNAME>
<PASSWROD>XXXXXXXXXXXXX</PASSWROD>
<GROUPNAME>FT_SelfAdmins</GROUPNAME>
<POLICYNAME>-</POLICYNAME>
<REMARKS>-</REMARKS>
</USER>
</USERS>
<GROUPS>
<GROUP>
<GROUPNAME>FT_SelfAdmins</GROUPNAME>
<POLICIES>
<POLICY>
<POLICYNAME>-</POLICYNAME>
</POLICY>
</POLICIES>
<REMARKS>-</REMARKS>
</GROUP>
<GROUP>
<GROUPNAME>FT_SelfUsers</GROUPNAME>
<POLICIES>
<POLICY>
<POLICYNAME>-</POLICYNAME>
</POLICY>
</POLICIES>
<REMARKS>-</REMARKS>
</GROUP>
</GROUPS>
推荐答案
@ECHO Off
SETLOCAL
::
:: remove variables starting $
FOR /F "delims==" %%a In ('set $ 2^>Nul') DO SET "%%a="
:: evaluate command line. Structure is
:: %1 : filename to be analysed
:: %2 : tag to signal "new data item"
:: %..: other REQUIRED tags
:: then repeat
:: /opt optional tags
:: /block start-analysis tag
:: /seq tags in required output-sequence
::
SET "$filename=%~1"
IF NOT EXIST "%$filename%" ECHO "%~1" NOT found&GOTO :EOF
SET "$mode=tag"
:tagloop
SHIFT
SET "$=%~1"
IF NOT DEFINED $ GOTO process
IF %$:~0,1%==/ (SET "$mode=%$:~1%") ELSE (CALL SET "$%$mode%=%%$%$mode%%% %~1")
GOTO tagloop
:process
:: start mode OFF in block mode, on otherwise
:: establish tag1 (which signals start-of-next-data-item)
IF DEFINED block (SET "$mode=") ELSE (SET "$mode=Y")
FOR %%z IN (%$tag%) DO IF NOT DEFINED $tag1 SET "$tag1=%%z"
:: default output sequence is requiredtags optionaltags
IF NOT DEFINED $seq SET "$seq=%$tag% %$opt%"
CALL :zapvars
FOR /f "usebackqtokens=*" %%L IN ("%$filename%") DO (
FOR /f "tokens=1-3delims=<>" %%a IN ("%%L") DO IF "%%c"=="" (CALL :lonely "%%a") ELSE (CALL :triplet "%%a" "%%b" "%%c")
)
)
GOTO :eof
:: One parameter on line - may be start/end of block
:lonely
FOR %%p IN (%$block%) DO IF /i "%%p"=="%~1" (SET "$mode=Y"&CALL :zapvars) ELSE (
IF /i "/%%p"=="%~1" CALL :output&SET "$mode=")
GOTO :eof
:: presume 3 elements - tag data endtag
:triplet
FOR %%p IN (%$tag1%) DO IF /i "%~1"=="%%p" CALL :output
FOR %%p IN (%$tag% %$opt%) DO IF /i "%~1"=="%%p" SET "$$%%p=%~2"
GOTO :eof
:output
IF NOT DEFINED $mode GOTO zapvars
:: build line for outputting - IF all the required elements are present
FOR %%p IN (%$tag%) DO IF NOT DEFINED $$%%p GOTO zapvars
SET "$line="
FOR %%p IN (%$seq%) DO IF DEFINED $$%%p (CALL SET "$line=%%$line%%,%%$$%%p%%") ELSE (CALL SET "$line=%%$line%%,""")
ECHO %$line:~1%
GOTO zapvars
:zapvars
FOR %%z IN (%$tag% %$opt%) DO SET "$$%%z="
GOTO :eof
好吧-可能有一些漏洞-所以出于实验目的"
Well - likely there's some holes in this - so "for experimental purposes"
以 thisbatch 文件名requiredtags
Run it as thisbatch filename requiredtags
如果有可选标签,请添加/opt可选标签
If there are optional tags, then add /opt optionaltags
要设置输出字段序列,请添加/seq标签以输出-默认情况下,请使用requiredtags + optionaltags
To set the output field sequence, add /seq tagsrequiredtobeoutput - by default, use requiredtags+optionaltags
要仅激活 starttag 和/starttag 之间的输出,然后添加/block标签
To activate output only between starttag and /starttag then add /block tag
处理相当简单.标签名称内置在以$
-$ tag,$ seq等开头的envvars中.假定这些行是<tag>
,</endtag>
或<tag>data</endtag>
.
The processing is reasonably simple. The tag names are built into envvars starting $
- $tag, $seq etc. The lines are assumed to be either <tag>
, </endtag>
or <tag>data</endtag>
.
除此之外,该过程由$mode
(设置为输出时)控制,该$mode
通过对block
标签的处理进行切换.数据被累积到envvars $$tagname
中,并且仅在设置了$mode
swich并且有完整的required
标签集时才输出.第一个required
标签是特殊的,它控制何时开始新的数据项(并且开始新的数据项意味着输出正在累积的前一项)
Beyond that, the process is controlled by $mode
(when set : output) which is switched by the processing of a block
tag. Data is accumulated into envvars $$tagname
and will only be output if the $mode
swich is set and there is a full set of required
tags. The first required
tag is special, it controls when a new data item is started (and starting a new data item implies that the previous item being accumulated gets output)
因此,命令
thisbatch q28540123.txt username passwrod /opt policyname groupname /seq policyname groupname username passwrod /block users
将提取数据<users>..</users>
,需要username passwrod
以及可选的policyname groupname
,并且在username
上的每个数据中断处(首先提到的必需元素)将显示顺序为policyname groupname username passwrod
would extract data <users>..</users>
, requiring username passwrod
and optionally policyname groupname
and at each databreak on username
(the first-mentioned required element) would show data in the sequence policyname groupname username passwrod
这篇关于从批处理中解析xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!