Javascript XML parsing

Hi,

I'm new to javascript and am starting to make basic AJAX REST calls using JQuery. I'm getting the response as XML but am having trouble parsing out the value I need. The structure of the XML is as follows and I need to get the 'value' of 'inactive' and trigger an 'alert'

<?xml version="1.0" encoding="UTF-8"?>
<Enumerated value="inactive" xmlns="http://bacnet.org/csml/1.2"/>

Open in new window


Here is what I tried but the alert is empty:

function getData(){

        $.ajax({
            method: "GET",
            url: "/enteliweb/api/.bacnet/22 Quarry Road/400000/binary-output,1102/present-value",
            dataType: 'xml',
            success: function (xml) {

               $(xml).find('Enumerated').each(function(){
                   alert($(this).find("value").text());
                });
            },
                error:function (response) {
                    alert(response);
                }

        });


    }

Open in new window

LVL 1
wint100Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Alexandre SimõesManager / Technology SpecialistCommented:
This is the code you want to use when you receive the XML:
var $xml = $($.parseXML(xml));
$xml.find('Enumerated').each(function (idx, item) {
    alert($(item).attr("value"));
});

Open in new window

The main trick is to parse the XML, and jQuery gives you that with $.parseXML
Second thing, value is an attribute, so you need to fetch it as such.

And that's it! :)

Here's a working sample without the ajax call
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
wint100Author Commented:
Great, thanks. I've had to comment out the xmlparse part though as I found previously that the xml is already an XMLDocument, not sure why but this code now works:

 function getData(){

        $.ajax({
            method: "GET",
            url: "/enteliweb/api/.bacnet/22 Quarry Road/400000/binary-output,1102/present-value",
            dataType: 'xml',
            success: function (xml) {
                
                $(xml).find('Enumerated').each(function (idx, item) {
                    alert($(item).attr("value"));
                });

            },
                error:function (response) {
                    alert(response);
                }

        });


    }

Open in new window

0
Alexandre SimõesManager / Technology SpecialistCommented:
The difference to yours is that you're picking the value properly now.
Before you were using .find to get the value.
.find will search for child nodes, not attributes.

The code that works uses .attr that gets or sets attributes of the current node.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.