<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: read code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 27 Jul 2008 08:30:05 GMT</pubDate>
    <description>DZone Snippets: read code</description>
    <item>
      <title>Reading an XML file using JavaScript</title>
      <link>http://snippets.dzone.com/posts/show/5091</link>
      <description>Reads an xml file using JavaScript from a web page. Files used: loadxml.js (main script), readxml.js (client script), and readxml.html. The code for loadxml.js was copied from the article &lt;a href="http://www.devarticles.com/c/a/JavaScript/JavaScript-and-XML/"&gt;JavaScript and XML&lt;/a&gt; [devarticles.com]&lt;br /&gt;&lt;br /&gt;file: loadxml.js&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  XML.load = function(url) {&lt;br /&gt;    var xmldoc = XML.newDocument();&lt;br /&gt;    xmldoc.async = false;  &lt;br /&gt;    xmldoc.load(url);&lt;br /&gt;    return xmldoc;  &lt;br /&gt;  };&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;XML.newDocument = function(rootTagName, namespaceURL) {&lt;br /&gt;    if (!rootTagName) rootTagName = "";&lt;br /&gt;    if (!namespaceURL) namespaceURL = "";&lt;br /&gt;&lt;br /&gt;    if (document.implementation &amp;&amp; document.implementation.createDocument) {&lt;br /&gt;        // This is the W3C standard way to do it&lt;br /&gt;        return document.implementation.createDocument(namespaceURL, &lt;br /&gt;                       rootTagName, null);&lt;br /&gt;    }&lt;br /&gt;    else { // This is the IE way to do it&lt;br /&gt;        // Create an empty document as an ActiveX object&lt;br /&gt;        // If there is no root element, this is all we have to do&lt;br /&gt;        var doc = new ActiveXObject("MSXML2.DOMDocument");&lt;br /&gt;&lt;br /&gt;        // If there is a root tag, initialize the document&lt;br /&gt;        if (rootTagName) {&lt;br /&gt;            // Look for a namespace prefix&lt;br /&gt;            var prefix = "";&lt;br /&gt;            var tagname = rootTagName;&lt;br /&gt;            var p = rootTagName.indexOf(':');&lt;br /&gt;            if (p != -1) {&lt;br /&gt;                prefix = rootTagName.substring(0, p);&lt;br /&gt;                tagname = rootTagName.substring(p+1);&lt;br /&gt;            }&lt;br /&gt;&lt;br /&gt;            // If we have a namespace, we must have a namespace prefix&lt;br /&gt;            // If we don't have a namespace, we discard any prefix&lt;br /&gt;            if (namespaceURL) {&lt;br /&gt;                if (!prefix) prefix = "a0"; // What Firefox uses&lt;br /&gt;            }&lt;br /&gt;            else prefix = "";&lt;br /&gt;&lt;br /&gt;            // Create the root element (with optional namespace) as a&lt;br /&gt;            // string of text&lt;br /&gt;            var text = "&lt;" + (prefix?(prefix+":"):"") + tagname +&lt;br /&gt;                (namespaceURL&lt;br /&gt;                 ?(" xmlns:" + prefix + '="' + namespaceURL +'"')&lt;br /&gt;                 :"") +&lt;br /&gt;                "/&gt;";&lt;br /&gt;            // And parse that text into the empty document&lt;br /&gt;            doc.loadXML(text);&lt;br /&gt;        }&lt;br /&gt;        return doc;&lt;br /&gt;    }&lt;br /&gt;}; &lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;file: readxml.js&lt;br /&gt;&lt;code&gt;&lt;br /&gt;function readXmlFile() {&lt;br /&gt;  url = 'http://rorbuilder.info/pl/test123';&lt;br /&gt;&lt;br /&gt;  doc = XML.load(url);&lt;br /&gt;  alert(doc.documentElement.firstChild.nextSibling.firstChild.nodeValue);&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;file: readxml.html&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"&lt;br /&gt;  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;&lt;br /&gt;&lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"&gt;&lt;br /&gt;  &lt;head&gt;&lt;br /&gt;    &lt;title&gt;Read an XML file&lt;/title&gt;&lt;br /&gt;    &lt;meta http-equiv="Content-Type" content="text/html;charset=utf-8"/&gt;&lt;br /&gt;    &lt;script type="text/javascript" src="js/loadxml.js"/&gt;&lt;br /&gt;    &lt;script type="text/javascript" src="js/readxml.js"/&gt;&lt;br /&gt;  &lt;/head&gt;&lt;br /&gt;&lt;br /&gt;  &lt;body&gt;&lt;br /&gt;    &lt;h1&gt;ReadXML&lt;/h1&gt;&lt;br /&gt;    &lt;p&gt;&lt;br /&gt;       If all goes well you should see an alert box display the message 'testing 123'&lt;br /&gt;   &lt;/p&gt;&lt;br /&gt;    &lt;p&gt;Press the 'go' button to read the xml file '&lt;a href="test123"&gt;test123&lt;/a&gt;' &lt;br /&gt;       from the web server. &lt;br /&gt;       &lt;input type="button" onclick="readXmlFile()" value="go"/&gt;&lt;br /&gt;    &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;    &lt;br /&gt;  &lt;/body&gt;&lt;br /&gt;&lt;/html&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Note: To avoid the cross-site security error message 'permission denied', store all the files on the same web server. &lt;br /&gt;&lt;br /&gt;To test the above code try out the &lt;a href="http://rorbuilder.info/pl/readxml.html" title="javascript xml demo"&gt;demo to read the xml file&lt;/a&gt; [rorbuilder.info].</description>
      <pubDate>Sat, 02 Feb 2008 22:53:27 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5091</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>read-url-with-basic-auth</title>
      <link>http://snippets.dzone.com/posts/show/2055</link>
      <description>&lt;code&gt;&lt;br /&gt;read-url-with-basic-auth: func [url key] [&lt;br /&gt;    read/custom url compose/deep [&lt;br /&gt;        header [Authorization: (join "Basic " key)]&lt;br /&gt;    ]&lt;br /&gt;&lt;br /&gt;]&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 17 May 2006 01:49:38 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2055</guid>
      <author>gregg.irwin (Gregg Irwin)</author>
    </item>
    <item>
      <title>load a CSV file</title>
      <link>http://snippets.dzone.com/posts/show/1281</link>
      <description>&lt;code&gt;&lt;br /&gt;    read-csv: func [&lt;br /&gt;        "Load and parse a delimited text file."&lt;br /&gt;        file [file!]&lt;br /&gt;        /with&lt;br /&gt;            delimiter&lt;br /&gt;        /local lines&lt;br /&gt;    ][&lt;br /&gt;        if not with [delimiter: ","]&lt;br /&gt;        lines: read/lines file&lt;br /&gt;        forall lines [&lt;br /&gt;            change/only lines parse/all first lines delimiter&lt;br /&gt;        ]&lt;br /&gt;        ; head lines  ;&lt;- needed for View &lt; 1.3&lt;br /&gt;    ]&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 23 Jan 2006 02:05:09 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1281</guid>
      <author>gregg.irwin (Gregg Irwin)</author>
    </item>
    <item>
      <title>PHP READ TAB SEPARATED FILE</title>
      <link>http://snippets.dzone.com/posts/show/1006</link>
      <description>// OPENS A TAB SEPARATED FILE&lt;br /&gt;// PUTS DATA INTO AN ARRAY AND SPLITS IT BY TAB&lt;br /&gt;// LOOPS THROUGH CODE AND PRINT&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;&lt;br /&gt;$filename = "Book1.txt";&lt;br /&gt;$fd = fopen($filename,"r");&lt;br /&gt;$contents = fread ($fd,filesize ($filename));&lt;br /&gt;fclose($fd);&lt;br /&gt;&lt;br /&gt;$splitcontents = explode(" ", $contents);&lt;br /&gt;$counter = 0;&lt;br /&gt;&lt;br /&gt;foreach($splitcontents as $data){&lt;br /&gt;	echo $counter++;&lt;br /&gt;	echo ":  " . $data;&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;?&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 22 Dec 2005 12:09:47 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1006</guid>
      <author>smartcookie ()</author>
    </item>
    <item>
      <title>Reading bytes value</title>
      <link>http://snippets.dzone.com/posts/show/740</link>
      <description>&lt;code&gt;&lt;br /&gt;from struct import unpack&lt;br /&gt;f = open('data.bin', 'rb')&lt;br /&gt;&lt;br /&gt;b = f.read(1)&lt;br /&gt;b2 = f.read(2)&lt;br /&gt;b4 = f.read(4)&lt;br /&gt;&lt;br /&gt;n = ord(b)  # from char to int&lt;br /&gt;n4 = unpack('&gt;L', b4)  # big endian&lt;br /&gt;n4 = unpack('&lt;L', b4)  # little endian&lt;br /&gt;n = unpack('&gt;h', b2)   # big endian short&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 18 Sep 2005 14:45:23 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/740</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
  </channel>
</rss>
