<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: output code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 18 May 2008 05:44:56 GMT</pubDate>
    <description>DZone Snippets: output code</description>
    <item>
      <title>Output JavaScript variables from PHP</title>
      <link>http://snippets.dzone.com/posts/show/5342</link>
      <description>Class with useful static methods for outputting PHP values into JavaScript format.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;//+ Jonas Raoni Soares Silva&lt;br /&gt;//@ http://jsfromhell.com&lt;br /&gt;&lt;br /&gt;class JS{&lt;br /&gt;	//generic and maybe not the desired results xD&lt;br /&gt;	function value($o){&lt;br /&gt;		if($o === null)&lt;br /&gt;			return 'null';&lt;br /&gt;		$t = strtolower(gettype($o));&lt;br /&gt;		if($t == 'string' &amp;&amp; is_numeric($o) &amp;&amp; ($o[0] || strlen($o) == 1) || in_array($t, array('double', 'integer')))&lt;br /&gt;			$t = 'number';&lt;br /&gt;		elseif($t == 'string' &amp;&amp; preg_match('@^\d{4}(?:-\d{1,2}){1,2}(?: (?:\d{1,2}:){2}\d{1,2})?$@', $o)) //strtotime works also with "strange" values strtotime('x')&lt;br /&gt;			$t = 'date';&lt;br /&gt;		elseif($t == 'array' &amp;&amp; ($c = count($k = array_keys($o))) &amp;&amp; $k !== range(0, $c - 1))&lt;br /&gt;			$t = 'object';&lt;br /&gt;		elseif(!in_array($t, array('boolean', 'string', 'array', 'object')))&lt;br /&gt;			$t = 'string';&lt;br /&gt;		$t = 'from' . $t;&lt;br /&gt;		return self::$t($o);&lt;br /&gt;	}&lt;br /&gt;	function fromNumber($o){&lt;br /&gt;		return +$o . '';&lt;br /&gt;	}&lt;br /&gt;	function fromObject($o){&lt;br /&gt;		$r = array();&lt;br /&gt;		foreach($o as $n =&gt; $v)&lt;br /&gt;			$r[] = self::fromString($n) . ':' . self::value($v);&lt;br /&gt;		return '{' . implode(',', $r) . '}';&lt;br /&gt;	}&lt;br /&gt;	function fromBoolean($o){&lt;br /&gt;		return $o ? 'true' : 'false';&lt;br /&gt;	}&lt;br /&gt;	//$q = should quote? &lt;br /&gt;	//$c = char that will be used to quote&lt;br /&gt;	function fromString($o, $q = true, $c = '"'){&lt;br /&gt;		return ($p = $q ? $c : '') . preg_replace('/\r\n|\n\r|\r/', '\n', str_replace($c, '\\' . $c, str_replace('\\', '\\\\', $o))) . $p;&lt;br /&gt;	}&lt;br /&gt;	function fromArray($o){&lt;br /&gt;		$s = '';&lt;br /&gt;		foreach($o as $v)&lt;br /&gt;			$s .= ($s ? ',' : '') . self::value($v);&lt;br /&gt;		return '[' . $s . ']';&lt;br /&gt;	}&lt;br /&gt;	function fromDate($o){&lt;br /&gt;		(is_numeric($o) &amp;&amp; $o = +$o) || ($o = strtotime($o)) &gt; 0 || ($o = mktime());&lt;br /&gt;		$o = explode(',', date('Y,n,j,G,i,s', $o));&lt;br /&gt;		foreach($o as $i =&gt; $v)&lt;br /&gt;			$o[$i] = +$v;&lt;br /&gt;		return 'new Date(' . implode(',', $o)  . ')';&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Example&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;$o = new stdClass;&lt;br /&gt;$o-&gt;abc = 123;&lt;br /&gt;echo implode("\n&lt;br /&gt;", array(&lt;br /&gt;	JS::value('1984-07-22 11:30:12'),&lt;br /&gt;	JS::value('Test'),&lt;br /&gt;	JS::value(1234),&lt;br /&gt;	JS::value(true),&lt;br /&gt;	JS::value(array(1,2,3)),&lt;br /&gt;	JS::value(array('lala' =&gt; 'x')),&lt;br /&gt;	JS::value($o)&lt;br /&gt;));&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Wed, 09 Apr 2008 23:57:18 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5342</guid>
      <author>jonasraoni (Jonas Raoni Soares Silva)</author>
    </item>
    <item>
      <title>C#: Inserting New Line in Multiline Textbox</title>
      <link>http://snippets.dzone.com/posts/show/4381</link>
      <description>// Ref: http://www.geekpedia.com/Question19_How-can-I-insert-a-new-line-in-a-TextBox.html&lt;br /&gt;// Make sure MultiLine property is true and use "\r\n"&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  TextBox1.Text = "First line\r\nSecond line";&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 02 Aug 2007 13:04:40 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4381</guid>
      <author>thitiv (Thiti V. Sintopchai)</author>
    </item>
    <item>
      <title>Java: log4j Initialization Example</title>
      <link>http://snippets.dzone.com/posts/show/4013</link>
      <description>// Initialization for Basic Console Output&lt;br /&gt;// Ref: http://logging.apache.org/log4j/docs/manual.html&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt; import com.foo.Bar;&lt;br /&gt;&lt;br /&gt; // Import log4j classes.&lt;br /&gt; import org.apache.log4j.Logger;&lt;br /&gt; import org.apache.log4j.BasicConfigurator;&lt;br /&gt;&lt;br /&gt; public class MyApp {&lt;br /&gt;&lt;br /&gt;   // Define a static logger variable so that it references the&lt;br /&gt;   // Logger instance named "MyApp".&lt;br /&gt;   static Logger logger = Logger.getLogger(MyApp.class);&lt;br /&gt;&lt;br /&gt;   public static void main(String[] args) {&lt;br /&gt;&lt;br /&gt;     // Set up a simple configuration that logs on the console.&lt;br /&gt;     BasicConfigurator.configure();&lt;br /&gt;&lt;br /&gt;     logger.setLevel(Level.DEBUG); // optional if log4j.properties file not used&lt;br /&gt;     // Possible levels: TRACE, DEBUG, INFO, WARN, ERROR, and FATAL&lt;br /&gt;&lt;br /&gt;     logger.info("Entering application.");&lt;br /&gt;     Bar bar = new Bar();&lt;br /&gt;     bar.doIt();&lt;br /&gt;     logger.info("Exiting application.");&lt;br /&gt;   }&lt;br /&gt; }&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 14 May 2007 09:44:45 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4013</guid>
      <author>thitiv (Thiti V. Sintopchai)</author>
    </item>
    <item>
      <title>Java DOM: Printing the Content of a Node</title>
      <link>http://snippets.dzone.com/posts/show/4011</link>
      <description>&lt;code&gt;&lt;br /&gt;    try&lt;br /&gt;    {&lt;br /&gt;      // Set up the output transformer&lt;br /&gt;      TransformerFactory transfac = TransformerFactory.newInstance();&lt;br /&gt;      Transformer trans = transfac.newTransformer();&lt;br /&gt;      trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");&lt;br /&gt;      trans.setOutputProperty(OutputKeys.INDENT, "yes");&lt;br /&gt;&lt;br /&gt;      // Print the DOM node&lt;br /&gt;&lt;br /&gt;      StringWriter sw = new StringWriter();&lt;br /&gt;      StreamResult result = new StreamResult(sw);&lt;br /&gt;      DOMSource source = new DOMSource(node);&lt;br /&gt;      trans.transform(source, result);&lt;br /&gt;      String xmlString = sw.toString();&lt;br /&gt;&lt;br /&gt;      System.out.println(xmlString);&lt;br /&gt;    }&lt;br /&gt;    catch (TransformerException e)&lt;br /&gt;    {&lt;br /&gt;      e.printStackTrace();&lt;br /&gt;    }&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 14 May 2007 09:19:05 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4011</guid>
      <author>thitiv (Thiti V. Sintopchai)</author>
    </item>
    <item>
      <title>Simple Java DOM XML Processing Example</title>
      <link>http://snippets.dzone.com/posts/show/4009</link>
      <description>// Simple Java DOM XML Processing Example&lt;br /&gt;// Source: http://www.genedavis.com/library/xml/java_dom_xml_creation.php&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;br /&gt;import java.io.*;&lt;br /&gt;&lt;br /&gt;import org.w3c.dom.*;&lt;br /&gt;&lt;br /&gt;import javax.xml.parsers.*;&lt;br /&gt;&lt;br /&gt;import javax.xml.transform.*;&lt;br /&gt;import javax.xml.transform.dom.*;&lt;br /&gt;import javax.xml.transform.stream.*;&lt;br /&gt;&lt;br /&gt;public class DomXmlExample {&lt;br /&gt;&lt;br /&gt;    /**&lt;br /&gt;     * Our goal is to create a DOM XML tree and then print the XML.&lt;br /&gt;     */&lt;br /&gt;    public static void main (String args[]) {&lt;br /&gt;        new DomXmlExample();&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public DomXmlExample() {&lt;br /&gt;        try {&lt;br /&gt;            /////////////////////////////&lt;br /&gt;            // Creating an empty XML Document&lt;br /&gt;&lt;br /&gt;            // We need a Document&lt;br /&gt;            DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();&lt;br /&gt;            DocumentBuilder docBuilder = dbfac.newDocumentBuilder();&lt;br /&gt;            Document doc = docBuilder.newDocument();&lt;br /&gt;&lt;br /&gt;            ////////////////////////&lt;br /&gt;            // Creating the XML tree&lt;br /&gt;&lt;br /&gt;            // create the root element and add it to the document&lt;br /&gt;            Element root = doc.createElement("root");&lt;br /&gt;            doc.appendChild(root);&lt;br /&gt;&lt;br /&gt;            // create a comment and put it in the root element&lt;br /&gt;            Comment comment = doc.createComment("Just a thought");&lt;br /&gt;            root.appendChild(comment);&lt;br /&gt;&lt;br /&gt;            // create child element, add an attribute, and add to root&lt;br /&gt;            Element child = doc.createElement("child");&lt;br /&gt;            child.setAttribute("name", "value");&lt;br /&gt;            root.appendChild(child);&lt;br /&gt;&lt;br /&gt;            // add a text element to the child&lt;br /&gt;            Text text = doc.createTextNode("Filler, ... I could have had a foo!");&lt;br /&gt;            child.appendChild(text);&lt;br /&gt;&lt;br /&gt;            /////////////////&lt;br /&gt;            // Output the XML&lt;br /&gt;&lt;br /&gt;            // set up a transformer&lt;br /&gt;            TransformerFactory transfac = TransformerFactory.newInstance();&lt;br /&gt;            Transformer trans = transfac.newTransformer();&lt;br /&gt;            trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");&lt;br /&gt;            trans.setOutputProperty(OutputKeys.INDENT, "yes");&lt;br /&gt;&lt;br /&gt;            // create string from xml tree&lt;br /&gt;            StringWriter sw = new StringWriter();&lt;br /&gt;            StreamResult result = new StreamResult(sw);&lt;br /&gt;            DOMSource source = new DOMSource(doc);&lt;br /&gt;            trans.transform(source, result);&lt;br /&gt;            String xmlString = sw.toString();&lt;br /&gt;&lt;br /&gt;            // print xml&lt;br /&gt;            System.out.println("Here's the xml:\n\n" + xmlString);&lt;br /&gt;&lt;br /&gt;        } catch (Exception e) {&lt;br /&gt;            System.out.println(e);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 14 May 2007 08:04:45 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4009</guid>
      <author>thitiv (Thiti V. Sintopchai)</author>
    </item>
    <item>
      <title>Cannot modify header information - headers already sent by FIX</title>
      <link>http://snippets.dzone.com/posts/show/2575</link>
      <description>Buffers output and eliminates problem with spaces/line breaks at the beginning/end of files. Make sure to place ob_start(); before header(); is called.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;ob_start();&lt;br /&gt;&lt;br /&gt;header("Location: somepage.php"); //Redirect&lt;br /&gt;&lt;br /&gt;ob_end_flush();&lt;br /&gt;exit;&lt;br /&gt;?&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 10 Sep 2006 03:54:29 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2575</guid>
      <author>dezmarie (Desirae Beberniss)</author>
    </item>
    <item>
      <title>Output MySQL to a text file</title>
      <link>http://snippets.dzone.com/posts/show/1442</link>
      <description>// description of your code here&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;/opt/mysql/bin/mysql -u root -h mysql-server &lt; q4.sql &gt; ! out.txt&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 09 Feb 2006 17:44:05 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1442</guid>
      <author>offspinner ()</author>
    </item>
  </channel>
</rss>
