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?

1
  • 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> Commented Feb 15, 2019 at 17:30

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

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']

2 Comments

what do you mean? instead of ns2:...?
Edited...maybe also googling for xpath and namespaces might help you out even more.
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/

2 Comments

@Vicky you must post a new question containing the necessary information
@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.

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.