Wednesday, September 24, 2014

HTML Parsing and Querying with JSoup

Now a days in web environment we have too much work with HTML pages.

If you have any requirement like parsing, querying or adding missing tags in html document than from my point of view JSoup is the answer for it.

Here, I am going to show some little work around HTML document with the help of JSoup utility.

Please download JSoup jar file from JSoup download page and add this into your projects lib directory.

Below is the code snippet for above discussion,

HTML File :

<!DOCTYPE html>
<body bgcolor="#FFFFFF" text="#000000">
    <div align="center"  id="mainDiv"><font size="6">Welcome to JSoup Test Demo</font> 
    </div>
    <form name="testForm" method="post" action="controller_context_path">
        <table width="90%" border="0" cellspacing="4" cellpadding="4">
            <tr>
                <td width="50%">
                    <div align="right">
                        User Id:
                    </div>
                </td>
                <td width="50%">
                    <div align="left">
                        <input type="text" name="userId" value="">
                    </div>
                </td>
            </tr>
            <tr>
                <td width="50%">
                    <div align="right"  id="div1">
                        Name:
                    </div>
                </td>
                <td width="50%">
                    <div align="left" id="div2">
                        <input type="text" name="orgId" value="">
                    </div>
                </td>
            </tr>
            <tr>
                <td width="50%">
                    <div align="right"></div>
                </td>
                <td width="50%">
                    <div align="left"  id="div3">
                        <input type="submit" name="Submit" value="Login">
                    </div>
                </td>
            </tr>
        </table>
        <p>&nbsp;</p>
    </form>
</body>
</html>
-----------------------------------------------------------
Java Code:
Below is the code snippet to parse above document and querying to specific tag in HTML page,

File htmlFile =  new File("HTML file path");
org.jsoup.nodes.Document document = Jsoup.parse(xslfile, "UTF-8");

This will create html document with all the tags. This will format whole html file if it broken or some tags which is not ended with their respective tag,
Now if we want body section of the html page than
Element body = document.body();
If we want to convert body element into string format than simply use body.toString() method of Element.

For traversing or querying across html document below is the code snippet.

Elements element = document.select("div.div1"); //tagName.id of the tag

This will returns all the div tags which have id div1 in html document.

Now querying exact document from the html document below is the code snippet,

Element element = document.select("div.div3").select("input[name$=Submit]").first(); 

This will returns the element of input type whose attribute name contains Submit text.

For in-depth querying pattern visit http://jsoup.org/cookbook/extracting-data/selector-syntax

This is things which i have mentioned here. You can visit http://jsoup.org for more information.

Here is the comparison of different HTML parser which will be useful in selecting the html parser which are currently available. Visit http://en.wikipedia.org/wiki/Comparison_of_HTML_parsers

This might be helpful to you guys to initiate work related html document.

Please don't forgot to add your comment or feedback if you like this post or useful in any ways.

Cheers,
Ashish Mishra

Saturday, August 2, 2014

Mostly asked HR interview questions with points

Here i have listed few key HR round interview questions with bullet point which are mostly asked. These questions are based on my personal experience. I think i should share this information with you and might be helpful to you when you are going to face HR round. Here i have listed only bullet points and not described in details.

Que 1: What is your strength?
Points to remember
Adaptability : Adjustable in any kind of environment situation to work
Hard Working : Very hard working towards work
Honest :
Flexibility : Can work any where and any place in any environment
Optimistic : Positive Attitude
Fast Decision Making : Fast decision making(Good decision) in any kind of situation and pressure
Persistence : Regular in work, work with dedication
Self Motivated : I am self motivated person

Example: I am honest, self motivated and hard working guy with positive attitude towards my carrer and life

Que 2: What is weakness?
Points to remember:
Straightforward : Strait forward attitude
Impatient : Impatient in work.
Sensitive
More Talkative
Trust people very quickly
I can't say no when someone ask for help
Take decisions very quickly
Get nervous when talk to stranger
To speak lie is difficult for me
I am bit lazy about which i am not interested

Example: I can't say no when someone ask for help and little bit lazy about which i am not interested
Note: Include only two-three points in weakness when facing real interview


Que 3: Why should i hire you?
Points to remember:
Share your knowledge
Work Experience
Skills Related to job
Career Goal

Example: With reference to my work experience, i satisfy all the requirement for this job. I am sincere with my work and would never let you down anyway. I promise you will never regret for the decision to appoint me in your organization.

Que 4: Tell me what you know about this company.
Points to remember:
Pre-study about the company in details
Do the background work about ongoing/upcoming projects
Know the names of owners and partners
Research about the current issues
Update your knowledge about their competitors

Example: It's one of the best growing company in India. The work environment of the company is very good. People feel proud to be part of the company as company provides full supports to their employees in professional front it has many branches across the world so i have good opportunity to show my talent.

Que 5: Why are looking for job change?
Points to remember
Thanks to previous organization
What you learn in your previous organization
Share your reason for job change
Relates to career goals

Example: I am thankful to my previous organization because i have learnt lot of things there. According to me changes are necessary for everyone to enhance skills, knowledge and for personal growth and financial growth. Your organization is the good platform where i can learn more.

Que 6: What are your salary requirements?
Points to remember
Should share their expected salary
Always say as per the company norms for the job

Example: I have 4 years of experience in IT. My current CTC is XXX Lac/yr salary has never been a big issue for me. Still i am expecting salary as company's norms as per my designation and my qualification and experience which can help me to maintain my standard of level of my personal and economical needs.

Que 7: What are your career goals?
Points to remember
Short term goal
Long term goal

Example: My short term goal is to get a job in reputed company where i can utilize my skills and improve my career path. My long term goal is to be in respectable position in that organization.


Que 8: Do you any questions to ask me?
Points to remember
Express thanks
Salary Structures
Job timings
Job Location
Overtime allowance
Training Period
Probation Period
Transport Facility

Example: Thank you for giving this opportunity. Sir I would like to know about the job timings and transport facility and what will be the job location and salary structure for this job?

Hope this will help you to crack HR round of interview. If you have any suggestion or feedback which help us than please share your opinion and feedback so i can add in the same.

Cheers,
Ashish Mishra

Wednesday, January 29, 2014

DOM4J: Remove duplicate xml elements from xml

Today i will explain about handling duplicate xml elements of xml.

I think you are little bit aware with DOM4J API from Apache Foundation.

DOM4J API basically is for playing with XML. Sometime your facing the issue that how to get distinct element list from the given xml. To achive this we iterate whole document and get required list. But same thing can be achive by given DOM4J API.

You need to have a latest copy of dom4j.jar. You can easily download it from Sourceforge

Here i will show you the practical example of the same.

//XML Document
<?xml version="1.0" encoding="UTF-8"?>
<document>
<element>
<id>1</id>
</element>
<element>
<id>1</id>
</element>
<element>
<id>2</id>
</element>
<element>
<id>3</id>
</element>
<element>
<id>2</id>
</element>
<element>
<id>6</id>
</element>
<element>
<id>5</id>
</element>
<element>
<id>3</id>
</element>
<element>
<id>4</id>
</element>
<element>
<id>4</id>
</element>
</document>

Now create one class file and add below code snippet in your class
String xmlDoc = ""; //Add code which reads the xml input file from your file location

//Creating org.dom4j.Document objet from xml content
Document document = DocumentHelper.parseText(xmlDoc);

//Now creating distinct list from the created xml ducument with below code snippet

List<Node> nodes = document.selectNodes("//document/element", "id", true);

/*
* XPath for distinct element : "//document/element"
* Element name on which nodes will be compare: "id"
* Boolean flag which indicates to remove duplicate nodes or not.
**/