Home > Web Front-end > JS Tutorial > JavaScript operates XML using Baidu RSS as a news source example_javascript skills

JavaScript operates XML using Baidu RSS as a news source example_javascript skills

WBOY
Release: 2016-05-16 17:56:15
Original
1153 people have browsed it

js operates xml source, as the dynamic news of the page
, refer to the JS source code as follows (save as rss.js file):

Copy code The code is as follows:

var main = document.getElementById("content").getElementsByTagName("DIV");
/*
* There is a file named xml under the current directory subfolder of the directory in which the source referenced below is saved.
* The colon in each line below is preceded by the file name, followed by the xml source address (the xml file can be downloaded from the source address, and saved as the corresponding file name after downloading)
* You can right-click on the address below , select the target to save as, after downloading, you will get a txt file, just change the extension to xml
* movie:"http://news.baidu.com/n?cmd=1&class=film&tn=rss"
* woman:"http://news.baidu.com/n?cmd=1&class=healthnews&tn=rss"
* house:"http://news.baidu.com/n?cmd=1&class=housenews&tn= rss"
* car:"http://news.baidu.com/n?cmd=1&class=autonews&tn=rss"
* sport:"http://news.baidu.com/n?cmd= 1&class=sportnews&tn=rss"
* edu:"http://news.baidu.com/n?cmd=1&class=edunews&tn=rss"
*/
var RssSource = {
movie: "xml/movie.xml",
woman: "xml/woman.xml",
house: "xml/house.xml",
car: "xml/car.xml",
sport: "xml/sport.xml",
edu: "xml/edu.xml"
}
function Init() {
LoadXml(RssSource.movie, main[0]);
LoadXml(RssSource.woman, main[1]);
LoadXml(RssSource.house, main[2]);
LoadXml(RssSource.car, main[3]);
LoadXml(RssSource .sport, main[4]);
LoadXml(RssSource.edu, main[5]);
}
function LoadXml(url, target) {
var xml = null;
var isIE = true;
if (window.ActiveXObject) //IF IE
{
xml = new ActiveXObject("Microsoft.XMLDOM");
isIE = true;
} else if (document.implementation.createDocument) //IF FF
{
xml = document.implementation.createDocument("", "", null);
isIE = false;
}
xml .async = false;
xml.load(url);
//Get the XML document root node
var root = xml.documentElement;
//Get the item node in the RSS XML source
var items = root.getElementsByTagName("item");
//Create DOm object - RSS title
var head = document.createElement("dt");
head.setAttribute("style ", "background-color:#ccc;cursor:pointer;");
if (isIE) { //Operation when IE
head.innerHTML = "" (root.getElementsByTagName("title ")[0].text).substring(2, 6) " " root.getElementsByTagName("pubDate")[0].text "";
target.appendChild(head);
//Create DOm object - RSS list
var ul = document.createElement("ul");
//Add the list to the DIV container
target .appendChild(ul);
//Loop to output daily news to li, where items.length is the number of news items
for (i = 0; i < items.length; i ) {
/ /Create DOM object li to store news
var li = document.createElement("li");
//Create DOM hyperlink object
var lk = document.createElement("a");
//Time
//The title attribute of the hyperlink is also used to save the news text
lk.title = items[i].selectSingleNode("title").text;
//Set the hyperlink The href attribute
lk.href = items[i].selectSingleNode("link").text;
//The text displayed by the hyperlink, if it is longer than 15 characters, is intercepted and then added...
lk.innerHTML = lk.title.length > 18 ? lk.title.substring(0, 16) "....": lk.title;
//lk.innerText = lk.title;
//Add li to ul
ul.appendChild(li);
//Add hyperlink to li
li.appendChild(lk);
}
} else { //Operation when not IE
head.innerHTML = "" (root.getElementsByTagName("title")[0].textContent).substring(2, 6) " " root.getElementsByTagName("pubDate")[0].textContent "";
target.appendChild(head);
//Create DOm object - RSS list
var ul = document.createElement("ul");
//Add the list to the DIV container
target.appendChild(ul);
//Loop to output daily news to li, where items .length is the number of news items
for (i = 0; i < items.length; i ) {
//Create DOM object li to store news
var li = document.createElement("li" );
//Create DOM hyperlink object
var lk = document.createElement("a");
//Time
//The title attribute of the hyperlink is also used to save the news text
lk.title = items[i].getElementsByTagName("title")[0].textContent;
//Set the href attribute of the hyperlink
lk.href = items[i].getElementsByTagName(" link")[0].textContent;
//The text displayed by the hyperlink, if it is longer than 15 characters, intercept it and add...
lk.innerHTML = lk.title.length > 18 ?lk.title.substring(0, 16) "....": lk.title;
//lk.innerText = lk.title;
//Add li to ul
ul .appendChild(li);
//Add hyperlink to li
li.appendChild(lk);
}
}
}

显示页面参考源码(存为htm页面)
复制代码 代码如下:























CSS源码(存为index.css )
复制代码 代码如下:

a:link,a:visited,a:active {
text-decoration:none;
}
a:hover {
text-decoration:underline;
}
#pagebody {
margin:0 auto;
width:800px;
height:1200px;
border-left:dotted 1px gray;
border-right:dotted 1px gray;
background-color:#eee;
}
#header {
height:200px;
}
#banner {
height:160px;
background-color:#fff;
}
#content div {
width:380px;
height:270px;
border:solid 1px gray;
overflow:hidden;
background-color:#fff;
}
#content div ul li {
list-style-image:url(list.gif);
}
.left {
float:left;
margin-top:10px;
margin-left:10px;
}
.right {
float:right;
margin-top:10px;
margin-right:10px;
}
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template