Extracting html element from string
I have some XML string output which looks like the following:
<xml>
<node>
<Title name="Title">Tea and Coffee</Title>
<Menu-Item name="Menu-Item">
<div class="field-item field-item-0">Freshly brewed fairtrade filter coffee and a selection of fairtrade tea and herbal infusions</div><div class="field-ite开发者_JAVA技巧m field-item-1">Ut laoreet porta tellus, ut pellentesque ipsum dictum metus.</div>
</Menu-Item>
</node>
</xml>
The next thing I need to do is extracting the 'div' elements and casting them so I can use them as jquery objects as normal.
Any ideas?
Let us say you have this as a text string in a variable named myxml. Then you read it with jquery, as follows:
$(myxml).find('div').each(function() { alert($(this).text()); })
The alert is only meant to show how you can get the inner text. Here's a post that can help you with more examples: reading-xml-with-jquery
$('<xml>'+
'<node>'+
'<Title name="Title">Tea and Coffee</Title>'+
'<Menu-Item name="Menu-Item">'+
'<div class="field-item field-item-0">Freshly brewed fairtrade filter coffee and a selection of fairtrade tea and herbal infusions</div><div class="field-item field-item-1">Ut laoreet porta tellus, ut pellentesque ipsum dictum metus.</div>'+
'</Menu-Item>'+
'</node>'+
'</xml>').find('div').appendTo('body');
See it in action: http://jsfiddle.net/EhQKv/
精彩评论