⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jsdoc-project-summary.xml

📁 sarissa用于支持多浏览器的浏览及编程
💻 XML
📖 第 1 页 / 共 2 页
字号:
            To serialize an XML DOM Node simply feed it to an <code>XMLSerializer</code> object:         </p>      <pre xml:space="preserve">var xmlString = new XMLSerializer().serializeToString(someXmlDomNode);</pre>      </div>    <div class="section">      <h3>        <a id="parseerror">How to check for parsing errors</a>      </h3>        <p>            You can chack for and get a human-readable description of the error using             the Sarissa getParseErrorText method, passing the document as the argument:         </p>        <pre>if(Sarissa.getParseErrorText(oDomDoc) == Sarissa.PARSED_OK){    // The document was parsed/loaded just fine, go on    doSomething(oDomDoc);} else{    // The document was not loaded correctly! Inform the user:    alert(Sarissa.getParseErrorText(oDomDoc));};</pre>        <p>            Sarissa.getParseErrorText will return one of:        </p>        <ul>            <li><code>Sarissa.PARSED_OK</code> if the document was parsed with no errors</li>            <li><code>Sarissa.PARSED_EMPTY</code> if the document is empty (this may occur instead of an error using XmlHttpRequest)</li>            <li><code>Sarissa.PARSED_UNKNOWN_ERROR</code> if the document was not loaded for an unknown reason</li>            <li>A human readable description of the parsing error</li>        </ul>         <p>            <span class="bold">Tip</span>: Wrap the result of Sarissa.getParseErrorText            in a <code>&quot;pre&quot;</code> element if you want to render it.        </p>      <p>            If you have used the deprecated .load methods, you can also use            the <code>parseError</code> property. The property always gives an            integer, anything other than zero signals an error.         </p>      <pre xml:space="preserve">// ...oDomDoc.async = false;oDomDoc.load("someDocument.xml");if(oDomDoc.parseError.errorCode != 0)   alert("not well formed or other error!");else   alert("loaded ok");</pre>    </div>    <div class="section">      <h3>        <a id="transform">How to transform a DOM Document Object with XSLT</a>      </h3>            <p>            Both Mozilla and IE provide JavaScript interfaces to control XSLT            transformations thanks to Transformiix and MSXML3 (or above)            respectively. MSXML3+ is available with IE6 or as a seperate            instalation.         </p>        <p>            Sarissa emulates Mozilla's XSLTProcessor for Internet Explorer.            This object allows reusability of stylsheets; with it you can use the same stylesheet             on more than one source file. You use the XSLTProcessor to control transformations and             set / get stylesheet parameters as in the following example:        </p>        <pre xml:space="preserve">// create an instance of XSLTProcessorvar processor = new XSLTProcessor();// create a DOM Document containing an XSLT stylesheetvar xslDoc = Sarissa.getDomDocument();var xslStr = "&lt;?xml version='1.0' encoding='UTF-8'?&gt;"+    "&lt;xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform' &gt;"+        "&lt;xsl:output method='html' version='1.0' encoding='UTF-8' indent='yes'/&gt;"+    "&lt;xsl:param name='title'&gt;&lt;xsl:value-of select=\"'default title'\"/&gt;&lt;/xsl:param&gt;"+        "&lt;xsl:template match='/'&gt;&lt;p class='test' title='{$title}'&gt;test&lt;/p&gt;"+        "&lt;/xsl:template&gt;&lt;xsl:template match='@*'&gt;"+        "&lt;/xsl:template&gt;&lt;/xsl:stylesheet&gt;";xslDoc = (new DOMParser()).parseFromString(xslStr, "text/xml");// make the stylesheet reusable by importing it in the // XSLTProcessorprocessor.importStylesheet(xslDoc);// now XSLTProcessor is the 'proxy' for our stylesheet,// the function below demonstrates usagefunction test(paramValue) {    // set a stylesheet parameter    processor.setParameter(null, "title", paramValue);    // create source document    var xmlDoc = Sarissa.getDomDocument("http://foo.org/ns/uri","foo", null);    // transform the document     var newDocument = processor.transformToDocument(xmlDoc);    // show transformation results    alert(new XMLSerializer().serializeToString(newDocument));}// test the above functiontest("test 1");</pre>      </div>      <div class="section">        <h3>          <a id="updateContentFromURI">How to update areas in your webpage from remote (optionally transformed) XML documents or XML DOM Nodes (oneliner!)</a>        </h3>        <p>        Loading a DOM document from a URL, transforming it and using it to update the content of an HTML element         is a very common task if you are into "AJAX" apps. With Sarissa you can do it in a single line of code:        </p>        <pre xml:space="preserve">          Sarissa.updateContentFromURI(sFromUrl, oTargetElement, xsltproc, callback, skipCache);</pre>        <p>          The parameters used here are:        </p>        <ol>          <li><code>sFromUrl</code>: the URL to make the request to, e.g. "http://localhost/mydoc.xml"</li>           <li><code>oTargetElement</code>: the element to update, e.g. <code>document.getElementById('content')</code></li>           <li><code>xsltproc</code> (optional): the transformer to use on the XML Document before updating the target element with it </li>          <li><code>callback</code> (optional): a Function object to execute once the update is finished successfuly, called as callback(oNode, oTargetElement)</li>           <li><code>skipCache</code> (optional): whether to skip any cache</li>         </ol>        <p>Quite similarly, if you want to use an XML DOM object instead of a remote XML document simply take a look at <code>Sarissa.updateContentFromNode</code></p>      </div>    <div class="section">      <h3>        <a id="xpath">How to use XPath from JavaScript to select Nodes from an XML Document</a>      </h3>      <p>            Mozilla fully implements <a href="http://www.w3.org/TR/DOM-Level-3-XPath">DOM Level 3 XPath</a> so it was pretty trivial to implement IE's basic             <code>selectNodes</code> and <code>selectSingleNode</code> methods, with full namespaces support. This is available in sarissa_ieemu_xpath.js.            Actually IE also needs the proprietary <code>setProperty</code> method for it's XPath implementation to work.             <code>setProperty</code> is used for a number of things in IE. First you'll have to use it to make XPath available            for a certain document:         </p>      <pre xml:space="preserve">oDomDoc.setProperty("SelectionLanguage", "XPath");</pre>      <p>In IE, using <code>selectNodes</code> or <code>selectSingleNode</code> without the above first will give an error.         Also, the same method with different parameters is used to allow IE to resolve namespace prefixes, for example:</p>      <pre xml:space="preserve">oDomDoc.setProperty("SelectionNamespaces",                     "xmlns:xhtml='http://www.w3.org/1999/xhtml'");</pre>      <p>If you want to allow IE to resolve multiple namespace prefixes, use a space delimited list like:</p>      <pre xml:space="preserve">oDomDoc.setProperty("SelectionNamespaces",                     "xmlns:xhtml='http://www.w3.org/1999/xhtml'                      xmlns:xsl='http://www.w3.org/1999/XSL/Transform'");</pre>      <p>Mozilla does not need any of the above. DOM L3 XPath is always available and namespaces are resolved err... automatically.         Below is an example of using <code>selectNodes</code> and <code>selectSingleNode</code> when Sarissa is available to provide         cross browser XPath functionality. For more documentation on these proprietary methods check with the documentation at the MSDN website (<a href="http://msdn.microsoft.com" title="link to Microsoft developer website">http://msdn.microsoft.com</a>).        I'm not providing a URL for that as they constantly change their URLs.</p>      <pre xml:space="preserve">&lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"    "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"&gt;&lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"&gt;&lt;head&gt;    &lt;title&gt;Untitled&lt;/title&gt;    &lt;script type="text/javascript" src="sarissa.js"&gt;    &lt;/script&gt;    &lt;script type="text/javascript" src="sarissa_ieemu_xpath.js"&gt;    &lt;/script&gt;    &lt;script type="text/javascript"&gt;    &lt;!--function testXpath(){    var xmlDoc = Sarissa.getDomDocument();    var objNodeList;    var xmlStr = "&lt;?xml version='1.0' encoding='UTF-8'?&gt;"+        "&lt;xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'&gt;"+        "&lt;xsl:output method='xml' version='1.0' encoding='UTF-8' indent='yes'/&gt;"+        "&lt;xsl:template match='*'&gt;&lt;/xsl:template&gt;&lt;xsl:template match='@*'&gt;"+        "&lt;/xsl:template&gt;&lt;/xsl:stylesheet&gt;";        xmlDoc = (new DomParser()).parseFromString(xmlStr, "text/xml");        <span style="bold">// the following two lines are needed for IE</span>    xmlDoc.setProperty("SelectionNamespaces", "xmlns:xsl='http://www.w3.org/1999/XSL/Transform'");    xmlDoc.setProperty("SelectionLanguage", "XPath");        testSelectNodesOn(xmlDoc, "//xsl:template");    testSelectNodesOn(xmlDoc.documentElement, "//xsl:template");    testSelectNodesOn((xmlDoc.documentElement.getElementsByTagName("*"))[0], "//xsl:template");}function testSelectNodesOn(domNode, sXpath){    alert("testing selectNodes("+sXpath+") on a "+domNode);    var objNodeList = domNode.selectNodes(sXpath);    for(i=0;i&lt;objNodeList.length;i++)        alert(new XMLSerializer().serializeToString(objNodeList[i]));    alert("testing selectSingleNode("+sXpath+") on a "+domNode);    var oElem = domNode.selectSingleNode(sXpath);    alert(oElem+"\n"+new XMLSerializer().serializeToString(oElem));};    //--&gt;    &lt;/script&gt;&lt;/head&gt;&lt;body&gt;&lt;button onclick="testXpath()"&gt;test xpath&lt;/button&gt;&lt;/body&gt;&lt;/html&gt;</pre>    </div>    <div class="section">      <h3>        <a id="nondom2xml">How to serialize non-DOM objects to XML </a>      </h3>      <p>      You can easily convert any non DOM object to XML using the <code>Sarissa.xmlize</code>       method. Sarissa will preserve the structure and naming of the object graph, translating it to an XML tree.       Collection items are translated to <code>array-item</code> elements. For an example, the following lines:      </p>      <pre xml:space="preserve">// create an object hierarchy           book.chapters = new Array();book.chapters[0] = "Kingdom of Tags";book.chapters[1] = "Fall";book.chapters[2] = "Final battle";        book.chapters[3] = "Characters that need to be escaped: &lt;&lt; &lt;&lt; \"' \"\"\"&amp;&amp;'' &lt; &gt; &amp; ' \" ";book.chapters[4] = "Epilogue";book.editor = "Manos Batsis";var publisher = new Object();publisher.name = "Some Publisher";book.publisher = publisher;// serialize to an XML stringvar s = Sarissa.xmlize(book, "book");alert("Generated XML:\n"+s)</pre>        <p>will generate the markup below:</p>        <pre xml:space="preserve">&lt;book&gt;   &lt;chapters&gt;      &lt;array-item key="0"&gt;Kingdom of fools&lt;/array-item&gt;      &lt;array-item key="1"&gt;Fall&lt;/array-item&gt;      &lt;array-item key="2"&gt;Final battle&lt;/array-item&gt;      &lt;array-item key="3"&gt;         Characters that need to be escaped: &amp;lt;&amp;lt; &amp;lt;&amp;lt;          &amp;quot;&amp;apos;          &amp;quot;&amp;quot;&amp;quot;&amp;amp;&amp;amp;&amp;apos;&amp;apos;          &amp;lt; &amp;gt; &amp;amp; &amp;apos; &amp;quot;       &lt;/array-item&gt;      &lt;array-item key="4"&gt;Epilogue&lt;/array-item&gt;   &lt;/chapters&gt;   &lt;editor&gt;Manos Batsis&lt;/editor&gt;   &lt;publisher&gt;      &lt;name&gt;Some Publisher&lt;/name&gt;   &lt;/publisher&gt;&lt;/book&gt;</pre>    </div>        <p>    <a href="http://sourceforge.net">        <img src="http://sourceforge.net/sflogo.php?group_id=75155&amp;type=1" width="88" height="31" alt="SourceForge.net Logo" />    </a>    </p></div>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -