用批处理文件解析xml

时间:2017-05-18 17:56:00

标签: xml string batch-file extract

尝试从xml文件中提取两个信息字符串。在示例linkage.xml文件中,我希望运行批处理文件并创建一个目录Smith-John-yyyy-mm-dd。到目前为止,我能够生成一个日期格式为我想要的文件夹(yyyy-mm-dd),但我无法提取“Smith”和“John”作为变量。非常感谢帮助。

linkage.xml

<LinkageParameter> 
   <Patient LastName="Smith" FirstName="John" ChartNumber="123">
      <Birthday>18/12/1972</Birthday>
      <Address>123 Main St, Salem, OR</Address>
      <ZipCode>97302</ZipCode>
      <Phone>(503)363-5432</Phone>
      <Mobile>(503)215-3215</Mobile>
      <SocialID>123456789</SocialID>
      <Gender>Male</Gender> 
   </Patient>
</LinkageParameter>

我的folder.bat文件

@echo off
for /f %%i in ('xml.exe sel -t -v "//LastName" CP.xml') do set var=%%i
for /f %%i in ('xml.exe sel -t -v "//LastName" CP.xml') do set var=%%j

setlocal enabledelayedexpansion

:: Extract date fields - language dependent
for /f "tokens=1-4 delims=/-. " %%i in ('date /t') do (
        set v1=%%i& set v2=%%j& set v3=%%k
        if "%%i:~0,1%%" gtr "9" (set v1=%%j& set v2=%%k& set v3=%%l)

        for /f "skip=1 tokens=2-4 delims=(-)" %%m in ('echo.^|date') do (
            set %%m=!v1!& set %%n=!v2!& set %%o=!v3!
    )
)

:: Final set for language independency (English and Portuguese - maybe works for Spanish and French)
set year=%yy%%aa%
set month=%mm%
set day=%dd%

md \\192.168.0.34\OpenDentImages\PatientImages\CT\%i%-%j%-%year%-%month%-%day%

2 个答案:

答案 0 :(得分:1)

而不是使用xml.exe,只要xml文件与发布的文件类似,您就可以尝试:

@Echo Off
Set "Pre="
For /F Tokens^=2^,4Delims^=^" %%A In ('Find "<Patient"^<"linkage.xml"'
) Do Set Pre=%%A-%%B
If Not Defined Pre GoTo :EOF

在您提供了%Pre%

的情况下,变量Smith-John应该是

答案 1 :(得分:0)

尝试使用xpath.bat(无需安装外部二进制文件):

for /f "tokens=* delims=" %%a in ('xpath.bat linkage.xml "//Patient/@LastName"') do set "last_name=%%~a"

for /f "tokens=* delims=" %%a in ('xpath.bat linkage.xml "//Patient/@FirstName"') do set "first_name=%%~a"

echo %first_name%    %last_name%