简体   繁体   中英

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?

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> .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM