($width) AND $width .= 'px'; $style = " style=\"width: $width\""; } $value = $value ? $value : date('H:i'); $s = ""; return $s; } // form_date('start', '2018-07-05') 为空则当前日期 function form_date($name, $value = 0, $width = FALSE) { $style = ''; if (FALSE !== $width) { is_numeric($width) AND $width .= 'px'; $style = " style=\"width: $width\""; } $value = $value ? $value : date('Y-m-d'); $s = ""; return $s; } /**用法 * * echo form_radio_yes_no('radio1', 0); * echo form_checkbox('aaa', array('无', '有'), 0); * * echo form_radio_yes_no('aaa', 0); * echo form_radio('aaa', array('无', '有'), 0); * echo form_radio('aaa', array('a'=>'aaa', 'b'=>'bbb', 'c'=>'ccc', ), 'b'); * * echo form_select('aaa', array('a'=>'aaa', 'b'=>'bbb', 'c'=>'ccc', ), 'a'); */ ?>javascript - How should I test if an object is a XML document (in a cross browser way) - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How should I test if an object is a XML document (in a cross browser way) - Stack Overflow

programmeradmin1浏览0评论

For an unit test, I want to be able to check if a certain returned object is a XML document. What is the best way to do so?

I am currently just testing for doc.implementation (the first DOM property that came to mind) but is there a better way? Also, is there a nice way to tell apart XML documents from HTML documents?

For an unit test, I want to be able to check if a certain returned object is a XML document. What is the best way to do so?

I am currently just testing for doc.implementation (the first DOM property that came to mind) but is there a better way? Also, is there a nice way to tell apart XML documents from HTML documents?

Share Improve this question edited Dec 30, 2011 at 14:31 hugomg asked Dec 29, 2011 at 19:38 hugomghugomg 70k29 gold badges164 silver badges255 bronze badges 2
  • doc.doctype is the doctype node. The doctype node should be XHTML rather then html. – Raynos Commented Dec 30, 2011 at 14:39
  • My documents aren't XHTML so they have no doctype. But thanks for the tip. – hugomg Commented Dec 30, 2011 at 22:20
Add a ment  | 

3 Answers 3

Reset to default 3

I'd have a look at the implementation of jQuery.isXMLDoc for ideas. It turns out that the code itself is in the Sizzle library, here:

Sizzle.isXML = function( elem ) {
    // documentElement is verified for cases where it doesn't yet exist
    // (such as loading iframes in IE - #4833) 
    var documentElement = (elem ? elem.ownerDocument || elem : 0).documentElement;

    return documentElement ? documentElement.nodeName !== "HTML" : false;
};
function isXML(xmlStr){
  var parseXml;

  if (typeof window.DOMParser != "undefined") {
    parseXml = function(xmlStr) {
      return (new window.DOMParser()).parseFromString(xmlStr, "text/xml");
    };
  } else if (typeof window.ActiveXObject != "undefined" && new window.ActiveXObject("Microsoft.XMLDOM")) {
    parseXml = function(xmlStr) {
      var xmlDoc = new window.ActiveXObject("Microsoft.XMLDOM");
      xmlDoc.async = "false";
      xmlDoc.loadXML(xmlStr);
      return xmlDoc;
    };
  } else {
    return false;
  }

  try {
    parseXml(xmlStr);
  } catch (e) {
    return false;
  }
  return true;      
}

I'm assuming that you're currently doing an implementation similar to http://www.javascriptkit./dhtmltutors/getxml3.shtml

If that's the case, I know it's not pretty but couldn't you simply just wrap it in try/catch? Or, do you need to know if it is XML and specifically not some other type. If that's the case I'm not sure you can without making some other assertions. A try catch will at least allow you to create an XML document from an object without throwing an error to the screen. You could assume then that if it loads into the DOM that it IS valid XML.

发布评论