有人可以帮我解决这个XML代码吗?

时间:2011-03-07 05:53:39

标签: javascript xml ajax

鉴于以下HTML,JavaScript和XML,

是什么
    tempText.innerHTML = allImages[i].textContent 

意思是,

的价值是多少
    allImages[i].textContent

HTML标记

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>My Second Ajax Script</title>
    <link type="text/css" rel="stylesheet" href="script02.css" />
    <script type="text/javascript" src="script02.js"></script>
</head>
<body>
    <div id="pictureBar"> </div>
</body>
</html>

JavaScript代码

window.onload = initAll;
var xhr = false;

function initAll() {
    if (window.XMLHttpRequest) {
        xhr = new XMLHttpRequest();
    }
    else {
        if (window.ActiveXObject) {
            try {
                xhr = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) { }
        }
    }

    if (xhr) {
        xhr.onreadystatechange = showPictures;
        xhr.open("GET", "C:\wamp\www\flickrfeed.xml", true);
        xhr.send(null);
    }
    else {
        alert("Sorry, but I couldn't create an XMLHttpRequest");
    }
}

function showPictures() {
    var tempDiv = document.createElement("div");
    var tempText = document.createElement("div");

    if (xhr.readyState == 4) {
        if (xhr.status == 200) {
            var allImages = xhr.responseXML.getElementsByTagName("content");

            for (var i=0; i<allImages.length; i++) {
                tempText.innerHTML = allImages[i].textContent;
                tempDiv = tempText.getElementsByTagName("p");

                var theText = tempDiv[1].innerHTML;
                theText = theText.replace(/240/g,"75");
                theText = theText.replace(/180/g,"75");
                theText = theText.replace(/_m/g,"_s");
                document.getElementById("pictureBar").innerHTML += theText;
            }
        }
        else {
            alert("There was a problem with the request " + xhr.status);
        }
    }
}

XML数据

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<feed xmlns="http://www.w3.org/2005/Atom"
      xmlns:dc="http://purl.org/dc/elements/1.1/"  xmlns:flickr="urn:flickr:" xmlns:media="http://search.yahoo.com/mrss/">    
    <title>Content from Paradise Ridge Sculpture Grove</title>
    <link rel="self" href="http://api.flickr.com/services/feeds/photoset.gne?set=72157600976524175&amp;nsid=23922109@N00&amp;lang=en-us" />
    <link rel="alternate" type="text/html" href="http://www.flickr.com/photos/dorismith/sets/72157600976524175"/>
    <id>tag:flickr.com,2005:http://www.flickr.com/photos/23922109@N00/sets/72157600976524175</id>
    <icon>http://farm2.static.flickr.com/1335/882568164_72eee9b41f_s.jpg</icon>
    <subtitle>The &lt;a href=&quot;http://www.paradiseridgewinery.com/&quot;&gt;Paradise Ridge Winery&lt;/a&gt; not only has great wines, but they also have a sculpture garden. We visited on 22 July 2007.</subtitle>
    <updated>2007-07-24T05:19:08Z</updated>
    <generator uri="http://www.flickr.com/">Flickr</generator>        
    <entry>
        <title>IMG_0045.JPG</title>
        <link rel="alternate" type="text/html" href="http://www.flickr.com/photos/dorismith/882590644/in/set-72157600976524175/"/>
        <id>tag:flickr.com,2005:/photo/882590644/in/set-72157600976524175</id>
        <published>2007-07-24T05:19:08Z</published>
        <updated>2007-07-24T05:19:08Z</updated>
        <dc:date.Taken>2007-07-22T13:42:49-08:00</dc:date.Taken>
        <content type="html">
            &lt;p&gt;&lt;a href=&quot;http://www.flickr.com/people/dorismith/&quot;&gt;Dori Smith&lt;/a&gt; posted a photo:&lt;/p&gt;
            &lt;p&gt;&lt;a href=&quot;http://www.flickr.com/photos/dorismith/882590644/&quot; title=&quot;IMG_0045.JPG&quot;&gt;&lt;img src=&quot;http://farm2.static.flickr.com/1063/882590644_5a4a0d89f3_m.jpg&quot; width=&quot;240&quot; height=&quot;180&quot; alt=&quot;IMG_0045.JPG&quot; /&gt;&lt;/a&gt;&lt;/p&gt;          
        </content>
        <author>
            <name>Dori Smith</name>
            <uri>http://www.flickr.com/people/dorismith/</uri>
        </author>
        <link rel="enclosure" type="image/jpeg" href="http://farm2.static.flickr.com/1063/882590644_5a4a0d89f3_m.jpg" />    
        <category term="winery" scheme="http://www.flickr.com/photos/tags/" />
        <category term="sonomacounty" scheme="http://www.flickr.com/photos/tags/" />
        <category term="sculptures" scheme="http://www.flickr.com/photos/tags/" />
        <category term="dorismith" scheme="http://www.flickr.com/photos/tags/" />
        <category term="paradiseridge" scheme="http://www.flickr.com/photos/tags/" />
        <category term="paradiseridgesculptures" scheme="http://www.flickr.com/photos/tags/" />
    </entry>
    <entry>
        <title>IMG_0043.JPG</title>
        <link rel="alternate" type="text/html" href="http://www.flickr.com/photos/dorismith/882589162/in/set-72157600976524175/"/>
        <id>tag:flickr.com,2005:/photo/882589162/in/set-72157600976524175</id>
        <published>2007-07-24T05:19:08Z</published>
        <updated>2007-07-24T05:19:08Z</updated>
        <dc:date.Taken>2007-07-22T13:41:55-08:00</dc:date.Taken>
        <content type="html">
            &lt;p&gt;&lt;a href=&quot;http://www.flickr.com/people/dorismith/&quot;&gt;Dori Smith&lt;/a&gt; posted a photo:&lt;/p&gt;
            &lt;p&gt;&lt;a href=&quot;http://www.flickr.com/photos/dorismith/882589162/&quot; title=&quot;IMG_0043.JPG&quot;&gt;&lt;img src=&quot;http://farm2.static.flickr.com/1179/882589162_25b4d6bcbe_m.jpg&quot; width=&quot;180&quot; height=&quot;240&quot; alt=&quot;IMG_0043.JPG&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
        </content>
        <author>
            <name>Dori Smith</name>
            <uri>http://www.flickr.com/people/dorismith/</uri>
        </author>
        <link rel="enclosure" type="image/jpeg" href="http://farm2.static.flickr.com/1179/882589162_25b4d6bcbe_m.jpg" />    
        <category term="winery" scheme="http://www.flickr.com/photos/tags/" />
        <category term="sonomacounty" scheme="http://www.flickr.com/photos/tags/" />
        <category term="sculptures" scheme="http://www.flickr.com/photos/tags/" />
        <category term="dorismith" scheme="http://www.flickr.com/photos/tags/" />
        <category term="paradiseridge" scheme="http://www.flickr.com/photos/tags/" />
        <category term="paradiseridgesculptures" scheme="http://www.flickr.com/photos/tags/" />
    </entry>
    <entry>
        <title>IMG_0042.JPG</title>
        <link rel="alternate" type="text/html" href="http://www.flickr.com/photos/dorismith/882587910/in/set-72157600976524175/"/>
        <id>tag:flickr.com,2005:/photo/882587910/in/set-72157600976524175</id>
        <published>2007-07-24T05:19:08Z</published>
        <updated>2007-07-24T05:19:08Z</updated>
        <dc:date.Taken>2007-07-22T13:40:36-08:00</dc:date.Taken>
        <content type="html">
            &lt;p&gt;&lt;a href=&quot;http://www.flickr.com/people/dorismith/&quot;&gt;Dori Smith&lt;/a&gt; posted a photo:&lt;/p&gt;
            &lt;p&gt;&lt;a href=&quot;http://www.flickr.com/photos/dorismith/882587910/&quot; title=&quot;IMG_0042.JPG&quot;&gt;&lt;img src=&quot;http://farm2.static.flickr.com/1073/882587910_39ffde28e6_m.jpg&quot; width=&quot;240&quot; height=&quot;180&quot; alt=&quot;IMG_0042.JPG&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
        </content>
        <author>
            <name>Dori Smith</name>
            <uri>http://www.flickr.com/people/dorismith/</uri>
        </author>
        <link rel="enclosure" type="image/jpeg" href="http://farm2.static.flickr.com/1073/882587910_39ffde28e6_m.jpg" />    
        <category term="winery" scheme="http://www.flickr.com/photos/tags/" />
        <category term="sonomacounty" scheme="http://www.flickr.com/photos/tags/" />
        <category term="sculptures" scheme="http://www.flickr.com/photos/tags/" />
        <category term="dorismith" scheme="http://www.flickr.com/photos/tags/" />
        <category term="paradiseridge" scheme="http://www.flickr.com/photos/tags/" />
        <category term="paradiseridgesculptures" scheme="http://www.flickr.com/photos/tags/" />
    </entry>
    <entry>
        <title>IMG_0041.JPG</title>
        <link rel="alternate" type="text/html" href="http://www.flickr.com/photos/dorismith/882586288/in/set-72157600976524175/"/>
        <id>tag:flickr.com,2005:/photo/882586288/in/set-72157600976524175</id>
        <published>2007-07-24T05:19:08Z</published>
        <updated>2007-07-24T05:19:08Z</updated>
        <dc:date.Taken>2007-07-22T13:40:18-08:00</dc:date.Taken>
        <content type="html">
            &lt;p&gt;&lt;a href=&quot;http://www.flickr.com/people/dorismith/&quot;&gt;Dori Smith&lt;/a&gt; posted a photo:&lt;/p&gt;
            &lt;p&gt;&lt;a href=&quot;http://www.flickr.com/photos/dorismith/882586288/&quot; title=&quot;IMG_0041.JPG&quot;&gt;&lt;img src=&quot;http://farm2.static.flickr.com/1334/882586288_6de3304bbb_m.jpg&quot; width=&quot;240&quot; height=&quot;180&quot; alt=&quot;IMG_0041.JPG&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
        </content>
        <author>
            <name>Dori Smith</name>
            <uri>http://www.flickr.com/people/dorismith/</uri>
        </author>
        <link rel="enclosure" type="image/jpeg" href="http://farm2.static.flickr.com/1334/882586288_6de3304bbb_m.jpg" />    
        <category term="winery" scheme="http://www.flickr.com/photos/tags/" />
        <category term="sonomacounty" scheme="http://www.flickr.com/photos/tags/" />
        <category term="sculptures" scheme="http://www.flickr.com/photos/tags/" />
        <category term="dorismith" scheme="http://www.flickr.com/photos/tags/" />
        <category term="paradiseridge" scheme="http://www.flickr.com/photos/tags/" />
        <category term="paradiseridgesculptures" scheme="http://www.flickr.com/photos/tags/" />
    </entry>
    <entry>
        <title>IMG_0040.JPG</title>
        <link rel="alternate" type="text/html" href="http://www.flickr.com/photos/dorismith/882583896/in/set-72157600976524175/"/>
        <id>tag:flickr.com,2005:/photo/882583896/in/set-72157600976524175</id>
        <published>2007-07-24T05:19:08Z</published>
        <updated>2007-07-24T05:19:08Z</updated>
        <dc:date.Taken>2007-07-22T13:39:49-08:00</dc:date.Taken>
        <content type="html">
            &lt;p&gt;&lt;a href=&quot;http://www.flickr.com/people/dorismith/&quot;&gt;Dori Smith&lt;/a&gt; posted a photo:&lt;/p&gt;
            &lt;p&gt;&lt;a href=&quot;http://www.flickr.com/photos/dorismith/882583896/&quot; title=&quot;IMG_0040.JPG&quot;&gt;&lt;img src=&quot;http://farm2.static.flickr.com/1233/882583896_90fad7b9ea_m.jpg&quot; width=&quot;180&quot; height=&quot;240&quot; alt=&quot;IMG_0040.JPG&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
        </content>
        <author>
            <name>Dori Smith</name>
            <uri>http://www.flickr.com/people/dorismith/</uri>
        </author>
        <link rel="enclosure" type="image/jpeg" href="http://farm2.static.flickr.com/1233/882583896_90fad7b9ea_m.jpg" />  
        <category term="winery" scheme="http://www.flickr.com/photos/tags/" />
        <category term="sonomacounty" scheme="http://www.flickr.com/photos/tags/" />
        <category term="sculptures" scheme="http://www.flickr.com/photos/tags/" />
        <category term="dorismith" scheme="http://www.flickr.com/photos/tags/" />
        <category term="paradiseridge" scheme="http://www.flickr.com/photos/tags/" />
        <category term="paradiseridgesculptures" scheme="http://www.flickr.com/photos/tags/" />
    </entry>
</feed>

3 个答案:

答案 0 :(得分:3)

鉴于这直接来自 JavaScript&amp;用于Web的Ajax:Visual QuickStart Guide,第7版 1 ,您在第329-335页的(逐行)说明中遇到了什么问题?

1 由我共同撰写,顺便说一句。

答案 1 :(得分:0)

textContent包含(惊喜!)节点的文本内容,例如其内容以及所有标签都被剥离。

答案 2 :(得分:0)

.innerHTML表示目标元素中的HTML。

例如

Hello World!

元素<。p>

的.innerHTML是'Hello World!'。

我不了解你的其他查询。

虽然我相信.textContent可能意味着XML节点的文本内容