jquery parse html in xml
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
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.
try to read xml file as html:
$.ajax({ type: "GET", url: 'file.xml', dataType: "html", success: function(xml) { ....
Just using $(this).find("description").html() seems to work.
精彩评论