Friday, May 21, 2010

Java XML Simple Read Example

To Read and extract the elements from the XML file first parse the XML file though the parser and create the Document
Document doc = builder.parse(str);

Once the document is availabe we can fetch the root and elements as follows
Node node = doc.getDocumentElement();
String root = node.getNodeName();

Example is given below

import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.*;

public class GetRootNode{
public static void main(String[] args) {
try{
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter xml file name: ");
String str = bf.readLine();
File file = new File(str);
if (file.exists()){
DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = fact.newDocumentBuilder();
Document doc = builder.parse(str);
Node node = doc.getDocumentElement();
String root = node.getNodeName();
System.out.println("Root Node: " + root);
}
else{
System.out.println("File not found!");
}
}
catch(Exception e){}
}
}

Tuesday, May 18, 2010

Hibernate Mapping - inverse="true"

Hibernate Basic Mapping:
Just refreshing our old fried Hibernate Mapping :)

Set -> We know Set holds unique objects. Example for using Set is "Member" can hold N number of "Claim"
Member is the Parent and Claim the child. Here we can use One-To-Many relation with Set as Collection.

inverse="true" -> persistence will be taken care by the other end ie., in this case child
cascade="all" -> Example to explain cascade option.  when you are deleting an parent it will automatically delete the child.





    
    

Parent p = (Parent) session.load(Parent.class, pid);
Child c = new Child();
p.addChild(c);
session.flush();

Monday, May 17, 2010

JSF aj4 ajaxsingle=true - Examine

When ajaxsingle=true is used in the element then, while ajax submission only that particular element will be submitted instead of whole form.

Example when we have two drop down Country and state. Consider state is mandatory field and state is populated by a4jsupport of country drop down. If we are not making ajaxsingle=true in country drop down then required validation error will be thrown while selecting the country even before user selecting the state.

JSF bypassupdates - Examine

When we use bypassupdates we can able to skip the model update phase alone while all other 5 phases among 6 will be executed by JSF framework.

JSF Phases:
1. Restore View Phase
2. Apply Request Values Phase
3. Covert and Validations Phase
4. Update Model Values Phase
5. Invoke Application Phase
6. Render Response Phase

When bypassupdates is defined, then JSF will skip execution of Phase 4

JSF immediate=true - Examine

When setting immediate=true in JSF command button we can able to skip validation and model update phase. This is helpful while implementing the cancel button functionality

JSF Phases:
1. Restore View Phase
2. Apply Request Values Phase
3. Covert and Validations Phase
4. Update Model Values Phase
5. Invoke Application Phase
6. Render Response Phase

When immediate=true is defined then JSF will skip execution of Phase 3 and 4 ie., convertion, validation and model update wont happen.

Example: