1

I've been searching for a similar question to avoid asking already solved questions, but I haven't found a valid answer for me, sorry if there's one and I haven't seen it. I'm reading an XML file from Jquery, the xml file has some CDATA stuff that I have purposely added and that I would like to preserve for formatting purposes:

<?xml version="1.0" encoding="UTF-8"?>
<categories>
    <description name="whatever">
        Blah, blah, blah<![CDATA[<br />]]>blah, blah, blah
    </description>
</categories>

After reading it, I append it to a div:

$(xml).find('description[name="whatever"]').each(function()
{       
    $(container).append($(this));
});

What I've got is that Jquery seems to scape the '<' and '>' so I finally have:

Blah, blah, blah&l;tbr /&gt;blah, blah, blah

I have already tried to force the appending to treat the content as text like this:

$(container).text($(this));

But then I get:

[Object object]

And if I do .html() I get the same result as with the .append()...

2 Answers 2

2

Ordinary XML processors have no way of knowing whether anything they process came from a CDATA section; as far as they know, any CDATA content is just plain text. As such, when jQuery processes CDATA stuff, it isn't going to treat it any differently than stuff that appeared in a string literal (in fact, since jQuery is simply a layer over the DOM, it has absolutely no way of knowing whether its input came from a CDATA section or a string literal, since the DOM doesn't make any such distinction).

Sign up to request clarification or add additional context in comments.

1 Comment

That's a good pill of knowledge full of sense, thank you for the info, then I should change my question on, how can I insert HTML inside an XML in order to be understood as HTML by Jquery?
1

For the folks that may fall in the same hole in the future:

First of all, declare it as XML:

$.get("yourxml.xml", {}, function (data) 
{            

},'xml');

Afterwards, introduce it like text:

$(xml).find('description[name="whatever"]').each(function()
{       
    $(container).append($(this).text());
});

This is working for me like a charm!

1 Comment

If it's supposed to be HTML, you should be using .html()... :-?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.