0

I am trying to get some xml datas from webservice. I can access all data but i cannot get one by one. I want write data to div that has id "code". It writes [object Element]. What i have to do?

<script type="text/javascript">
        $(document).ready(function () {
            var returnValue = localStorage.getItem("returnValue");
            var userName = localStorage.getItem("userName");
            alert("Hello" + userName);
            var wsUrl = "http://xxx=GetStocks";

            var soapRequest =
                '<?xml version="1.0" encoding="utf-8"?>\
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\ xmlns:xsd="http://www.w3.org/2001/XMLSchema"\ xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">\
  <soap:Body>\
    <GetStocks xmlns="http://sales.xxx.org/">\
      <token>' + returnValue + '</token>\
    </GetStocks>\
  </soap:Body>\
</soap:Envelope>';

            $.ajax({
                type: "POST",
                url: wsUrl,
                contentType: "text/xml",
                dataType: "xml",
                data: soapRequest,
                success: processSuccess,
                error: processError
            });
        });

        function processSuccess(data, status, req) {
            if (status == "success") {
                var stocks = $(req.responseXML).find("pStockInfo");
                var codes = $(req.responseXML).find("Code");
                var stocksCount = stocks.length;

                $("#code").text(codes[0]); //Here i want first code.               
            }
        }

        function processError(data, status, req) {
            alert(req.responseText + " " + status);
        }
    </script>

enter image description here

Xml Data

enter image description here

I want get first code "FO19428"

2
  • Please insert a console.log(codes); before $("#code").text(codes[0]); and post the output in your question. Commented Mar 3, 2017 at 14:28
  • It would help if you have the XML data inside the question, possibly in a isolated snippet example inside a variable with only the code that focuses on processing it. Aside the fact images are blocked in my browser, I can't copy-paste data from an image into a snippet, fiddle, etc.. to see why/what works for certain. Commented Mar 3, 2017 at 14:29

3 Answers 3

1

You can use jQuery.eq()

 var codes = $(req.responseXML).find("Code");
 var firstCode = codes.eq(0).text(); 
Sign up to request clarification or add additional context in comments.

Comments

1

codes[0] contains <code>FO19428</code>. You can use jQuery's .text() function to get it's content:

var firstCode = $(codes[0]).text();

Here a fiddle:

var responseXML = "<ArrayOfPStockInfo><pStockInfo><Code>FO19428</Code><Name>ITACA KC</Name><Stock>2</Stock></pStockInfo></ArrayOfPStockInfo>";

var codes = $(responseXML).find("Code");
var firstCode = $(codes[0]).text();
$("#code").text(firstCode);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="code"></div>

Comments

1

jquery find return all the elements found.so you must map all the elements to their textContent.

Test

test('jquery operate on XML', () => {
        const xml = new DOMParser().parseFromString(`
            <bookstore>
                <book>
                    <title>cooking</title>
                </book>
                <book>
                    <title>drinking</title>
                </book>
            </bookstore>
        `, 'text/xml');
    
        let books = $(xml).find('book title').map(function () {
            return this.textContent;
        }).toArray();
        expect(xml instanceof Document).toBe(true);
        expect(books).toEqual(['cooking', 'drinking']);
    });

Demo

var xml = new DOMParser().parseFromString([
        '<bookstore>',
            '<book>',
                '<title>cooking</title>',
            '</book>',
            '<book>',
                '<title>drinking</title>',
            '</book>',
        '</bookstore>'
    ].join(''), 'text/xml');

var books = $(xml).find('book title').map(function () {
    return this.textContent;
}).toArray();

console.log(books);
  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

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.