获取与批处理文件的XML标记的内容(更新:也获取标签属性) [英] Get content of xml tags with batch file (UPDATED:Getting also tags attributes)

查看:590
本文介绍了获取与批处理文件的XML标记的内容(更新:也获取标签属性)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的file.xml

 <根和GT;
<城市>
        <&的cityName GT;巴黎及LT; /&的cityName GT;
        <&的cityName GT;伦敦 - LT; /&的cityName GT;
        <&的cityName GT; ...< /&的cityName GT;
        ...
< /城市>
<国家和GT;
        <国家名称>印度和LT; /国家名称>
        <国家名称>&JAPON LT; /国家名称>
        <国家名称> ...< /国家名称>
        ...
< /国家和GT;
<大陆GT&;
        < continentName型=缘>亚洲和LT; / continentName>
        < continentName型=缘>美< / continentName>
        < continentName式地缘> ...< / continentName>
        ...
< /大洲>
< /根>

我想用一个批处理文件来获得指定标签的所有内容,也是所有指定标签的属性(键和值),并在写入文件。例如,我在批处理文件中指定continentName,所以我想有一个file.txt的有:结果
型的地理亚洲结果
型的地理美洲结果
型的地理...

这是我的时刻(我只得到标签内容,而不是属性):

 关闭@echo
SETLOCAL enabledelayedexpansion ENABLEEXTENSIONS
集标记= continentName
设置f_xml = file.xml集TAG2 = /%标签%FOR / F令牌= 2-4delims =<>中%%一个在(%f_xml%)做(
 IF%%一个==标签%%IF%% C==%标签2%ECHO(%% B>> file.txt的


解决方案

修改解决方案变更为回应新规格

我改变了原来的批处理程序履行的新的的要求:

 关闭@echo
集标记= continentName
设置f_xml = file.xml
(FOR / F令牌= 3,4 delims =< = gt;中%%一个在('FINDSTR\\< /%标签%\\>中%f_xml%')也呼应%%〜一个%% b)> file.txt的

本项目工程的只有的与上面所示的.xml文件(我的意思是,在现阶段,我写了这个解决方案),以及与此请求的输出:

 地理亚洲
美国地理
地缘...

如果该文件被更改(如插入更多的属性,或将没有属性,或者在几行分裂continentName标签等),或输出改变,previous解决方案将无法工作。

I have the following "file.xml":

<root>
<cities>
        <cityName>paris</cityName>
        <cityName>london</cityName>
        <cityName>...</cityName> 
        ...         
</cities>
<countries>
        <countryName>india</countryName>
        <countryName>japon</countryName> 
        <countryName>...</countryName>  
        ...         
</countries>
<continents>
        <continentName type="geo">asia</continentName>
        <continentName type="geo">america</continentName>
        <continentName type"geo">...</continentName>
        ...     
</continents>
</root>

I would like to get with a batch file all the content of a specified tag and also all the specified tag's attributes(key and value) and write in a file. For example, I specify "continentName" in my batch file, so I would have a "file.txt" with :
type geo asia
type geo america
type geo ...

This is what I have for the moment (I only get tags content, not attributes) :

@echo off    
setlocal enabledelayedexpansion enableextensions
set tag=continentName
set f_xml=file.xml

set tag2=/%tag%

for /f  "tokens=2-4delims=<>" %%a in (%f_xml%) do (
 IF "%%a"=="%tag%" IF "%%c"=="%tag2%" ECHO(%%b>>file.txt
)

解决方案

EDIT: Solution changed as reply to new specifications

I changed the original Batch program to fulfill the new request:

@echo off
set tag=continentName
set f_xml=file.xml
(for /F "tokens=3,4 delims=<=>" %%a in ('findstr "\</%tag%\>" %f_xml%') do echo %%~a %%b) > file.txt

This program works only with the .xml file as shown above (I mean, at the moment I wrote this solution) and with this requested output:

geo asia
geo america
geo ...

If the file is changed (like inserting more attributes, or placing none attribute, or splitting the continentName tag in several lines, etc), or the output is changed, previous solution will not work.

这篇关于获取与批处理文件的XML标记的内容(更新:也获取标签属性)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆