Reading and Parsing XML Files with Enterprise JavaBeansby Faisal Khan.
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 :
|