Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the nodes inner text in java script?

I have a string of XML format. As shown below:

<gt>
    <st>sample1</st>
    <tt>sample2</tt>                    
    <tt>sample3</tt>
</gt>

I need to get the node value in Java script file. How can I get the value?

like image 427
P.Muralikrishna Avatar asked May 28 '12 07:05

P.Muralikrishna


2 Answers

Use .parseXML().

var xmldom = $.parseXML('<gt><st>sample1</st><tt>sample2</tt><tt>sample3</tt></gt>');

console.log($(xmldom).find('gt *'));

This will find all nodes under <gt>.

like image 98
Ja͢ck Avatar answered Oct 22 '22 12:10

Ja͢ck


Try this code:

var xml = "<gt><st>sample1</st><tt>sample2</tt><tt>sample3</tt></gt>",
    xmlDoc = $.parseXML(xml),
    $xml = $( xmlDoc ),
    $st = $xml.find( "st" );

alert( $st.text() );

See the js fiddle example

like image 36
Kshitij Avatar answered Oct 22 '22 14:10

Kshitij