473,385 Members | 2,005 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Strip CDATA with regex

Hi,

Can sombody here please help me a bit with a regular expression.
I have a xml file where I need to strip the CDATA sections of any
contained data.

Eg.
<xml>
<tag><[CDATA[ some data ]]></tag>
<tag><[CDATA[ some more data ]]></tag>
</xml>

Should end up like this:
<xml>
<tag><[CDATA[]]></tag>
<tag><[CDATA[]]></tag>
</xml>

Now, I have the start and end of the range
(\[CDATA\[)
and
(\]\]>)

But I cannot figure out how I match any character that is not like the
end of the range.

That is > is ok, ] is ok
but ]]> is not ok.

Thanks in advance,
Balaras
Jul 23 '05 #1
3 6151


Balaras wrote:

Can sombody here please help me a bit with a regular expression.
I have a xml file where I need to strip the CDATA sections of any
contained data.

Eg.
<xml>
<tag><[CDATA[ some data ]]></tag> It should be
<![CDATA[ <tag><[CDATA[ some more data ]]></tag>
</xml>

Should end up like this:
<xml>
<tag><[CDATA[]]></tag>
<tag><[CDATA[]]></tag>
</xml>


How about parsing the XML into a DOM document and then manipulating
those CDATA section nodes and serializing back, Mozilla example:

var xmlMarkup = [
'<xml>',
'<tag><![CDATA[ some data ]]></tag>',
'<tag><![CDATA[ some more data ]]></tag>',
'</xml>'
].join('\r\n');

var xmlDocument = new DOMParser().parseFromString(xmlMarkup,
'application/xml');

var tagElements = xmlDocument.getElementsByTagName('tag');
for (var i = 0; i < tagElements.length; i++) {
var cdataSection = tagElements[i].firstChild;
if (cdataSection.nodeType == 4) {
cdataSection.data = '';
}
}

var newXmlMarkup = new XMLSerializer().serializeToString(xmlDocument);

That yields

<xml>
<tag><![CDATA[]]></tag>
<tag><![CDATA[]]></tag>
</xml>
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #2
Thanks Martin,

Actually I posted this to c.l.javascript by accident, it was ment for a
php group. I have to do some preprocessing before the xml is sent to the
client.

However your post helped me in another manner :)

var newXmlMarkup = new XMLSerializer().serializeToString(xmlDocument);


I did not know about the XMLSerializer, and I need it :)

Does IE have an equivallent or does a .innerHTML return valid xml ?

/Balaras
Jul 23 '05 #3


Balaras wrote:

var newXmlMarkup = new XMLSerializer().serializeToString(xmlDocument);


I did not know about the XMLSerializer, and I need it :)

Does IE have an equivallent or does a .innerHTML return valid xml ?


An XML DOM document (or any XML DOM node) with IE has a property named
xml which gives you the serialized markup so with IE/MSXML you can use
xmlDocument.xml
to get the markup.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Dr Clue | last post by:
I'm not really an expert with RegExp() , although I do use it. The problem I have is that I want to strip comments out of a CSS file using RegExp() The reason is that I'm loading and parsing to...
6
by: Mark Miller | last post by:
I have a scheduled job that uses different XSL templates to transform XML and save it to disk. I am having problems with the code below. The problem shows up on both my development machine (Windows...
3
by: Howard Dean | last post by:
I have a string with several carriage returns in it. For example: "This is my test string" I wish to convert it to "This is my test string" (remove all carriage returns. Can someone tell me...
3
by: Michal A. Valasek | last post by:
Hello, I want to transform text with HTML markup to plain text. Is there some simple way how to do it? I can surely write my own function, which would simply strip everything with < and >....
2
by: Daniel M. Hendricks | last post by:
I'm looking for a function/regex in C# to strip unwanted HTML tags from comments posted to my web site. Previously, it was written in PHP and I used this function to strip unwanted tags: ...
3
by: John A Grandy | last post by:
Let's say you are storing lots of URLs in XML files. So, for those with non-XML-compliant characters you use the <!]> construct ... <Element1><!]></Element1> Does .NET provide an intrinsic...
2
by: Jared | last post by:
Hi I have been trying to find an easy way to strip the hyperlinks out of an html string to make exporting ASP.NET GridView's to Excel a bit more user friendly. Couldn't find anything in these...
4
by: Steve | last post by:
Hi, I'm a complete PHP n00b slowly finding my way around I'm using the following function that I found on php.net to strip out html and return only the text. It works well except for when you...
1
by: Dariusz Tomoń | last post by:
Hi, I have got xml document with CDATA sections containing special characters like links to images. All Iwant is to display the content in my div section. I tried like this: protected...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.