如何删除AJAX XML响应的外部元素 [英] how to remove outer element of AJAX XML Response

查看:66
本文介绍了如何删除AJAX XML响应的外部元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景:我希望通过AJAX将元素列表返回给浏览器.使用JAXB进行编组,并且由于XML需要一个根元素(JSON没有...),因此我将列表包装在一个信封(包装)元素中.现在,在客户端(浏览器)上,我希望删除外部元素,而将元素列表完整保留在内部.然后,我希望将提取的元素列表放入HTML Dom.我该怎么办?

Scenario: I wish to return a list of elements via AJAX back to browser. Using JAXB for the marshalling and due to XML requiring a root element (which JSON does not...) I wrapped my list inside an envelope (wrapper) element. Now on client side (browser), I wish to remove the outside element leaving the list of elements inside intact. I then wish to put the extracted list of elements into the HTML Dom. How can I do this?

示例说明:

希望发送:

<tr><td>data</td></tr><tr><td>data</td></tr>

由于对text/xml响应类型的限制(为了符合XML),我将其包装为:

Due to restrictions on text/xml response type (in order to comply with XML), I wrapped this as:

<tbody><tr><td>data</td></tr><tr><td>data</td></tr></tbody>

因此,在我的jquery回调方法中,我收到了以上内容(作为xml对象).我如何(除了可以执行的工作以外,没有其他特定的顺序)删除tbody元素,并确保其余的tr标记是可接受的domData.我玩过find(),filter()等,但仍然没有解决这两个要求的解决方案.

So in my jquery callback method I receive the above (as xml object). How can I (in no particular order except whatever works) remove the tbody element, and ensure the remaining tr tags are acceptable domData. I played with find(), filter() etc but still no solution that addresses both requirements.

感谢所有已经答复的人!这真的让我很烦.如果不测试所有提供的答案,则似乎有多种方法可以执行此操作,并且可能每个答案都可以标记为正确答案.我实际上是这样做的:

Thank you to everyone who has already replied! This one was really troubling me. Without testing all the supplied answers, it would appear there are multiple ways to do this and likely the answers each could be marked as the correct one. I actually went with this:

 }).done(function(xmlResponse) {
    $strData = xmlToString(xmlResponse);
    $html = $($strData);
    $('#mytable tbody').html($html.contents());
 });

由于某种原因,如果没有显式标注首先将xml转换为字符串,则无法正常工作.此后,将字符串包装在$()内将其强制为符合DOM,然后content()方法负责外部元素的提取(删除).

For some reason, without the explicit callout to convert the xml to a string first, it didn't work. Thereafter, wrapping the string inside $() coerced it to be DOM conformant, and then the contents() method took care of the extraction (removal) of the outer element.

这是xmlToString()方法.如果有人指出我可以消除对这种丑陋方法的需要,那么那绝对是一个好处.看起来好像是2009年以前的html应用程序中的某些内容:

Here is the xmlToString() method. If anyone can point out how I can remove the need for this ugly method then that would definitely be a bonus. It looks like something out of a pre 2009 html application:

function xmlToString(xmlData) { 
  var xmlString;
  if (window.ActiveXObject){
     xmlString = xmlData.xml;
  } else{
     xmlString = (new XMLSerializer()).serializeToString(xmlData);
  }
  return xmlString;
}   

我将把我读到的第一个答案标记为正确,因为这足以使我超越终点线.

I will mark the first answer I read as correct on basis that it was enough to get me over the finish line.

推荐答案

您可以使用.contents()提取根元素的内容:

You can just use .contents() to extract the contents of your root element:

$html = $('<tbody><tr><td>data</td></tr><tr><td>data</td></tr></tbody>');
$html.contents().appendTo('#something-else');

这篇关于如何删除AJAX XML响应的外部元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆