3

I am sending a request to a URL using $.get(). The response text returns with the following XML:

<?xml version="1.0" encoding="UTF-8" ?>
<myNode>
  <cmd>Requested Item</cmd>
  <myValue>this is the text i need to get with jquery</myValue>
  <res>OK</res>
</myNode>

I need to get the text inside the <myValue> and </myValue> tag:

<myValue>this is the text i need to get with jquery</myValue>

I have tried the following code inside $.get() function:

$.get(url,function(xml){
    var x, i, attnode, xmlDoc, txt;
    xmlDoc = xml.responseXML;
    x = xmlDoc.getElementsByTagName('data');
}

but there is no value in variable x.

0

2 Answers 2

2

Just wrap the xml with $ (jQuery) function, then you can use .find to find the node. Something like $(xml).find('myValue').html()

Demo (In this demo I'm not using ajax but the principle is the same):

var xml = '<?xml version="1.0" encoding="UTF-8" ?>' +
    '<myNode>' + 
    '<cmd>Requested Item</cmd>' +
    '<myValue>this is the text i need to get with jquery</myValue>' +
    '<res>OK</res>' + 
    '</myNode>';

var x = $(xml).find('myValue').html();
console.log(x);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

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

Comments

1

Try this:

$.get(url,function(xml){
    var x, i, attnode, xmlDoc, txt;
    xmlDoc = $.parseXML( xml.responseXML );
    var $xml = $( xmlDoc );
    var myValue= $xml.find( "myValue" );
    console.log(myValue.text())
}

Documentation: https://api.jquery.com/jQuery.parseXML/

1 Comment

yes its also work. I notice that i have not search the result as $(xml) ... thanks again

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.