Signup · Login
Stardeveloper.com  
Home · Tutorials · Forums · Web Hosting Plans · Faisal Khan's Blog · Contact
Search Stardeveloper.com
Stardeveloper RSS Feed
Newsletter
Enter your email address below to be informed every time a new article is posted at Stardeveloper.com:

You can follow Faisal Khan on Twitter
Article Categories
.NET  .NET
  ASP (16)
  ASP.NET (41)
  ADO (16)
  ADO.NET (10)
  COM (6)
  Web Services (4)
  C# (1)
  VB.NET (3)
  IIS (2)

J2EE  J2EE
  JSP (15)
  Servlets (9)
  Web Services (1)
  EJB (4)
  JDBC (4)
  E-Commerce (1)
  J2ME (1)
  Products (1)
  Applets (1)
  Patterns (1)
Log In
UserName Or Email:

Password:

Auto-Login:

Miscellaneous Links
  Submit Article

Hosted by Securewebs.com
 
Home : J2EE : EJB : Reading and Parsing XML Files with Enterprise JavaBeans
 

Reading and Parsing XML Files with Enterprise JavaBeans

by Faisal Khan.Follow Faisal Khan on Twitter

Overview :
In this tutorial you will learn how to read XML files using Java. We will then apply this knowledge to create a Session bean ( EJB ) with ability to read given XML file and return a Java object containing values read from the XML file.

So you will learn two things in this tutorial :

  • Reading and Parsing XML files using Java
  • Developing an XMLReaderEJB Session bean

In an earlier article, "Building your first Enterprise JavaBean", we learned how to create our first EJB and deploy it on JBoss Server. We then called that EJB from a JSP page running on Tomcat Server. If this is your first time with EJBs then I'll suggest that you read that article first. In this tutorial we will use the same directory structure we created in that article, so if you feel that few things don't make sense to you then kindly give the above article a reading.

Building XMLReaderEJB Session bean :
We will call our XML reader EJB as 'XMLReaderEJB'. Before delving into the discussion of building XMLReaderEJB Session bean, make sure you have working JBoss and Tomcat Servers on your system. Although this EJB should work on any EJB container, for example's sake we are going to run it on JBoss. If this is your first time with JBoss and Tomcat then I'll suggest that you give "Building your first Enterprise JavaBean" a reading, this will save you lot of time and effort later.

So if you have followed that article you should have a directory structure like following :

C:\Projects
    TomcatJBoss
    EJB
        FirstEJB

Where 'FirstEJB' is the directory containing project files for FirstEJB Session bean we created earlier. Create a new sub-folder under 'EJB' and name it 'XMLReaderEJB'. Now create a sub-folder "src" in "XMLReaderEJB" folder you just created. Your directory structure should look like following :

C:\Projects
    TomcatJBoss
    EJB
        FirstEJB
        XMLReaderEJB
            src

Let's now create the required files for our XMLReaderEJB Session bean :

i. Contacts.xml :
This is XML file our EJB is going to read. Create a new 'xml' folder under the C:\Projects\TomcatJBoss folder we had created earlier. In 'xml' folder create a new 'Contacts.xml' file. Copy and paste following text in it :

<?xml version="1.0" ?>
<contacts>
	<contact>
		<name>Bill Joe</name>
		<email>bill@email.com</email>
		<phone-no>123-456-7890</phone-no>
		<address state="NV" country="USA">
			10 - Some Good Street,
			Some Good City.
		</address>
	</contact>
	<contact>
		<name>Cool Guy</name>
		<email>cool@email.com</email>
		<phone-no>123-456-7890</phone-no>
		<address state="Punjab" country="Pakistan">
			20 - Northway St, Goodwill County, Lahore.
		</address>
	</contact>
</contacts>

It simply contains contact information for two persons. Their names, emails and address information has been stored in this XML file. This is the information we are going to retrieve using XMLReaderEJB.

ii. XMLReader Remote Interface :
Create a new Java source file and name it "XMLReader.java". Place it according to it's package statement in the 'src' folder we created above. Complete path to XMLReader.java should be :

C:\Projects\EJB\XMLReaderEJB\src\com\stardeveloper\ejb\session\XMLReader.java

Now, copy and paste following code in it and hit the 'save' button :

package com.stardeveloper.ejb.session;

import com.stardeveloper.ejb.util.Contact;
import com.stardeveloper.ejb.exception.CouldNotReadException;

import javax.ejb.EJBObject;
import java.rmi.RemoteException;

public interface XMLReader extends EJBObject {

	public Contact[] readXML(String fileName)
		throws CouldNotReadException, RemoteException;
}

Explanation :
Our XMLReader Remote interface extends javax.ejb.EJBOjbect interface and contains just one method for it's clients to invoke, readXML(String fileName). This method asks for complete local path to an XML file and returns an array of 'Contact' objects. We will see later what this XML file contains and what 'Contact' objects are.

Our readXML() method throws CouldNotReadException and RemoteException. This method will throw CouldNotReadException when it is unable to find the given XML file or the XML file contains invalid XML.

iii. XMLReaderHome Home Interface :
Create a new Java source file and name it "XMLReaderHome.java". Place it according to it's package statement in the 'src' folder we created above. Complete path to XMLReaderHome.java should be :

C:\Projects\EJB\XMLReaderEJB\src\com\stardeveloper\ejb\session\XMLReaderHome.java

Now, copy and paste following code in it and hit the 'save' button :

package com.stardeveloper.ejb.session;

import javax.ejb.CreateException;
import javax.ejb.EJBHome;
import java.rmi.RemoteException;

public interface XMLReaderHome extends EJBHome {

    public XMLReader create() throws CreateException, RemoteException;
}

Explanation :
Our XMLReaderHome Home interface extends javax.ejb.EJBHome interface and contains a single create() method for getting a reference to XMLReader Remote interface. As we learned in the earlier article, Home interface contains methods for creating and removing EJB implementation objects.

iv. XMLReaderEJB Class File :
Create a new Java source file and name it "XMLReaderEJB.java". Place it according to it's package statement in the 'src' folder we created above. Complete path to XMLReaderEJB.java should be :

C:\Projects\EJB\XMLReaderEJB\src\com\stardeveloper\ejb\session\XMLReaderEJB.java

Now, copy and paste following code in it and hit the 'save' button :

Overview :
In this tutorial you will learn how to read XML files using Java. We will then apply this knowledge to create a Session bean ( EJB ) with ability to read given XML file and return a Java object containing values read from the XML file.

So you will learn two things in this tutorial :

  • Reading and Parsing XML files using Java
  • Developing an XMLReaderEJB Session bean

In an earlier article, "Building your first Enterprise JavaBean", we learned how to create our first EJB and deploy it on JBoss Server. We then called that EJB from a JSP page running on Tomcat Server. If this is your first time with EJBs then I'll suggest that you read that article first. In this tutorial we will use the same directory structure we created in that article, so if you feel that few things don't make sense to you then kindly give the above article a reading.

Building XMLReaderEJB Session bean :
We will call our XML reader EJB as 'XMLReaderEJB'. Before delving into the discussion of building XMLReaderEJB Session bean, make sure you have working JBoss and Tomcat Servers on your system. Although this EJB should work on any EJB container, for example's sake we are going to run it on JBoss. If this is your first time with JBoss and Tomcat then I'll suggest that you give "Building your first Enterprise JavaBean" a reading, this will save you lot of time and effort later.

So if you have followed that article you should have a directory structure like following :

C:\Projects
    TomcatJBoss
    EJB
        FirstEJB

Where 'FirstEJB' is the directory containing project files for FirstEJB Session bean we created earlier. Create a new sub-folder under 'EJB' and name it 'XMLReaderEJB'. Now create a sub-folder "src" in "XMLReaderEJB" folder you just created. Your directory structure should look like following :

C:\Projects
    TomcatJBoss
    EJB
        FirstEJB
        XMLReaderEJB
            src

Let's now create the required files for our XMLReaderEJB Session bean :

i. Contacts.xml :
This is XML file our EJB is going to read. Create a new 'xml' folder under the C:\Projects\TomcatJBoss folder we had created earlier. In 'xml' folder create a new 'Contacts.xml' file. Copy and paste following text in it :

<?xml version="1.0" ?>
<contacts>
	<contact>
		<name>Bill Joe</name>
		<email>bill@email.com</email>
		<phone-no>123-456-7890</phone-no>
		<address state="NV" country="USA">
			10 - Some Good Street,
			Some Good City.
		</address>
	</contact>
	<contact>
		<name>Cool Guy</name>
		<email>cool@email.com</email>
		<phone-no>123-456-7890</phone-no>
		<address state="Punjab" country="Pakistan">
			20 - Northway St, Goodwill County, Lahore.
		</address>
	</contact>
</contacts>

It simply contains contact information for two persons. Their names, emails and address information has been stored in this XML file. This is the information we are going to retrieve using XMLReaderEJB.

ii. XMLReader Remote Interface :
Create a new Java source file and name it "XMLReader.java". Place it according to it's package statement in the 'src' folder we created above. Complete path to XMLReader.java should be :

C:\Projects\EJB\XMLReaderEJB\src\com\stardeveloper\ejb\session\XMLReader.java

Now, copy and paste following code in it and hit the 'save' button :

package com.stardeveloper.ejb.session;

import com.stardeveloper.ejb.util.Contact;
import com.stardeveloper.ejb.exception.CouldNotReadException;

import javax.ejb.EJBObject;
import java.rmi.RemoteException;

public interface XMLReader extends EJBObject {

	public Contact[] readXML(String fileName)
		throws CouldNotReadException, RemoteException;
}

Explanation :
Our XMLReader Remote interface extends javax.ejb.EJBOjbect interface and contains just one method for it's clients to invoke, readXML(String fileName). This method asks for complete local path to an XML file and returns an array of 'Contact' objects. We will see later what this XML file contains and what 'Contact' objects are.

Our readXML() method throws CouldNotReadException and RemoteException. This method will throw CouldNotReadException when it is unable to find the given XML file or the XML file contains invalid XML.

iii. XMLReaderHome Home Interface :
Create a new Java source file and name it "XMLReaderHome.java". Place it according to it's package statement in the 'src' folder we created above. Complete path to XMLReaderHome.java should be :

C:\Projects\EJB\XMLReaderEJB\src\com\stardeveloper\ejb\session\XMLReaderHome.java

Now, copy and paste following code in it and hit the 'save' button :

package com.stardeveloper.ejb.session;

import javax.ejb.CreateException;
import javax.ejb.EJBHome;
import java.rmi.RemoteException;

public interface XMLReaderHome extends EJBHome {

    public XMLReader create() throws CreateException, RemoteException;
}

Explanation :
Our XMLReaderHome Home interface extends javax.ejb.EJBHome interface and contains a single create() method for getting a reference to XMLReader Remote interface. As we learned in the earlier article, Home interface contains methods for creating and removing EJB implementation objects.

iv. XMLReaderEJB Class File :
Create a new Java source file and name it "XMLReaderEJB.java". Place it according to it's package statement in the 'src' folder we created above. Complete path to XMLReaderEJB.java should be :

C:\Projects\EJB\XMLReaderEJB\src\com\stardeveloper\ejb\session\XMLReaderEJB.java

Now, copy and paste following code in it and hit the 'save' button :


 ( 3 Remaining ) Next

See all comments and questions (post-ad) posted for this tutorial.


Comments/Questions ( Threads: 4, Comments: 5 )
    Contains 1 or more replies by the Author of this Article.
    Contains 1 or more replies by Faisal Khan.

  1. EJB Transactions.....
  2. There is a problem while compiling
  3. Reading XML Files with Enterprise JavaBeans from Xindice with XPath
  4. problems... ( 1 Reply )

Post Comments/Questions

In order to post questions/comments, you must be logged-in. If you are not a member yet, then signup, otherwise login. Once you login then come back to this page and you'll see a form right here which will allow you to post comments/questions.

Please note, one of the benefits of signing up is to be notified immediately by email everytime you receive a reply to the thread you have subscribed to.

 
© 1999 - 2009 Stardeveloper.com, All Rights Reserved.