read and modify field from XML

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
trikky
Posts: 2
Joined: 24 Jun 2015 09:35

read and modify field from XML

#1 Post by trikky » 24 Jun 2015 09:52

Hi guys,

I'm new to this forum. I've registered because I need some support for creating a simple batch process.

I'm not experienced with batch processing. I will try to explain what I'm looking for.

Given an XML file as the input parameter, the batch process has to:
- Find any tag called <FileName> in that XML file.
- Copy the value of such tag into a variable.
- Do something with such variable
- Change the value of the tag to another value.

The tag can be something like:

Code: Select all

<FileName>C:\Users\J M\LOGIS\NFC BA.jpg</FileName>


Could you help me in creating such task? I'm using Windows Vista

Cheers

ShadowThief
Expert
Posts: 1167
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: read and modify field from XML

#2 Post by ShadowThief » 24 Jun 2015 15:42

Properly formatted XML like

Code: Select all

<XML>
   <FileName>C:\Users\J M\LOGIS\NFC BA.jpg</FileName>
   <FileSize>355 MB</FileSize>
   <FileType>JPEG</FileType>
</XML>


or the nasty all-squished together kind like

Code: Select all

<XML><FileName>C:\Users\J M\LOGIS\NFC BA.jpg</FileName><FileSize>355 MB</FileSize><FileType>JPEG</FileType></XML>


Because if it's the first one, then you can use

Code: Select all

for /F "tokens=1-3 delims=<>" %%A in ('findstr "<FileName>" data.xml') do set file_name=%%C

which assumes that your data is in a file called data.xml and that there is a tab to the left of the <FileSize> tag. (Note that this will only get the value of the tag and store it to a variable; if you want to change the value, you're either going to have to rewrite the entire file, or you can try JREPL.bat.) If it's all squished together, I've got nothing; batch generally doesn't play nice with XML unless it's formatted pretty.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: read and modify field from XML

#3 Post by penpen » 25 Jun 2015 05:38

You could also use my "xslt.bat" to extract data out of xml files (example and xslt.bat):
http://www.dostips.com/forum/viewtopic.php?p=32941#p32941.

penpen

trikky
Posts: 2
Joined: 24 Jun 2015 09:35

Re: read and modify field from XML

#4 Post by trikky » 25 Jun 2015 12:48

Ey guys,

Thanks for your support.

Finally I've made it with VBscript. More adequate to what I'm looking for.

It's the nasty type of XML.

Here is a bit of a real example:

Code: Select all

<FileName>C:\Users\casa\Desktop\CreatePro\foto.bmp</FileName><Width>11100</Width><Height>2900</Height>


I'm going to have a look to xslt.bat , cause it can be useful for me.

Cheers

ALEXRIXON
Posts: 1
Joined: 25 Jun 2015 23:11

Re: read and modify field from XML

#5 Post by ALEXRIXON » 25 Jun 2015 23:14

You can reformat the xml by replacing the pattern >< with >\n\t< and then >\n\t</ with >\n</

Post Reply