.text () in jQuery doesn't work for xml element for any reason

I have the following javascript:

function getMessageFromXML(xml) {
    alert('xml: ' + xml);
    alert("Text of message: " + $(xml).find('dataModelResponse').find('adminMessage').text());
    return $(xml).find('dataModelResponse').find('adminMessage').text();
}

which runs in the following XML:

<dataModelResponse>
  <adminMessage>BARF</adminMessage>
  <adminData>
    <identifier>123456</identifier>
  </adminData>
</dataModelResponse>

I know that XML is correctly transmitted due to the first warning, but for some reason the message appears as empty. I checked that there was exactly 1 message and 1 dataModelResponse element in xml using the .length modifier for find () queries like this, but for some reason I cannot get it to print the correct message.

Suggestions?

EDIT: Changed the name of the tag I was looking for. Posted between the two versions, sorry.

+3
source share
4 answers

Replace $(xml).find('dataModelResponse').find('message').text();with $(xml).find('message').text();.

The documentation for jQuery.find()states:

, .

XML dataModelResponse. $(xml).find('dataModelResponse'), , , dataModelResponse dataModelResponse.

+3

$(xml) node, dataModelResponse. , dataModelResponse, , , text() .

:

console.log("Text of message: " + $(xml).text());

Text of message: BARF 123456 

( , )

console.log("Text of message: " + $(xml).find('message').text());

Text of message: BARF

console.log("Text of message: " + $(xml).find('dataModelResponse').text());

Text of message: 
+2

Could it be 'adminMessage'?

+1
source

If you are using jQuery 1.5, you can write:

alert("Text of message: " + $($.parseXML(xml)).find('adminMessage').text());

or

alert("Text of message: " + $($.parseXML(xml)).find('dataModelResponse > adminMessage').text());  

or

alert("Text of message: " + $($.parseXML(xml)).find('dataModelResponse').find('adminMessage').text());
0
source

Source: https://habr.com/ru/post/1793318/


All Articles