Using Baidu RSS for JavaScript to operate XML as a news source example _ javascript skills

Source: Internet
Author: User
JavaScript uses Baidu RSS for XML operations as a news source example. If you need it, you can refer to the js operation xml source for dynamic news on the page.
See the JS source code below (saved as an rss. js file ):

The Code is as follows:


Var main = document. getElementById ("content"). getElementsByTagName ("DIV ");
/*
* The current directory contains a sub-folder named xml, and the referenced source is stored in the directory.
* The colon in each row is followed by the file name and the xml source address (you can download the xml file from the source address and save it as the corresponding file name)
* You can right-click the address below and select Save as the target. After the download, the txt file is obtained and the extension is changed 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 root node of the XML document
Var root = xml.doc umentElement;
// Obtain the item node in the rss xml Source
Var items = root. getElementsByTagName ("item ");
// Create a DOm object-RSS title
Var head = document. createElement ("dt ");
Head. setAttribute ("style", "background-color: # ccc; cursor: pointer ;");
If (isIE) {// IE operation
Head. innerHTML =""+ (Root. getElementsByTagName (" title ") [0]. text). substring (2, 6) +""+ Root. getElementsByTagName (" pubDate ") [0]. text + "";
Target. appendChild (head );
// Create a DOm object-RSS list
Var ul = document. createElement ("ul ");
// Add the list to the DIV container
Target. appendChild (ul );
// Cyclically output the daily news to li, where items. length is the number of news entries
For (I = 0; I <items. length; I ++ ){
// Create the DOM object li to store news
Var li = document. createElement ("li ");
// Create a DOM Hyperlink Object
Var lk = document. createElement ("");
// Time
// The title attribute of the hyperlink, used to save news text at the same time
Lk. title = items [I]. selectSingleNode ("title"). text;
// Set the href attribute of the hyperlink
Lk. href = items [I]. selectSingleNode ("link"). text;
// The text displayed in the hyperlink. If it is longer than 15 characters, it is truncated and 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 the hyperlink to li
Li. appendChild (lk );
}
} Else {// non-IE operation
Head. innerHTML =""+ (Root. getElementsByTagName (" title ") [0]. textContent). substring (2, 6) +""+ Root. getElementsByTagName (" pubDate ") [0]. textContent + "";
Target. appendChild (head );
// Create a DOm object-RSS list
Var ul = document. createElement ("ul ");
// Add the list to the DIV container
Target. appendChild (ul );
// Cyclically output the daily news to li, where items. length is the number of news entries
For (I = 0; I <items. length; I ++ ){
// Create the DOM object li to store news
Var li = document. createElement ("li ");
// Create a DOM Hyperlink Object
Var lk = document. createElement ("");
// Time
// The title attribute of the hyperlink, used to save news text at the same time
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 in the hyperlink. If it is longer than 15 characters, it is truncated and 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 the hyperlink to li
Li. appendChild (lk );
}
}
}


Display page reference source code (stored as htm page)

The Code is as follows:
























Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.