JavaScript reads XML file implementation program

Source: Internet
Author: User
The code is as follows Copy Code

1, read the XML file through JS, the main is to judge each browser

View Code

Load XML document
var loadxml = function (xmlFile) {
var xmldoc;
if (window. ActiveXObject) {
xmldoc = new ActiveXObject (' Microsoft.XMLDOM ');//ie browser
Xmldoc.async = false;
Xmldoc.load (XmlFile);
}
else if (Isfirefox=navigator.useragent.indexof ("Firefox") >0) {//Firefox browser
else if (document.implementation && document.implementation.createDocument) {//This is mainly for Google browser processing
xmldoc = Document.implementation.createDocument (",", null);
Xmldoc.load (XmlFile);
}
else{//Google Browser
var xmlhttp = new window. XMLHttpRequest ();
Xmlhttp.open ("Get", xmlfile,false);
Xmlhttp.send (NULL);
if (xmlhttp.readystate = = 4) {
xmldoc = xmlhttp.responseXML.documentElement;
}
}

return xmldoc;
}

First, the XML object is judged
var checkxmldocobj = function (xmlFile) {
var xmldoc = Loadxml (xmlFile);
if (xmldoc = = null) {
Alert (' Your browser does not support XML file reads, so this page prohibits your operation, the recommended use of IE5.0 above can solve this problem! ');
Window.location.href = '.. /err.html ';

}
return xmldoc;
}

2. Display the data from the read XML file to the HTML document

View Code
<script type= "Text/javascript" language= "JavaScript" >
var xmldoc = Checkxmldocobj ('.. /openclass.xml '); Read the data in the XML file
var a = document.getElementsByTagName ("a");//Get all a tags
$ (document). Ready (function () {
VAR nodes;
if ($.browser.msie) {//Note the differences between browsers
nodes = Xmldoc.getelementsbytagname (' collage ') [0].childnodes; Read the data that needs to be displayed in the XML file
}
else if (Isfirefox=navigator.useragent.indexof ("Firefox") >0) {
nodes = Xmldoc.getelementsbytagname (' collage ') [0].children; Read the data that needs to be displayed in the XML file
}
else{
nodes = Xmldoc.getelementsbytagname (' resource ');
}

for (var i = 0; i < a.length; i++) {
if (A[i].parentnode.nodename = "SPAN") {
for (var j = 0; J < Nodes.length; J + +) {
var resource = Nodes[j];
var url = resource.getattribute (' url ');
var href=$ (A[i]). attr ("href");
if (href = = URL) {
var count = Resource.getattribute (' click ');
var span = document.createelement ("div");
var str = document.createtextnode ("CTR:" + count);
Span.appendchild (str);
var div = A[i].parentnode.parentnode;
Div.appendchild (span);
Break
}
}
}
}
});
$ (function () {//) increased click through GET request
$ (a). MouseDown (function () {
var href = $ (this). attr ("href");
$.get (".. /receive.ashx ", {url:href,rd:math.random ()}, function (msg) {

});
})
})
</script>

3, update the corresponding XML file on the server by updating the ashx file

public void ProcessRequest (HttpContext context)
{
Context. Response.ContentType = "Text/plain";
string src = context. request.querystring["url"];
String path = context. Server.MapPath ("Openclass.xml"); Open an XML file
XmlDocument xmldoc = new XmlDocument ();
Xmldoc.load (path); Note that you cannot use the Xmlload () method
XmlNodeList nodeslist = xmldoc.selectnodes ("/collage/resource"); Find the corresponding node
foreach (XmlNode node in nodeslist)
{
XmlElement XE = (xmlelement) node;
if (XE. GetAttribute ("url") = = src)
{
int count = Int. Parse (XE. GetAttribute ("click"));
Count = count + 1;
Xe. SetAttribute ("click", Count.) ToString ()); Update content
}
}
Xmldoc.save (path); Save

}

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.