1

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?

dotchuZ
  • 2,621
  • 11
  • 39
  • 65

3 Answers3

1

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']
Bas Slagter
  • 9,831
  • 7
  • 47
  • 78
1

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

Ben
  • 13,297
  • 4
  • 47
  • 68
0

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/

Community
  • 1
  • 1
Rafay
  • 30,950
  • 5
  • 68
  • 101
  • @Vicky you must post a new question containing the necessary information – Rafay Jul 21 '13 at 18:37
  • @3nigma, just that jsfiddle link returns undefined. It needs to be used as a reference for future, edit that or try to remove that fiddle link. – user9371102 Jul 22 '13 at 06:18