jquery - 使用 JQuery 解析 XML

标签 jquery xml parsing

我正在查询 Microsoft Office SharePoint Server 搜索服务以将一些结果写入 Web 部件。我的查询工作正常,但在通过 JQuery 解析 xml 响应时遇到了一些问题。

下面是 XML 响应

<ResponsePacket xmlns="urn:Microsoft.Search.Response">
  <Response domain="QDomain">
  <Range>
  <StartAt>1</StartAt> 
  <Count>1</Count> 
  <TotalAvailable>1</TotalAvailable> 
  <Results>
  <Document xmlns="urn:Microsoft.Search.Response.Document">
  <Action>
  <LinkUrl fileExt="aspx">https://mysite.domain.inc:443/Person.aspx?guid=4A4F27E2 9C99 4866 BB08 DE494475A4E7</LinkUrl> 
  </Action>
  <Properties xmlns="urn:Microsoft.Search.Response.Document.Document">
  <Property>
  <Name>TITLE</Name> 
  <Type>String</Type> 
  <Value>Smith, Joseph</Value> 
  </Property>
  <Property>
  <Name>RANK</Name> 
  <Type>Int64</Type> 
  <Value>873</Value> 
  </Property>
  <Property>
  <Name>SIZE</Name> 
  <Type>Int64</Type> 
  <Value>0</Value> 
  </Property>
  <Property>
  <Name>DESCRIPTION</Name> 
  <Type>String</Type> 
  <Value>Hi guys!</Value> 
  </Property>
  <Property>
  <Name>WRITE</Name> 
  <Type>DateTime</Type> 
  <Value>2009 07 31T03:00:24 04:00</Value> 
  </Property>
  <Property>
  <Name>PATH</Name> 
  <Type>String</Type> 
  <Value>https://mysite.domain.inc:443/Person.aspx?guid=4A4F27E2 9C99 4866 BB08 DE494475A4E7</Value> 
  </Property>
  <Property>
  <Name>JOBTITLE</Name> 
  <Type>String</Type> 
  <Value>Programmer</Value> 
  </Property>
  </Properties>
  </Document>
  </Results>
  </Range>
  <Status>SUCCESS</Status> 
  </Response>
  </ResponsePacket>

我正在尝试获取 TITLE,即 Smith、Joseph 和 JOBTITLE,即 Programmer using JQuery。

我开始于:

$(xml).find('Properties').each(function(){
  //not sure how to get the ones I want, use an indexer?
});

最佳答案

有点像

var title;
var jobTitle;

$('Property Name', 'Properties').each(function() {

  var $this = $(this);
  if ($this.text() === "TITLE") {
    title = $this.nextAll("Value").text();
  }
  if ($this.text() === "JOBTITLE") {
    jobTitle = $this.nextAll("Value").text();
  }

});

return {
            "title" : title,
            "jobTitle" : jobTitle
       }

这是一个 Working Demo 使用您的 XML。

编辑:

如评论中所述,我假设 XML 是文档的一部分。如果 XML 不是文档的一部分,则更改以下行

$('Property Name', 'Properties').each(function() { ...

$('Property Name', xml).each(function() {

其中 xml 是服务 xml 响应。

关于jquery - 使用 JQuery 解析 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1362861/

相关文章:

sql-server - 在 SQL Server 的 OPENXML 函数中将空元素视为空值

c# - C#字符串到DateTime的转换

java - 我的 JavaCC 文本解析器中缺少什么?

ruby-on-rails - 在 ruby​​ 中以任意 gmt 偏移量解析时间

jquery - 如何使用 jQuery strip 化多行记录的行?

javascript - 如何使用(out)jquery禁用href并立即调用onclick?

ios - 使用 NSXMLParser 在 Swift 中解析分层 XML

java - 使用 Java 将两个 XML 文件合并为一个 XML 文件

javascript - 如何使 'clickable hand' 出现在 HTML 文档中?

jquery - 将随机背景颜色应用于多个 DIV