Board logo

标题: [文本处理] 批处理如何从XML文件中提取需要的字符串(指定属性的值)? [打印本页]

作者: mxarky    时间: 2013-7-18 09:34     标题: 批处理如何从XML文件中提取需要的字符串(指定属性的值)?

本帖最后由 pcl_test 于 2017-4-30 08:14 编辑

各位大虾们,求助一段代码从文件中提取 name 和 UserAccount 字段后面的姓名输出到另外一个文本文件b.txt,一行一个姓名,排除重复的姓名
输出结果为:

刘欢
李杰云
张能星
...
Peter Huo
...
胡勇
刘倩
...
...
张艳


XML文件保存为a.txt文件内容如下:

<?xml version="1.0" encoding="GB2312"?>
<XXXXOrLater_Exported_Data_9D373D5B-AA39-42e7-A819-BF5F7A8961FC><Database><Sys_User><Item ID="8800" UserName="刘
欢" Pwd="997E59F2FB2E4AA92CD02FAA1364698600000000000000000000000000000000" PwdType="0" SmsID="" Name="刘欢"
UserType="0" Gender="1" Face="127" Mobile="" Email="" Phone="" ProfileOpenLevel="0" BuddyAuth="0"
LastLogonTime="0" UserRight="00000000000000000000000000000000" AccountState="" UserVersion="15963"
AuthType="0"/><Item ID="8888" UserName="李杰云"
Pwd="2293B69E6147F24D0FD2780D1875D38200000000000000000000000000000000" PwdType="0" SmsID="" Name="李杰云"
UserType="0" Gender="0" Face="126" Mobile="" Email="" Phone="" ProfileOpenLevel="0" BuddyAuth="0"
LastLogonTime="0" UserRight="00000000000000000000000000000000" AccountState="" UserVersion="15971"
AuthType="0"/><Item ID="8906" UserName="Peter"
Pwd="DEB723A9692EFB2C78EA58177921156F00000000000000000000000000000000" PwdType="0" SmsID="" Name="张星能"
UserType="0" Gender="0" Face="126" Mobile="" Email="" Phone="" ProfileOpenLevel="0" BuddyAuth="0"
LastLogonTime="0" UserRight="00000000000000000000000000000000" AccountState="" UserVersion="15851" AuthType="0"/>
...
省略N行
...
<Item ID="12707" UserName="胡勇1" Pwd="88A72B692FCD631C4483F190E487B58F00000000000000000000000000000000"
PwdType="0" SmsID="" Name="胡勇" UserType="0" Gender="0" Face="126" Mobile="" Email="" Phone=""
ProfileOpenLevel="0" BuddyAuth="0" LastLogonTime="0" UserRight="00000000000000000000000000000000" AccountState=""
UserVersion="17095" AuthType="0"/><Item ID="12708" UserName="朱琳"
Pwd="997E59F2FB2E4AA92CD02FAA1364698600000000000000000000000000000000" PwdType="0" SmsID="" Name="朱琳"
UserType="0" Gender="1" Face="127" Mobile="" Email="" Phone="" ProfileOpenLevel="0" BuddyAuth="0"
LastLogonTime="0" UserRight="00000000000000000000000000000000" AccountState="0" UserVersion="15540"
AuthType="0"/><Item ID="9938" Birthday="19830711" Age="25" BloodType="1" Constellation="0" College="" HomePage=""
Fax="" Address="" name="Orgstruct_0" base64encode="
eJwBDQzy80JRUVpJUAMMAAB4nJVb23NTxxl/z0z+B8bvtOemc5kJZKA0DA9Mk5bm
IW+00JaZBDowvfw7YCIbK8SSbEkcyZJ8la2rZR1L8hg3BdwYCIaACTfN2FOtbX3f
...
省略N行
...
9UEUM+8qDWyvSRom6ZidivFZ8GY6RlmWyhL+KcytRNTTbXDMgcMtCa2p8fqCN+Ni
dkxEa+ucNQ/+vXHy44/+D6VdBtfLrtvj"/></OrgstructFileInfo><TabFileInfo><File name="TabConfig.xml" base64encode="
eJyzsa/IzVEoSy0qzszPs1Uy1DNQUkjNS85PycxLt1VydzIyNjRSsrfj5bIJSUwq
VghLLQIqUrID8RSA2NPFVsnYwMBQSSEksyQn1VZp47KzP5QUQotybJUySkoKrPT1
...
省略N行
...
0MJ8g/Ch5O9n0CnmSxuT6cSIKEOQm4cL5WMMgV5aifdnAxjH6CDir1DMEoRwr/kj
DZrCMI5BMRGzvD2WG/wWe8961VAl7ZpzqG6TKEz3ek0Dcyn/O7InhVfQmwf5qbcj
2df0RRv+y4N/G3TWpfYSPIYxE8/BYQgT0Q05amHzH61bMrm0hq6lMcwk1mjcfwCv
4nB3"/></TabFileInfo><DisGroupDB><GroupBasicInfo><Item GroupID="10021" GroupGUID="{259AA190-6D14-4CDE-85A7-
C2C3EAEC41B1}" GroupName="事业群" GroupInitiator="" CreateTime="1310375684" GroupDescription=""
ParticipantCount="5" GroupType="0" BasicInfoVer="2434" UserListVer="2434" IsEffective="1" szExpand1=""
...
省略N行
...
GroupName="社团活动群" GroupInitiator="" CreateTime="1371437169" GroupDescription="" ParticipantCount="7"
GroupType="0" BasicInfoVer="2527" UserListVer="2527" IsEffective="1" szExpand1="" dwExpand1="0" dwExpand2="0"
dwExpand3="0"/></GroupBasicInfo><GroupUserList><Item Index="862" UserAccount="周鲲彬" GroupID="10021"
UserRight="0" szExpand1="" dwExpand1="0" dwExpand2="0"/><Item Index="863" UserAccount="刘倩倩" GroupID="10021"
UserRight="0" szExpand1="" dwExpand1="0" dwExpand2="0"/><Item Index="865" UserAccount="李林杰" GroupID="10021"
...
省略N行
...
GroupID="10027" UserRight="0" szExpand1="" dwExpand1="0" dwExpand2="0"/><Item Index="2412" UserAccount="邱彬水"
GroupID="10027" UserRight="0" szExpand1="" dwExpand1="0" dwExpand2="0"/><Item Index="2557" UserAccount="Peter
Huo" GroupID="10028" UserRight="0" szExpand1="" dwExpand1="0" dwExpand2="0"/><Item Index="2558"
UserAccount="Shannon Chen" GroupID="10028" UserRight="0" szExpand1="" dwExpand1="0" dwExpand2="0"/><Item
Index="3075" UserAccount="刘倩" GroupID="10046" UserRight="0" szExpand1="" dwExpand1="0" dwExpand2="0"/><Item  
...
省略N行
...
Index="3009" UserAccount="张艳" GroupID="10045" UserRight="0" szExpand1="" dwExpand1="0"
dwExpand2="0"/></GroupUserList><TotalGroupInfo><Item Index="0" CurrentGroupMaxVer="2586" TotalGroupCount="19"
MaxGroupID="10047" szExpand1="" dwExpand1="0"
dwExpand2="0"/></TotalGroupInfo></DisGroupDB><ClientConfig><ModifyAllowMarks><Item
ConfigName="SYSMSG_Stay_Time_In_Seconds" ConfigValue="10"/><Item ConfigName="SYSMSG_Never_Disappear"
ConfigValue="10"/><Item ConfigName="SYSMSG_Send_to_OnLine"
ConfigValue="10"/></SYSMSG_Config></ClientConfig></XXXXOrLater_Exported_Data_9D373D5B-AA39-42e7-A819-
BF5F7A8961FC>
作者: apang    时间: 2013-7-18 10:28

  1. @echo off&setlocal enabledelayedexpansion
  2. for /f "delims=" %%a in ('findstr /i "SmsID.*Name= Index.*UserAccount=" a.txt') do (
  3.     set "str=%%a"
  4.     set "str=!str:* name=!"
  5.     set "str=!str:* UserAccount=!"
  6.     for /f delims^=^=^" %%b in ("!str!") do (
  7.         if not defined %%b echo,%%b&set "%%b=1"
  8.     )
  9. )
  10. pause
复制代码

作者: mxarky    时间: 2013-7-18 10:45

回复 2# apang


谢谢 apang ,此代码如果是出来我贴出的数据没问题,但是实际文件将近 1MB,运行批处理时只弹出 cmd中 运行set 后的环境变量界面。

...
PROCESSOR_LEVEL=6
PROCESSOR_REVISION=2a07
ProgramFiles=C:\Program Files
PROMPT=$P$G
SESSIONNAME=Console
SystemDrive=C:
SystemRoot=C:\WINDOWS
TEMP=C:\DOCUME~1\XXX\LOCALS~1\Temp
TMP=C:\DOCUME~1\XXX\LOCALS~1\Temp
USERDOMAIN=FA-506D30E
USERNAME=XXX
USERPROFILE=C:\Documents and Settings\XXX
windir=C:\WINDOWS
pause
作者: apang    时间: 2013-7-18 11:08

回复 3# mxarky


    能否把你的数据样本传到网盘?我测试一下
作者: mxarky    时间: 2013-7-18 12:56

回复 4# apang

问题解决了,原来是要用UltraEdit对XML文件重新排版下。
作者: wskwfkbdn    时间: 2013-7-18 22:19

xml文件是需要转码成ansi,批处理才能处理




欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2