小东:
突然发现,现在的JS代码怎么和99年用的JS代码差别这么大了啊,又得重新学了
[阅读: 310] 2005-01-28 14:14:31
/**
*
* Used to merge more concurrent response from Amazon.
*
* @fileName AmazonParser.js
* @$LastChangedDate: 2004-05-07 16:58:00 +0200 (Fri, 07 May 2004) $
* @author Fabio Serra <faser@faser.net>
* @copyright Fabio Serra (The Initial Developer of the Original Code)
* @license Mozilla Public License Version 1.1
*
*/
/**
* Construct a new AmazonParser object
* @class This class is used to manage all the XML Amazon response arrived from
* one or multiple request in the same Connection
* @constructor
* @return A new AmazonParser object
*
*/
function AmazonParser(xmlDoc) {
if(xmlDoc) {this.xmlDoc = xmlDoc;}
}
/**
* Get the total pages available for the search
* @type int
*/
AmazonParser.prototype.getTotalPages = function() {
if(!this.xmlDoc) {return false;}
var path = "//ProductInfo/TotalPages";
var xpathResult = this.xmlDoc.evaluate(path,this.xmlDoc,null,XPathResult.ANY_TYPE,null).iterateNext();
if(xpathResult != null) {
return xpathResult.textContent;
} else {
throw("AmazonParser Exception: TotalPages not found");
}
}
/**
* Get the total items for the search
* @type int
*/
AmazonParser.prototype.getTotalResults = function() {
if(!this.xmlDoc) {return false;}
var path = "//ProductInfo/TotalResults";
var xpathResult = this.xmlDoc.evaluate(path,this.xmlDoc,null,XPathResult.ANY_TYPE,null).iterateNext();
if(xpathResult != null) {
return xpathResult.textContent;
} else {
throw("AmazonParser Exception: TotalResult not found");
}
}
/**
* Check if the valid XML has the error tag
* @type bool
*/
AmazonParser.prototype.hasError = function() {
var path = "//ErrorMsg";
var xpathResult = this.xmlDoc.evaluate(path,this.xmlDoc,null,XPathResult.ANY_TYPE,null).iterateNext();
if(xpathResult != null) {
return true;
} else {
return false;
}
}
/**
* Get the error message from the valid xml Amazon response
* @throw ErrorMsg not found
* @type string
*/
AmazonParser.prototype.getErrorMsg = function() {
var path = "//ErrorMsg";
var xpathResult = this.xmlDoc.evaluate(path,this.xmlDoc,null,XPathResult.ANY_TYPE,null).iterateNext();
if(xpathResult != null) {
return xpathResult.textContent;
} else {
throw("AmazonParser Exception: ErrorMsg not found");
}
}
/**
* Count how many Details node are present in the xml response
* type int
*/
AmazonParser.prototype.getNrDetails = function() {
var el = this.xmlDoc.getElementsByTagName("Details");
if(el.length) {
return el.length;
}else{
return 0;
}
}
/**
* Merge the new Amazon document with the current document
* Once the documents are merged TotalPages, TotalResults are not more valid
* because they referer to the first original amazon page.
* @param {XML Document} docToMerge
* @return true if the document has been merged or created
* @type bool
*/
AmazonParser.prototype.mergeDocument = function(docToMerge) {
if(!this.xmlDoc) {
this.xmlDoc = docToMerge;
return true;
}
//I can merge an amazon response only if there are not error
if(this.hasError()) {return false;}
var reference = this.xmlDoc.getElementsByTagName('ProductInfo').item(0);
var details = docToMerge.getElementsByTagName('Details');
if(details.length > 0) {
for (var i=0;i< details.length ;i++) {
var node = details.item(i).cloneNode(true);
reference.appendChild(node);
}
}
return true;
}
/**
* Add an empty review tag if it's missing from heavy search. In this way the life
* is more easy for the DisplayController.
* @return void
*/
AmazonParser.prototype.setMissingReviews = function() {
var parser = new DOMParser();
var strTag = "<Reviews/>"
var reviews;
var nodes = this.xmlDoc.getElementsByTagName("Details");
for(var i= 0; i < nodes.length; i++) {
reviews = nodes[i].getElementsByTagName("Reviews").item(0);
if(reviews == null) {
innerXML(nodes.item(i),strTag);
}
}
}