2

I have a xml file containing the xml header and xml footer and I want to remove the header and footer and store the content in a variable. The xml file's content changes inside a loop.

For example:

for (some range) do (
set "xmlHeader=<?xml version="1.0" encoding="UTF-8"?><Config  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0">"
set "xmlFooter=</Config>"

<then get and set variable from xml file>
)

The xml file contains:

<?xml version="1.0" encoding="UTF-8"?><Config  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0">
<tag1>info1 changes in the loop</tag1>
<tag2>info2 changes in the loop</tag2>
</Config>

And I want a variable in batch be storing

<tag1>info1</tag1>
<tag2>info2</tag2>

I've tried:

for (outer loop condition ) do (
for /f "Tokens=* Delims=" %%c in (xmlFile.xml) do set config=%%c
"!config:%xmlHeader%=!"
echo "!config!"
)

But the replace didn't do anything. Please help! Thank you.

Evan
  • 507
  • 2
  • 7
  • 21
  • Not very helpful, I know, but this is the kind of stuff [XSLT](http://en.wikipedia.org/wiki/XSLT) was invented for. – Gray Jul 29 '13 at 21:04
  • what is this: `changes in the loop` ? – Endoro Jul 29 '13 at 21:06
  • Manipulating XML in batch is tricky. You can easily break your code with unexpected `"`, `<`, ... I use `xmlstarlet` (xmlstar.sourceforge.net) for XML manipulation in batch. – LS_ᴅᴇᴠ Jul 30 '13 at 09:37
  • In this case, with xmlstarlet, `xml sel -t -c "/Config/node()" -n file.xml` – LS_ᴅᴇᴠ Jul 30 '13 at 09:54
  • Answer to Endoro. Changes in the loop means this text will change within the for loop. This is not that impo rtant to the text manipulation I am trying to do. So we can assume the string inside the tag is constant. – Evan Jul 30 '13 at 13:01

2 Answers2

3

try it with sed for Windows:

example:

    >type file
    <?xml version="1.0" encoding="UTF-8"?><Config  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0">
    <tag1>info1 changes in the loop</tag1>
    <tag2>info2 changes in the loop</tag2>
    </Config>

    >sed -r "\#xml version|</Config>#d; s#^(<[^>]+>\S+)[^<]+(<[^>]+>)#\1\2#" file
    <tag1>info1</tag1>
    <tag2>info2</tag2>
Endoro
  • 37,015
  • 8
  • 50
  • 63
  • Thanks again Endoro. This works for configs on different lines. How to do this when the input is on the same line for example: info1 changes in the loop info2 changes in the loop – Evan Jul 30 '13 at 15:21
  • I've tried another way to use sed. this works on Linux but don't do anything on Windows: set "xmlHeader=" sed -e "s@%xmlHeader%@@g" -i.backup xmlFile.xml any suggestions? – Evan Jul 30 '13 at 15:57
  • This wasn't the question. Please do not enhance questions. Make new ones. – Endoro Jul 30 '13 at 15:57
  • I've posted a new question using sed at: http://stackoverflow.com/questions/17952175/xml-string-replacement-in-windows-batch-file – Evan Jul 30 '13 at 16:23
  • Thanks Endoro. I was hoping you would provide further inputs on the case when xml is all on one line. But I will accept the answer you've given that address the case when they are on different lines. – Evan Jul 30 '13 at 17:07
2

Batch String Replacement

%Var:Term=Replace%

These are some rules you must follow when working with batch's string replacement

  • The string to be replaced (the search term) may not contain any of the following characters
    • Equals sign = (Because this is how it knows the search term ends)
  • The string to be replaced (the search term) may not begin with the following characters
    • Tilde ~ (Because this is a special character for batch strings)
    • Asterisk * (Because this is a special character for batch strings)

Solutions

See http://www.dostips.com/forum/viewtopic.php?f=3&t=2710 by DBenham for a 100% batch solution.

See https://stackoverflow.com/a/16735079/891976 by DBenham for a Batch/JScript hybrid solution.

See https://gist.github.com/DavidRuhmann/4608317 for a Proof of Concept of mine 100% batch.

Community
  • 1
  • 1
David Ruhmann
  • 11,064
  • 4
  • 37
  • 47