0

One of the nodes of my XML file has HTML within a CDATA tag. I need to get the HTML out and render it on my page.

<student>
    <student_id>
        Tijuana
    </student_id>
    <student_classes>
        <![CDATA[
            <ul>
                <li>class 1</li>
                <li>class 2</li>
            </ul>
        ]]>
    </student_classes>
</student>

If I try to get the node text, it removes all the HTML and just gives me plain text.

$.ajax({
    type: "GET",
    url: "students.xml",
    dataType: "xml",
    success: xmlParser
});

function xmlParser(xml) {
    //Returns a string like "class 1 class 2"
    var blah = $(xml).find('student_classes').text()); 
}

What I need is to get the HTML inside of the CDATA tag so I can actually use it on my page. How can I do this? Using Javascript or jQuery. Here is what I need from the student_classes node:

<ul>
    <li>class 1</li>
    <li>class 2</li>
</ul>
KidBilly
  • 3,408
  • 1
  • 26
  • 40
  • I can't reproduce the problem. I get an error because of your extra `})` and if I fix that and add code to do *something* with the return value of `text()` but it includes all the data. Likely the fault lies with what you are doing with that value … which you didn't include in the question. This is why you should provide a [mcve]! See also: [ask] – Quentin Feb 10 '21 at 17:32
  • That part of the question isn't really the point. :S But i'll fix it – KidBilly Feb 10 '21 at 17:34
  • It really is the point. I get the HTML source code out when I use `text()`. The problem you describe does not exist. – Quentin Feb 10 '21 at 17:36
  • Is there any way you can show me? I'm not sure what I'm doing wrong. – KidBilly Feb 10 '21 at 17:37
  • Re edit: Now it throws `Uncaught SyntaxError: Unexpected token ')'`. If I fix that and look at the `blah` variable then it contains the HTML source code. – Quentin Feb 10 '21 at 17:38
  • Does this answer your question? [parse html inside cdata using jquery or javascript](https://stackoverflow.com/questions/14961044/parse-html-inside-cdata-using-jquery-or-javascript) – Heretic Monkey Feb 10 '21 at 17:38
  • "I'm not sure what I'm doing wrong." — We can't tell what you are doing wrong either because you haven't provided a [mcve] – Quentin Feb 10 '21 at 17:38
  • @Quentin If he's trying to use the HTML, then he needs to basically without hardcoding the XML in my example. Append it to some elements, like I did correct? – ABC Feb 10 '21 at 17:42
  • @File — It depends on what he's trying to achieve, which is why I've been asking for a [mcve] – Quentin Feb 10 '21 at 17:44
  • @KidBilly Show us by typing it up, what you want the result to be please. – ABC Feb 10 '21 at 17:46
  • @File I'm not sure what he's talking about. Of course the problem exists. If you call text() on an XML node with CDATA containing HTML tags, the tags are removed. I'm not the first one to ask this question. – KidBilly Feb 10 '21 at 17:46
  • They aren't! I `console.log(blah)` and the tags were not removed. – Quentin Feb 10 '21 at 17:49
  • https://imgur.com/a/QdPhHns – Quentin Feb 10 '21 at 17:50
  • @Quentin How/where are you testing? Is there a way to test on jsfiddle or codepen? – KidBilly Feb 10 '21 at 17:52
  • I copied your code into local files. Fixed the syntax errors. Added the log statement. Then ran `php -S localhost:7007` so I could access it over HTTP and avoid the same origin policy. – Quentin Feb 10 '21 at 17:54
  • So when I load the xml as in my example, with an AJAX call on a local file, I get the same results as you. In my actual project, which has private data, I'm loading the XML from a URL. That's where the difference seems to be. – KidBilly Feb 10 '21 at 18:43
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/228542/discussion-between-kidbilly-and-quentin). – KidBilly Feb 10 '21 at 18:50

0 Answers0