jQuery - 从XML页面获取内容并保存到变量

时间:2015-11-20 13:24:02

标签: javascript jquery xml xml-parsing

我试图将XML元素的内容变为变量:

<ContactPresence>
<ContactUri>test.com</ContactUri>
<PresenceState>offline</PresenceState>
</ContactPresence>

如何将“PresenceState”变为变量?

1 个答案:

答案 0 :(得分:1)

使用jQuery,您可以使用parseXML

  var xml = "<ContactPresence><ContactUri>test.com</ContactUri<PresenceState>offline</PresenceState></ContactPresence>",
  xmlDoc = $.parseXML( xml ),
  $xml = $( xmlDoc ),
  $node = $xml.find( "PresenceState" );

  var presenceState = $node.text(); // Should contain offline