从XML文档中获取标题的值

时间:2011-01-28 10:24:48

标签: jquery xml parsing sharepoint-2010 xml-parsing

嘿所有!我有一个jquery在sharepoint服务器和&以XML文档的形式获取结果,如下所示:

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

我需要用标题&amp;填充文本字段(#output)链接路径 使用此功能

$(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);
    });

我可以获得LinkUrl,但标题为null 请帮我填写带有标题的文本字段。来自

<Property>
    <Name>TITLE</Name>
    <Type>String</Type>
    <Value>where are 401k benefit investment prospectus</Value>
</Property>

提前致谢!

3 个答案:

答案 0 :(得分:1)

没有元素标题。 TITLE位于元素&lt; Name&gt;

title = $("Property>Name", $(this)).text();

答案 1 :(得分:1)

SP 2010有一个脚本对象模型,可以更轻松地访问Web服务:

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());
}); 

答案 2 :(得分:1)

jQuery有一个包含选择器,但我不认为它有一个完全匹配的选择器。使用包含你可以做类似的事情

    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();

(显然你可以将所有这些粉碎在一起 - 扩展以征求意见)。要获得'TITLE'元素(而不是'SUBTITLE'),我认为你必须循环,例如。

        var title = null;
        $(this).find('Name').each(function() {
          var $name = $(this);
          if ($name.text() == 'TITLE') {
            title = $name.closest('Property').find('Value').text();
            return false;
          }
        });