Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing XML with JQuery

I am querying the Microsoft Office SharePoint Server Search Service to write some results into a web part. I have the query working correctly but am having some trouble parsing the xml response via JQuery.

Below is the XML response

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

I'm trying to get the TITLE i.e. Smith, Joseph and the JOBTITLE i.e Programmer using JQuery.

I started with:

$(xml).find('Properties').each(function(){
  //not sure how to get the ones I want, use an indexer?
});
like image 675
Ta01 Avatar asked Feb 06 '26 00:02

Ta01


1 Answers

Something like

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
       }

Here's a Working Demo with your XML.

EDIT:

As noted in the comments, I have made the assumption that the XML is part of the document. If the XML is not part of the document, then change the following line

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

to

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

where xml is the service xml response.

like image 82
Russ Cam Avatar answered Feb 07 '26 14:02

Russ Cam