Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/469.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从Iframe获取XML_Javascript_Jquery_Xml Parsing - Fatal编程技术网

Javascript 如何从Iframe获取XML

Javascript 如何从Iframe获取XML,javascript,jquery,xml-parsing,Javascript,Jquery,Xml Parsing,我有一个包含XML字符串的iframe。我想提取它,以便在javascript或jquery中进行处理。 下面是示例iframe <iframe> <list> <request> <name>CourseDaoMarklogicImpl</name> <id>spring735cfdc6d848026363b835bfcc69c5026a56d217&

我有一个包含XML字符串的iframe。我想提取它,以便在javascript或jquery中进行处理。 下面是示例iframe

<iframe>
    <list>
        <request>
            <name>CourseDaoMarklogicImpl</name>
            <id>spring735cfdc6d848026363b835bfcc69c5026a56d217</id>
            <hits>21</hits>
            <durationsSum>3438</durationsSum>
            <durationsSquareSum>1481952</durationsSquareSum>
            <maximum>688</maximum>
            <cpuTimeSum>45</cpuTimeSum>
            <systemErrors>0</systemErrors>
            <responseSizesSum>-21</responseSizesSum>
            <childHits>0</childHits>
            <childDurationsSum>0</childDurationsSum>
        </request>
    </list>
</iframe>
有没有办法做到这一点?谢谢

$('<div>').append(  $(".iframe").contents()    ).remove().text()
编辑 我现在看到它是跨域的

这行不通

您应该改用jSonP。

试试以下方法:

// get the content of iframe and parse it to a jQuery object
var xml = $($("iframe").text());
// find a 'name' element and retrieve the text
xml.find('name').text(); // "CourseDaoMarklogicImpl"
更新: 如果是跨域的,则必须以不同的方式检索数据。您可以使用YQL服务

var url = ""; // url to your XML file

$.ajax({
  url: "http://query.yahooapis.com/v1/public/yql",
  data: {
    q: "select * from xml where url='" + url + "'",
    format: "json"
  }, 
  success: function(data) {
    console.log(data); // data is a Object representing XML data
  }
});

是一个示例。

如果是同一个域:

alert($("<div>").append($("list", frames['nameOfMyIframe'].document)).html())

XML是加载到iframe中,还是像您的示例中那样提供,即作为无效的替代内容?jQuery是一个JavaScript库,不是JavaScript的替代品。@Quentin-它是使用forms.submitfyi加载到iframe中的。如果您不想在iframe中使用它或不需要它,您也可以这样做:$'list',XML.somexml.somefunction@这是跨域的,你必须使用代理。否则它就是一个安全威胁。@nikhil.agw如果iframe源在同一个域上,它就会工作。`$.getJSON'?',aaa;函数aaadata{alertdata.TheMsg;};'哦你是说json。我不能使用json。我只能使用xml。没关系,你也可以从服务器上获取xml。@nikhil.agw是的,它不会跨域工作。我更新了我的答案-看一看。