使用 Javascript 读取页面 XML

发布于 2024-10-21 02:53:22 字数 1056 浏览 4 评论 0原文

嘿伙计们,这让我简直要疯了,所以我想请教这个网站上的专家,看看你们是否知道怎么做 =)

我正在尝试创建一些可以读出网页元素的 javascript 代码(例如第一段说了什么?)。这是我到目前为止所做的,但它不起作用,我不明白为什么:

<script type="text/javascript">
<!--
var req;
// handle onreadystatechange event of req object
function processReqChange() {
    // only if req shows "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
            //document.write(req.responseText);
            alert("done loading");

            var responseDoc = new DOMParser().parseFromString(req.responseText, "text/xml");
            alert(responseDoc.evaluate("//title",responseDoc,null,
                        XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue);
         } 
         else {
            document.write("<error>could not load page</error>");
         }
    }
}

req = new XMLHttpRequest();
req.onreadystatechange = processReqChange;
req.open("GET", "http://www.apple.com", true);
req.send(null);
// -->

不断出现的警报是“空”,我不明白为什么。有什么想法吗?

Hey guys, this is driving me absolutely insane so I wanted to ask the experts on this site to see if you know how to do it =)

I'm trying to create some javascript code that can read out elements of a web page (eg. what does the first paragraph say?). Here's what I have so far, but it doesnt work and I cant figure out why:

<script type="text/javascript">
<!--
var req;
// handle onreadystatechange event of req object
function processReqChange() {
    // only if req shows "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
            //document.write(req.responseText);
            alert("done loading");

            var responseDoc = new DOMParser().parseFromString(req.responseText, "text/xml");
            alert(responseDoc.evaluate("//title",responseDoc,null,
                        XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue);
         } 
         else {
            document.write("<error>could not load page</error>");
         }
    }
}

req = new XMLHttpRequest();
req.onreadystatechange = processReqChange;
req.open("GET", "http://www.apple.com", true);
req.send(null);
// -->

The alert that keeps appearing is "null" and I can't figure out why. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

×眷恋的温暖 2024-10-28 02:53:22

这可能是由于跨域限制......除非您将网页托管在 apple.com 上。 :) 您还可以使用 jQuery 并避免写出所有内容和/或处理任何常见的可能的跨浏览器 XML 加载/解析问题。 http://api.jquery.com/category/ajax/

更新:< /强>
看起来它可能与源网站的 Content-Type 或类似的东西有关...例如,此代码似乎可以工作...(注意加载的域...)


var req;
// handle onreadystatechange event of req object
function processReqChange() {
    // only if req shows "loaded"
    if (req.readyState == 4) {
        // only if "OK"

        if (req.status == 200) {
            //document.write(req.responseText);
            //alert("done loading");
            //alert(req.responseText);

            var responseDoc = new DOMParser();
            var xmlText = responseDoc.parseFromString(req.responseText, "text/xml");
            try{
              alert(xmlText.evaluate("//title",xmlText,null,XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue);
            }catch(e){
              alert("error");
            }
         } 
         else {
            document.write("could not load page");
         }
    }
}

req = new XMLHttpRequest();
req.onreadystatechange = processReqChange;
req.open("GET", "http://www.jquery.com", true);
req.send(null);

我也尝试加载 espn.com 和google.com,并注意到它们都有“Content-Encoding:gzip”,所以也许这就是问题所在,只是猜测。

This may be due to cross domain restriction... unless you're hosting your web page on apple.com. :) You could also use jQuery and avoid writing all that out and/or dealing with any common possible cross-browser XML loading/parsing issues. http://api.jquery.com/category/ajax/

Update:
Looks like it may have something to do with the source web site's Content-Type or something similar... For example, this code seems to work... (Notice the domain loaded...)


var req;
// handle onreadystatechange event of req object
function processReqChange() {
    // only if req shows "loaded"
    if (req.readyState == 4) {
        // only if "OK"

        if (req.status == 200) {
            //document.write(req.responseText);
            //alert("done loading");
            //alert(req.responseText);

            var responseDoc = new DOMParser();
            var xmlText = responseDoc.parseFromString(req.responseText, "text/xml");
            try{
              alert(xmlText.evaluate("//title",xmlText,null,XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue);
            }catch(e){
              alert("error");
            }
         } 
         else {
            document.write("could not load page");
         }
    }
}

req = new XMLHttpRequest();
req.onreadystatechange = processReqChange;
req.open("GET", "http://www.jquery.com", true);
req.send(null);

I also tried loading espn.com and google.com, and noticed they both have "Content-Encoding:gzip" so maybe that's the issue, just guessing though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文