Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Thiti V. Sintopchai http://thitiv.blogspot.com

« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS 

C#: Inserting New Line in Multiline Textbox

// Ref: http://www.geekpedia.com/Question19_How-can-I-insert-a-new-line-in-a-TextBox.html
// Make sure MultiLine property is true and use "\r\n"

   1  
   2    TextBox1.Text = "First line\r\nSecond line";

Java: log4j Initialization Example

// Initialization for Basic Console Output
// Ref: http://logging.apache.org/log4j/docs/manual.html

   1  
   2   import com.foo.Bar;
   3  
   4   // Import log4j classes.
   5   import org.apache.log4j.Logger;
   6   import org.apache.log4j.BasicConfigurator;
   7  
   8   public class MyApp {
   9  
  10     // Define a static logger variable so that it references the
  11     // Logger instance named "MyApp".
  12     static Logger logger = Logger.getLogger(MyApp.class);
  13  
  14     public static void main(String[] args) {
  15  
  16       // Set up a simple configuration that logs on the console.
  17       BasicConfigurator.configure();
  18  
  19       logger.setLevel(Level.DEBUG); // optional if log4j.properties file not used
  20       // Possible levels: TRACE, DEBUG, INFO, WARN, ERROR, and FATAL
  21  
  22       logger.info("Entering application.");
  23       Bar bar = new Bar();
  24       bar.doIt();
  25       logger.info("Exiting application.");
  26     }
  27   }

Java DOM: Printing the Content of a Node

   1  
   2      try
   3      {
   4        // Set up the output transformer
   5        TransformerFactory transfac = TransformerFactory.newInstance();
   6        Transformer trans = transfac.newTransformer();
   7        trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
   8        trans.setOutputProperty(OutputKeys.INDENT, "yes");
   9  
  10        // Print the DOM node
  11  
  12        StringWriter sw = new StringWriter();
  13        StreamResult result = new StreamResult(sw);
  14        DOMSource source = new DOMSource(node);
  15        trans.transform(source, result);
  16        String xmlString = sw.toString();
  17  
  18        System.out.println(xmlString);
  19      }
  20      catch (TransformerException e)
  21      {
  22        e.printStackTrace();
  23      }

Simple Java DOM XML Processing Example

// Simple Java DOM XML Processing Example
// Source: http://www.genedavis.com/library/xml/java_dom_xml_creation.php

   1  
   2  
   3  import java.io.*;
   4  
   5  import org.w3c.dom.*;
   6  
   7  import javax.xml.parsers.*;
   8  
   9  import javax.xml.transform.*;
  10  import javax.xml.transform.dom.*;
  11  import javax.xml.transform.stream.*;
  12  
  13  public class DomXmlExample {
  14  
  15      /**
  16       * Our goal is to create a DOM XML tree and then print the XML.
  17       */
  18      public static void main (String args[]) {
  19          new DomXmlExample();
  20      }
  21  
  22      public DomXmlExample() {
  23          try {
  24              /////////////////////////////
  25              // Creating an empty XML Document
  26  
  27              // We need a Document
  28              DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
  29              DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
  30              Document doc = docBuilder.newDocument();
  31  
  32              ////////////////////////
  33              // Creating the XML tree
  34  
  35              // create the root element and add it to the document
  36              Element root = doc.createElement("root");
  37              doc.appendChild(root);
  38  
  39              // create a comment and put it in the root element
  40              Comment comment = doc.createComment("Just a thought");
  41              root.appendChild(comment);
  42  
  43              // create child element, add an attribute, and add to root
  44              Element child = doc.createElement("child");
  45              child.setAttribute("name", "value");
  46              root.appendChild(child);
  47  
  48              // add a text element to the child
  49              Text text = doc.createTextNode("Filler, ... I could have had a foo!");
  50              child.appendChild(text);
  51  
  52              /////////////////
  53              // Output the XML
  54  
  55              // set up a transformer
  56              TransformerFactory transfac = TransformerFactory.newInstance();
  57              Transformer trans = transfac.newTransformer();
  58              trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
  59              trans.setOutputProperty(OutputKeys.INDENT, "yes");
  60  
  61              // create string from xml tree
  62              StringWriter sw = new StringWriter();
  63              StreamResult result = new StreamResult(sw);
  64              DOMSource source = new DOMSource(doc);
  65              trans.transform(source, result);
  66              String xmlString = sw.toString();
  67  
  68              // print xml
  69              System.out.println("Here's the xml:\n\n" + xmlString);
  70  
  71          } catch (Exception e) {
  72              System.out.println(e);
  73          }
  74      }
  75  }
« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS