user1914896
user1914896

Reputation: 43

Escaping xml tags inside xml

I have python script that parse xml file and execute commands. I want my xml to read another xml file and edit it. For that i used perl command

"perl -pi -0777 -e 's@<en:TagName>.*?</en:TagName>@<en:TagName>new-value</en:TagName>@sg'FileName.xml"

This i have added in my xml file that is parsed by shell script.

<cmd>perl -pi -0777 -e 's@<en:TagName>.*?</en:TagName>@<en:TagName>new-value</en:TagName>@sg'FileName.xml</cmd>

here my python script is treating <en:TagName> as a tag and is not able to parse it. So i added "perl -pi -0777 -e 's#&lt;en:TagName&gt;.*?&lt;en:TagName&gt;#&lt;en:tagName&gt;new-value&lt;/en:TagName&gt;#sg' FileName.xml" and also used "#" as delimiter(as @ is special character in shell script that we are using)

Putting # is not able to provide desired result. Instead of replacing it is appending new value at the end.

Upvotes: 4

Views: 346

Answers (1)

RobEarl
RobEarl

Reputation: 7912

Surround your perl commands with CDATA tags:

Some text, like JavaScript code, contains a lot of "<" or "&" characters. To avoid errors script code can be defined as CDATA.

Everything inside a CDATA section is ignored by the parser.

A CDATA section starts with "<![CDATA[" and ends with "]]>"

Upvotes: 4

Related Questions