1
votes

I want to parse the following xml which is the response from an restful webservice:

<ns2:list xmlns="urn:foo1:foo" xmlns:ns2="foo2:foo">

   <entityData>
      <namedAttributes>...</namedAttributes>
      <dynamicEnums>...</dynamicEnums>
   </entityData>

   <ns2:employees>
      <ns2:user id="test">
          <ns2:name genderTitle="0" firstName="Rock" surName="Solid"></ns2:name>
      </ns2:user >
   </ns2:employees>
</ns2:list>

If I try a xpath-expression I only get [object Object] as alert:

function parse(xml){
   var test= $(this).find('/ns2:list/ns2:employees/ns2:user[85]/ns2:name');
   alert(test);
};

Adding .text()-method like: var test= $(this).find('/ns2:list/ns2:employees/ns2:user[85]/ns2:name').text(); only makes the alert empty...

The xpath expression should not be wrong, I used Firebug to get the expression, maybe in this example some typing error.

Anyone knows whats wrong? Or the other way round: how to alert fields like firstName?

3
I have a very simple namespace. How I can get the value of ns2:count? <availableSlots xmlns:ns5="jabber.org/protocol/httpbind" xmlns:ns2="bindings.egain.com/chat" xmlns:ns4="urn:ietf:params:xml:ns:xmpp-stanzas" xmlns:ns3="jabber:client"> <ns2:count>1</ns2:count> </availableSlots> - Abdul Waheed

3 Answers

1
votes

Doesnt /list/employees/user[@id='test']/name/@firstName work?

1
votes

You probably need to add the namespace in your query for the name.

So you need something like:

/*[local-name()='list' and namespace-uri()='urn:foo1:foo']
0
votes

you may need to look at .parseXML to parse the xml also here is a good SO answer to parse xml with namespaces jQuery XML parsing with namespaces

here is a good link too Namespace Selectors for jQuery

solution

    xmlDoc = $.parseXML(xml),
    $xml = $(xmlDoc),
    $name = $xml.find( "ns2\\:name" ).attr("surName");

   alert($name);

here id the fiddle http://jsfiddle.net/Jbnev/