用JQuery解析XML

用JQuery解析XML,jquery,xml,parsing,Jquery,Xml,Parsing,我正在查询Microsoft Office SharePoint Server搜索服务,以便将一些结果写入web部件。我的查询工作正常,但在通过JQuery解析xml响应时遇到了一些问题 下面是XML响应 <ResponsePacket xmlns="urn:Microsoft.Search.Response"> <Response domain="QDomain"> <Range> <StartAt>1</StartAt>

我正在查询Microsoft Office SharePoint Server搜索服务,以便将一些结果写入web部件。我的查询工作正常,但在通过JQuery解析xml响应时遇到了一些问题

下面是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>

是否有一个选项允许您将项目取回为
JSON

这些教程看起来不错:

如果您能够以JSON(JavaScript对象表示法)的形式获取数据,那么在JavaScript中使用/操作数据就更容易了。根据数据量的不同,您可能会看到性能的提高

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
       }
这里有一个包含XML的

编辑:

如评论中所述,我假设XML是文档的一部分。如果XML不是文档的一部分,则更改以下行

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

其中
xml
是服务xml响应。

尝试以下代码


var xml='1https://mysite.domain.inc:443/Person.aspx?guid=4A4F27E2 9C99 4866 BB08 DE494475A4E7标题字符串Smith,Joseph RANK Int64 873 SIZE Int64 0描述字符串大家好!写入日期时间2009 07 31T03:00:24 04:00路径字符串https://mysite.domain.inc:443/Person.aspx?guid=4A4F27E2 9C99 4866 BB08 DE494475A4E7作业标题字符串程序员成功';
$(文件)。准备好了吗(
函数()
{
var标题、职务标题;
$(xml)。查找('Property>Name')。每个(
函数()
{
$name=$(这个);
如果($name.text()=='TITLE')
title=$name.parent().find('value').text();
如果($name.text()==='JOBTITLE')
jobTitle=$name.parent().find('value').text();
}
);
警报(标题);
警报(职务名称);
}
);

我几乎用纯选择器得到了它-
$(xml).find(“Name:contains(TITLE)”).nextAll(“Value”).text()
但是因为您需要TITLE和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");

据我所知,这些都是内置的搜索,旧的ASMX风格的Web服务,内置在SharePoint中。您可能需要补充一点,这段代码假设XML是文档的一部分。如果没有,则必须执行$(xml)才能使此代码正常工作。
$('Property Name', xml).each(function() {
  <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>
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");