1

I have a webservice which is responding with this data from server

<?xml version="1.0" encoding="UTF-8"?>
<t0>
   <t9>0</t9>
   <t1>
      <t15>LUI</t15>
      <t3>1353.50</t3>
      <t6>25</t6>
      <t4>12.40</t4>
      <t5>0.92</t5>
      <t7>1342.50</t7>
      <t8>1368.90</t8>
        </t1>

<t1>
      <t15>LUI</t15>
      <t3>1233.50</t3>
      <t6>25</t6>
      <t4>12.40</t4>
      <t5>0.92</t5>
      <t7>1342.50</t7>
      <t8>1368.90</t8>
        </t1>

</t0>

From the XML response I want to read only the first root t1 tag and ignore the second t1 tag

Once i read the first root t1 tag , i need to extract the t3 and t8 values of t1

I have tried this ,

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>

<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript">
var symbols = [ "TEST1", "TEST2", "TEST3"];

var mydata = {
    "firstName": "John",
    "lastName": "Doe"
};

 $(document).ready(function () {
 $.each( symbols, function( index, value ){
    loadXMLDoc(value);
});
});
function loadXMLDoc(value)
{
        $.ajax({
            type: 'POST',
            url: 'https://mywebserver//8080//data',
        data : "e=91&s="+value+"",
       success: function (data) {

  $(data).find("t1").each(function()
  {

var t3 =   $(this).attr("t3");
var t8  =  $(this).attr("t8");

alert(t3);
alert(t8);

  });



    } ,
            error: function (e) {    
               alert('error'+e);
            }
        });

  }

    </script>
</head>
<body>

</body>
</html>

Could you please let me know , how can i from the root t1 tag extract t3 and t8 tags ??

2 Answers 2

3

You can use .children() or .find() here since t3 and t8 are children of t1:

$(data).find("t1").each(function () {
    var t3 = $(this).find("t3").text();
    var t8 = $(this).find("t8").text();

    alert(t3);
    alert(t8);
});
Sign up to request clarification or add additional context in comments.

1 Comment

So efficient , thank you very much , you saved my day .
1

To read the first one you need to return false after the first loop and each of the elements you still need to use find -

$(data).find("t1").each(function () {
    var t3 = $(this).find("t3").text();
    var t8 = $(this).find("t8").text();

    return false; // stops the loop after one t1
});

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.