1

I have an xml file with html tags in it that I parse using jquery ajax. I can get the text value without a problem. How do I get the html part within an element?

Edit: I tried wrapping the html in cdata but would rather have the xhtml within the xml.

$.ajax({ type: "GET",
                        url: "product.xml",
                        dataType: "xml",
                        success: function (xml) {

                            $(xml).find("product[productid=" + selection + "]").each(function () {
                                var $title = $(this).find("title").text();        //get title  
                                var $desc = $(this).find("description").html();          //get description  
                                //$('[nodeName=]',xml)
                                $("#producttitle").html("");
                                $("#productdesc").append($desc);
                                $("#producttitle").append($title);

                            });

                        }  

<product productid="1">
    <title>Active Directory</title>
    <status>noinfo</status>
    <description>
        Headline
        <b>tester</b>
        <ul>
            <li>test list element</li>
        </ul>
    </description>
    <link title=""></link>
</product>

Cheers, Terry

1
  • Well basically the usual jquery.ajax $.ajax({ type: "GET", url: "product.xml", dataType: "xml", success: function (xml) {.... I get the value with var $desc = $(this).find("description").text(); Commented Jun 8, 2011 at 10:08

3 Answers 3

1

To get content with tags included as tags rather than escaped as text content, use html:

var $desc = $(this).find("description").html();

However, if you are inserting this content into the document, you don't need to get the HTML -- you can just append the selection:

$(this).find('description').clone().appendTo('#containingElement');

clone is necessary to import the node.

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

2 Comments

unfortunately using .html() won't work with ajax datatype xml.
@Terry Thanks, I've deleted that bit.
1

try to read xml file as html:

$.ajax({ type: "GET", url: 'file.xml', dataType: "html", success: function(xml) { ....

1 Comment

Using the 'HTML' datatype (instead of 'XML') helped solved a problem where the JQuery XML parser failed due to unsupported special char encodings contained in the XML.
0

Just using $(this).find("description").html() seems to work.

Comments

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.