Monday, July 4, 2011

Java : read from xml


Check this API : javax.xml.parsers  below :
http://download.oracle.com/javase/6/docs/api/




==============XMLReader.java==================

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class XMLReader {

 public static void main(String argv[]) {

  try {
  File file = new File("MyXMLFile.xml");
  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  DocumentBuilder db = dbf.newDocumentBuilder();
  Document doc = db.parse(file);
  doc.getDocumentElement().normalize();
  System.out.println("Root element " + doc.getDocumentElement().getNodeName());
  NodeList nodeLst = doc.getElementsByTagName("employee");
  System.out.println("Information of all employees");

  for (int s = 0; s < nodeLst.getLength(); s++) {

    Node fstNode = nodeLst.item(s);
   
    if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
 
           Element fstElmnt = (Element) fstNode;
      NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("firstname");
      Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
      NodeList fstNm = fstNmElmnt.getChildNodes();
      System.out.println("First Name : "  + ((Node) fstNm.item(0)).getNodeValue());
      NodeList lstNmElmntLst = fstElmnt.getElementsByTagName("lastname");
      Element lstNmElmnt = (Element) lstNmElmntLst.item(0);
      NodeList lstNm = lstNmElmnt.getChildNodes();
      System.out.println("Last Name : " + ((Node) lstNm.item(0)).getNodeValue());
    }

  }
  } catch (Exception e) {
    e.printStackTrace();
  }
 }
}

=====================END=====================

============MyXMLFile.xml=================
<?xml version="1.0"?>
<company>
    <employee>
        <firstname>Sriram</firstname>
        <lastname>Sreedhar</lastname>
    </employee>
    <employee>
        <firstname>Harini</firstname>
        <lastname>Sriram</lastname>
    </employee>
    <employee>
        <firstname>firstname</firstname>
        <lastname>lastname</lastname>
    </employee>
</company>








========================================

Oracle datasource in Glassfish

Glassfish does not ship with oracle jdbc drivers. The oracle driver needs to be added manually.  The driver can be found either in a JDeveloper or oracle client or database installation in {install_path}/jdbc/lib, or from oracle's site:

http://www.oracle.com/technology/software/tech/java/sqlj_jdbc/htdocs/jdbc_10201.html

Glassfish needs at least ojdbc14.jar to work properly. Older drivers do not work. Put the ojdbc14.jar in {glassfish}/lib directory and restart glassfish:

asadmin stop-domain domain1
asadmin start-domain domain1

Now you need to navigate to the administrator application:
http://localhost:4848/
 
Open the 'Resources' -> 'JDBC' -> 'Connection Pools' and select 'New.
Fill in the Connection Pool 'Name'.
Select 'Resource Type' : 'javax.sql.ConnectionPoolDataSource'.
Select 'Database Vendor' : 'Oracle'.
Select 'Next'.

At the next page go down at the additional Properties.
Select all the properties and select 'Delete Properties'.
Select 'Add Property', fill 'Name' : 'user' and 'Value' : {db_user}
Select 'Add Property', fill 'Name' : 'password' and 'Value' : {db_user_pass}
Select 'Add Property', fill 'Name' : 'url' and 'Value' :
jdbc:oracle:thin:@[host][:port]:SID
or
jdbc:oracle:thin:@//[host][:port]/SID
Select 'Finish'.

Navigate to Connection Pools and select the pool you just created.
Click Ping to test that it is working. If it is not, it is probably because you have mistyped the connection credentials. Go to 'Additional Properties' tab of the connection pool detail page and correct any errors. Try ping again.

Open the 'Resources' -> 'JDBC' -> 'JDBC Resources' and select 'New.
Fill the JNDI Name like 'jdbc/myconnnection'.
Select from 'Pool Name' combo box the pool we already created.
And select 'OK'.

The Connection Pool is ready!

Have taken this article from
http://mariosgaee.blogspot.com/2009/12/oracle-connection-pool-in-glassfish.html

Configuring Web Application Server Glassfish :

Download Glassfish from : https://glassfish.dev.java.net/

untar the glassfish in install path eg : /install_home

run the glassfish-x.x-unix.sh (mine was I think 3.1)

To Start a domain:

$ ./asadmin start-domain domain1
Waiting for domain1 to start .......
Successfully started the domain : domain1
domain  Location: /ubuntu/glassfish_install/glassfish/domains/domain1
Log File: /ubuntu/glassfish_install/glassfish/domains/domain1/logs/server.log
Admin Port: 4848
Command start-domain executed successfully.

To Stop Domain :

$ ./asadmin stop-domain domain1
Waiting for the domain to stop ...
Command stop-domain executed successfully.


To Deploy a Application :

$ ./asadmin deploy /ubuntu/ebooks/hello.war
Application deployed with name hello.
Command deploy executed successfully.


I Configured Http instance port to (9090) , default is 8080:

I use below to check application deployed.
http://127.0.0.1:9090/hello


Admin server console (make sure you have atleast one domain running to access admin server):


http://localhost:4848





This is how the installation screen details will look like :

CREATING DOMAIN
_______________
Executing command :/ubuntu/glassfish_install/glassfish/bin/asadmin --user admin --passwordfile /tmp/asadminTmp2258577312851916948.tmp create-domain --savelogin --checkports=false --adminport 4848 --instanceport 9090 --domainproperties=jms.port=7676:domain.jmxPort=8686:orb.listener.port=3700:http.ssl.port=8181:orb.ssl.port=3820:orb.mutualauth.port=3920 domain1
/ubuntu/glassfish_install/glassfish/bin/asadmin --user admin --passwordfile /tmp/asadminTmp6210518625018142589.tmp create-domain --savelogin --checkports=false --adminport 4848 --instanceport 9090 --domainproperties=jms.port=7676:domain.jmxPort=8686:orb.listener.port=3700:http.ssl.port=8181:orb.ssl.port=3820:orb.mutualauth.port=3920
=============================
domain1Using port 4848 for Admin.
Using port 9090 for HTTP Instance.
==================================
Using port 7676 for JMS.
Using port 3700 for IIOP.
Using port 8181 for HTTP_SSL.
Using port 3820 for IIOP_SSL.
Using port 3920 for IIOP_MUTUALAUTH.
Using port 8686 for JMX_ADMIN.
Using default port 6666 for OSGI_SHELL.
Using default port 9009 for JAVA_DEBUGGER.
Distinguished Name of the self-signed X.509 Server Certificate is:
[CN=ubuntu,OU=GlassFish,O=Oracle Corporation,L=Santa Clara,ST=California,C=US]
Distinguished Name of the self-signed X.509 Server Certificate is:
[CN=ubuntu-instance,OU=GlassFish,O=Oracle Corporation,L=Santa Clara,ST=California,C=US]
No domain initializers found, bypassing customization step
Domain domain1 created.
Domain domain1 admin port is 4848.
Domain domain1 admin user is "admin".
Login information relevant to admin user name [admin]
for this domain [domain1] stored at
[/home/sriram/.asadminpass] successfully.
Make sure that this file remains protected.
Information stored in this file will be used by
asadmin commands to manage this domain.
Command create-domain executed successfully.