Javascript 读取 XML String 或 XML File

function listAllStates()
{ 

var xmlDoc;
var xmlString = '<states><state>wei</state></states>';

    // Mozilla and Netscape browsers
    if (document.implementation.createDocument) {
        var parser = new DOMParser()
        xmlDoc = parser.parseFromString(xmlString, "application/xml")
    // MSIE
    } else if (window.ActiveXObject) {
        xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
        xmlDoc.async="false"
        xmlDoc.loadXML(xmlString)
    }
   

  var x = xmlDoc.getElementsByTagName('state');

  alert (xmlDoc);
  alert (x.length);
}
 
 listAllStates();

 

写道
相关链接:
http://diveintogreasemonkey.org/patterns/parse-xml.html
http://developer.taboca.com/cases/en/client-javascript-dom-parser/

Import XML from a file
http://www.quirksmode.org/dom/importxml.html

 

写道
I created a very basic function which allows you to convert XML in a string type, into an XML Document type, which gives you the benefits of XML with AJAX.

I'll write up a better explanation another time, as well as submit some other useful data conversion functions



<script type="text/javascript">
// Name: createXMLDocument
// Input: String
// Output: XML Document
jQuery.createXMLDocument = function(string)
{
var browserName = navigator.appName;
var doc;
if (browserName == 'Microsoft Internet Explorer')
{
doc = new ActiveXObject('Microsoft.XMLDOM');
doc.async = 'false'
doc.loadXML(string);
} else {
doc = (new DOMParser()).parseFromString(string, 'text/xml');
}
return doc;
}
</script>


Usage:


<script type="text/javascript">
var text = '<message><from>me</from><to>you</to></message>';
var doc = $.createXMLDocument(text);
</script>

 

你可能感兴趣的:(JavaScript,jquery,xml,Ajax,Microsoft)