get value fot title from XML document
hey all ! i have a jquery that hits a Query on the sharepoint server & fetches the result in the form of XML document that look like :
<?xml version="1.0" ?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<QueryResponse xmlns="urn:Microsoft.Search">
<QueryResult>
<ResponsePacket xmlns="urn:Microsoft.Search.Response">
<Response>
<Range>
<StartAt>1</StartAt>
<Count>1</Count>
<TotalAvailable>1</TotalAvailable>
<Results>
<Document xmlns="urn:Microsoft.Search.Response.Document">
<Action>
<LinkUrl>http://ishaan1519:1234/Lists/Discussions/where are 401k benefit investment prospectus</LinkUrl>
</Action>
<Properties xmlns="urn:Microsoft.Search.Response.Document.Document">
<Property>
<Name>TITLE</Name>
<Type>String</Type>
<Value>where are 401k benefit investment prospectus</Value>
</Property>
<Property>
<Name>PATH</Name>
<Type>String</Type>
<Value>http://ishaan1519:1234/Lists/Discussions/where are 401k benefit investment prospectus</Value>
</Property>
</Properties>
</Document>
</Results>
</Range>
<Status>SUCCESS</Status>
</Response>
</ResponsePacket>
</QueryResult>
</QueryResponse>
</soap:Body>
</soap:Envelope>
i need to populate the text field (#output ) with title & link path using this function
$(xData.responseXML).find("QueryResult").each(function () {
var x = $("<xml>" + $(this).text() + "</xml>");
x.find("Document").each(function () {
url = $("Action>LinkUrl", $(this)).text();
title = $("Title", $(this)).text();
$("#output").append("title: " + title + " - LinkUrl: " + url);
});
i can get the LinkUrl but the title is null
please help me out to populate the textfield with title . from
<Property>
<Name>TITLE</Name>
<Type>String</Type>
<Value>where开发者_JAVA百科 are 401k benefit investment prospectus</Value>
</Property>
Thanks in Advance!
There is no element Title. TITLE is within the element <Name>
title = $("Property>Name", $(this)).text();
SP2010 has a script object model built to make accessing the webservices a lot easier:
var clientContext = new SP.ClientContext("http://ishaan1519:1234/");
var list = clientContext.get_web().get_lists().getByTitle('/Discussions');
var q = "<View><ViewFields><FieldRef Name='Title'/><FieldRef Name='Path'/></ViewFields></View>";
camlQuery.set_viewXml(q);
var listItems = list.getItems(camlQuery);
clientContext.load(listItems, 'Include(Title,Path)');
clientContext.executeQueryAsync(function(sender, args) {
var listEnumerator = listItems.getEnumerator();
while (listEnumerator.moveNext()) {
var title = listEnumerator.get_current().get_item("Title");
var path = listEnumerator.get_current().get_item("Path");
///do your stuff
}
}, function(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
});
jQuery has a contains selector, but I don't think it has an exact-match selector. Using contains you could do something like
x.find("Document").each(function () {
url = $("Action>LinkUrl", $(this)).text();
// find the Name element that contains TITLE
var $nameTitle = $(this).find("Name:contains('TITLE')");
// find the containing Property element
var $property = $nameTitle.closest('Property');
// find the Value in that Property
var $value = $property.find('Value');
// and read text
var title = $value.text();
(obviously you can smash all of those together - expanded for comments). To get just the 'TITLE' element (and not say 'SUBTITLE') I think you'd have to loop, e.g.
var title = null;
$(this).find('Name').each(function() {
var $name = $(this);
if ($name.text() == 'TITLE') {
title = $name.closest('Property').find('Value').text();
return false;
}
});
精彩评论