Generating a simple XML file in java

August 5, 2011 Leave a comment
import java.io.File;
import org.w3c.dom.*;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException; 
import java.io.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;


public class GeneratingSampleXml {


	public static void main(String args[]) throws Exception{
		
		/*Creating an empty document*/
		DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
		Document doc = documentBuilder.newDocument();
		
		/*Creating an element. By default first element is the root element */
		Element root = doc.createElement("Book");
		doc.appendChild(root);
		
		/* Adding a comment */
		Comment comment = doc.createComment("a comment");
		doc.insertBefore(comment, root);
		
		/* Adding Child to the root element*/
		Element title = doc.createElement("Title");
		root.appendChild(title);
		title.insertBefore(doc.createTextNode("Java Performance Tuning"), title.getFirstChild());

		Element author = doc.createElement("Author");
		root.appendChild(author);
		author.insertBefore(doc.createTextNode("Ravleen kaur"), author.getFirstChild());

		
		TransformerFactory transfac = TransformerFactory.newInstance();
		Transformer trans = transfac.newTransformer();
		trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
		trans.setOutputProperty(OutputKeys.INDENT, "yes");


		
		StringWriter sw = new StringWriter();
		StreamResult result = new StreamResult(sw);
		DOMSource source = new DOMSource(doc);
		trans.transform(source, result);
		String xmlString = sw.toString();

		
		System.out.println("Here's the xml:\n\n" + xmlString);
	}
}

Run .sql in MySql

June 9, 2011 Leave a comment

Use the following command to run the script file in mysql db.

mysql> \. sourcefile.sql

Note :
1. There should not be semicolon at the end.
2. there should be a space between ‘\.’ and source file name(the complete path of the source file).

JConsole to monitor and manage JVM

April 14, 2011 Leave a comment
  • Jconsole is a JMX-compliant monitoring tool.
  • It can be used to monitor both local JVM (the same box on which jconsole is running) and remote (Other boxes) JVM on which the specified application is running that need to be monitored.
  • Jconsole allows to take the dump of the observations and you can use eclipse-mat to analyze the dump in more detailed.

    For more details on Jconsole you can refer to the following links

    • http://java.sun.com/developer/technicalArticles/J2SE/jconsole.html
    • http://download.oracle.com/javase/1.5.0/docs/guide/management/jconsole.html

      One of the simple way to remove duplicates from a List

      March 31, 2011 Leave a comment

      The following method is one of the simple way to remove duplicates from a List.

      private List<AdditionalInsured> removeDuplicates(List<AdditionalInsured> addInsList) {
      HashSet<AdditionalInsured> h = new HashSet<AdditionalInsured>(addInsList);
      addInsList.clear();
      addInsList.addAll(h);
      return addInsList;
      }

      Maven : Error occurred during initialization of VM

      February 23, 2011 Leave a comment

      issue : when we are trying to build a maven project from command prompt, we were encountered with the following error message

      “Error occurred during initialization of VM
      Could not reserve enough space for object heap
      Could not create the Java virtual machine.”

      Solution 1 : when we reduced the memory in maven options, we were able to build the project. Changed maven options are given below.

      MAVEN_OPTS = -Xmx512m -XX:MaxPermSize=128m

      Issue : “OutOfMemoryError” while building a maven project

      February 22, 2011 Leave a comment

      Issue : when we are trying to build a maven project, we encountered the following error

      “java.lang.OutOfMemoryError: Java heap space
      at com.sun.tools.javac.util.Name.fromUtf(Name.java:84)
      at com.sun.tools.javac.util.Name$Table.fromUtf(Name.java:510)
      at com.sun.tools.javac.util.ByteBuffer.toName(ByteBuffer.java:151)
      at com.sun.tools.javac.jvm.ClassWriter.typeSig(ClassWriter.java:407)
      at com.sun.tools.javac.jvm.ClassWriter.writeMethod(ClassWriter.java:945)
      at com.sun.tools.javac.jvm.ClassWriter.writeMethods(ClassWriter.java:1426)
      at com.sun.tools.javac.jvm.ClassWriter.writeClassFile(ClassWriter.java:1507)
      at com.sun.tools.javac.jvm.ClassWriter.writeClass(ClassWriter.java:1444)
      at com.sun.tools.javac.main.JavaCompiler.genCode(JavaCompiler.java:618)
      at com.sun.tools.javac.main.JavaCompiler.generate(JavaCompiler.java:1289)
      at com.sun.tools.javac.main.JavaCompiler.generate(JavaCompiler.java:1259)
      at com.sun.tools.javac.main.JavaCompiler.compile2(JavaCompiler.java:765)
      at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:730)
      at com.sun.tools.javac.main.Main.compile(Main.java:353)
      at com.sun.tools.javac.main.Main.compile(Main.java:279)
      at com.sun.tools.javac.main.Main.compile(Main.java:270)
      at com.sun.tools.javac.Main.compile(Main.java:69)
      at com.sun.tools.javac.Main.main(Main.java:54)”.

      Solution 1 : As a quick solution add the following statements to pom.xml

      <plugin>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>2.0.2</version>
      <configuration>
      <source>${compile.src.version}</source>
      <target>${compile.src.version}</target>
      <debug>true</debug>
      <optimize>true</optimize>
      <meminitial>128m</meminitial>
      <maxmem>1024m</maxmem>
      <fork>true</fork>
      <showDeprecation>true</showDeprecation>
      <showWarnings>true</showWarnings>
      </configuration>
      </plugin>

      Note : if the same problem exists from eclipse then try running from command prompt.

      Issues with inner data-table in nested form

      February 11, 2011 Leave a comment

      Issue : when you use datatable in a nested form (like inside a panel-series or another datatable) then data of inner datable will not get reflected or refreshed if the data gets modified.

      • Solution 1 : we used normal table as a quick solution for this issue.
      • Solution 2 : we can use a binding variable and clear the data (found on net but not tried).
      • Solution 3 : <ui:repeat> can also be used based on the situation.

       

      Follow

      Get every new post delivered to your Inbox.