2009-09-01 91 views
3

我正在查询Microsoft Office SharePoint Server搜索服务以将一些结果写入Web部件。我的查询工作正常,但在解析通过JQuery的XML响应时遇到了一些麻烦。使用JQuery解析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> 

我想拿到冠军,即史密斯,约瑟夫和JOBTITLE即程序员使用JQuery XML响应。

我开始:

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

回答

4

喜欢的东西

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响应。

+0

旧ASMX样式的Web服务,您可能要添加此代码假定XML是文档的一部分。如果没有,你将不得不为这个代码工作$(xml)。 – SolutionYogi 2009-09-01 15:16:55

+0

谢谢。现在会更新 – 2009-09-01 15:25:10

0

是否有让您获得的项目早在JSON,而不是一种选择?

+0

不,我知道的,这些都是内置的搜索,内置到SharePoint – kd7 2009-09-01 14:57:50

3

这些教程看起来不错:jQuery and XML revisited,Reading XML with jQuery

如果您能够将数据作为JSON(JavaScript Object Notation)获取,那么就使用/操作JavaScript中的数据而言,您会更容易。您可能会看到性能增益,具体取决于数据量。

2

请尝试下面的代码。

Working Demo →

<script> 

    var 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>'; 

    $(document).ready(
     function() 
     { 
      var title, jobTitle; 
      $(xml).find('Property > Name').each(
       function() 
       { 
        $name = $(this); 
        if($name.text() === 'TITLE') 
         title = $name.parent().find('value').text(); 

        if($name.text() === 'JOBTITLE') 
         jobTitle = $name.parent().find('value').text(); 
       } 
      ); 

      alert(title); 
      alert(jobTitle); 
     } 
    ); 

    </script> 
1

我已经非常接近纯选择得到它 - $(xml).find("Name:contains(TITLE)").nextAll("Value").text()而是因为你想要的标题和JOBTITLE就坏了。

无论如何,我想我会把我的解决方案放在那里,因为它有点不同 - 主要想法是只有1,如果得到任何关键。

function getValue(children, key) { 
    var ret; 
    children.find("Name").each(function() { 
    if($(this).text() == key) { 
     ret = $(this).nextAll("Value").text(); 
     return; 
    } 
    }); 
    return ret; 
} 

var children = $(xml).find("Property"); 
var name = getValue(children, "TITLE"); 
var jobTitle = getValue(children, "JOBTITLE");