2009-09-01 89 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");