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>
17
  • 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 minimal reproducible example! See also: How to Ask Commented Feb 10, 2021 at 17:32
  • That part of the question isn't really the point. :S But i'll fix it Commented Feb 10, 2021 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. Commented Feb 10, 2021 at 17:36
  • Is there any way you can show me? I'm not sure what I'm doing wrong. Commented Feb 10, 2021 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. Commented Feb 10, 2021 at 17:38

0

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.