Reputation: 31610
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?
});
Upvotes: 3
Views: 2128
Reputation: 31761
I came very close to getting it with pure selectors - $(xml).find("Name:contains(TITLE)").nextAll("Value").text()
but because you wanted title and jobtitle it broke.
Anyway, I figure I'll throw my solution in there as it's a little different - the main idea is that there's only 1 if to get any key.
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");
Upvotes: 1
Reputation: 125488
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.
Upvotes: 4
Reputation: 32233
Try following code.
<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>
Upvotes: 2
Reputation: 21727
These tutorials seem good: jQuery and XML revisited, Reading XML with jQuery.
If you are able to get the data as JSON (JavaScript Object Notation) you will have it easier as far as using/manipulating your data in JavaScript. And you might see performance-gains, depending on the amount of data.
Upvotes: 3
Reputation: 42125
Is there an option that allows you to get the items back as JSON
instead?
Upvotes: 0