1

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-item 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?

4
  • 3
    I know you do not use regex! That I know Commented Jun 15, 2011 at 13:04
  • Where exactly is your problem? Do you not know how to select anything from xml? do you not know how to convert the xml node you are getting into a jquery object? You might want to clarify exactly where you've got to and where you are stuck... Commented Jun 15, 2011 at 13:04
  • Are you calling this XML with AJAX? Is the code already on the page? Standard jQuery would be var div = $('.field-item'); Commented Jun 15, 2011 at 13:06
  • Yes I am using ajax to shift through the xml output. I am doing the following <code>$($menuitem).each( function(){ alert('Found : ' + $($menuitem).text() ); } );</code> but I get the content of both 'divs' concatenated into one. I wonder if there is a way to retrieve the content as div elements so I can refer to them as jquery object? Commented Jun 15, 2011 at 13:11

2 Answers 2

3

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

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

Comments

1
$('<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/

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.