Login
Register

Home

Trainings

Fusion Blog

EBS Blog

Authors

CONTACT US

Prasad Bhogle
  • Register

Oracle Gold Partners, our very popular training packages, training schedule is listed here
Designed by Five Star Rated Oracle Press Authors & Oracle ACE's.

webinar new

Search Courses

Executive Summary:

 

XML – Extensible Markup Language is becoming common standard for data exchange/interchange between organizations and replacing ASCII files. With XML becoming famous, there is a need to develop XML parsers which can read specific type of XML data and write into destination systems. In Oracle EBS, implementations many times source data/legacy data can be given in XML format and I have seen developers writing parsers using PLSQL using UTL_FILE utility to read XML files. Writing such PLSQL parsers is tedious job and is more prone to errors.

 

I feel JAVA language should be used for writing XML parsers as it’s flexible, simple, robust and platform independent. This write paper gives some tips to design/develop XML parsers using Java.

 

 

What is XML?

  • XML stands for Extensible Markup Language
  • XML is a markup language much like HTML
  • XML was designed to carry data, not to display data
  • XML tags are not predefined. You must define your own tags
  • XML is designed to be self-descriptive

 

XML is classified as an extensible language because it allows its users to define their own elements. The primary purpose is to facilitate the sharing of structured data across different information systems. XML is nothing special, its just plain text. Software that can handle plain text can also handle XML.

 

Example of an XML message:

<message>

<to>This email address is being protected from spambots. You need JavaScript enabled to view it.</to>

<from>This email address is being protected from spambots. You need JavaScript enabled to view it.</from>

<subject>XML Is Really Cool</subject>

<text> How many ways is XML cool? Let me count the ways... </text>

</message>

 

 

XML contains tags and attribute. Tags are the data element, which can also contain attributes which are nothing but additional information about the tag itself.

 

<message to="This email address is being protected from spambots. You need JavaScript enabled to view it." from="This email address is being protected from spambots. You need JavaScript enabled to view it." subject="XML Is Really Cool">
<text> How many ways is XML cool? Let me count the ways... </text>

</message>

 

 

XML and Related Specifications

 

There are two levels of correctness of XML document:

  • Well Formed A well-formed document conforms to all of XML’s syntax rules otherwise a conforming parser is not allowed to process it
  • Valid A valid document additionally conforms to some semantic rules. These rules are either user-defined or included as XML Schema or DTD.

Most commonly used standards/specification documents are XSD (XML Schema Definition) and DTD (Document Type Definition).

 

DTD example

<!ELEMENT ADDRESS_BOOK (ADDRESS)+ >

<!ELEMENT ADDRESS (NAME,STREET+, CITY,STATE,ZIP)>

<!ELEMENT NAME (#PCDATA) >

<!ELEMENT STREET (#PCDATA) >

<!ELEMENT CITY (#PCDATA) >

<!ELEMENT STATE (#PCDATA) >

<!ELEMENT ZIP (#PCDATA) >

 

 

 

PCDATA is Parsed Character Data. We can add attribute list as follows:

<!ATTLIST STREET TYPE (street|suitno|aptno|other) #IMPLIED>

 

XSD Example

<?xml version="1.0" encoding="UTF-8"?>

 

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">

<xs:element name="Employee">

<xs:annotation>

<xs:documentation>Comment describing your root element</xs:documentation>

</xs:annotation>

<xs:complexType>

<xs:sequence>

<xs:element name="Name"/>

<xs:element name="Age"/>

</xs:sequence>

</xs:complexType>

</xs:element>

</xs:schema>

 

 

Java for XML Processing

After small overview of XML now lets discuss about XML processing APIs. This white paper is about usage of Java language so we will discuss about Java APIs. There are two commonly used ways which are used for XML processing

  • DOM (Document Object Model) defines a standard way accessing and manipulating XML documents. The DOM presents an XML document as tree structure, with elements, attributes and text as nodes.
  • SAX (Simple API for XML) is based on events. With SAX, the parser tells the application what is in the document by notifying the application of stream of parsing events. Application then processes those events to act on data.

There are various vendors like apache, oracle, ibm which provide XML parsers based on DOM and SAX processing. All the examples in this white paper are based on apache provided parsers. Apache XML parser (xerces.jar) is available at http://xml.apache.org/xerces-j/index.html

 

To start using apache parser, download xerces.jar and add it to classpath of your machine. This jar file provides various java APIs which can be using for processing XML files.

 

 

 

Before Writing a Parser

 

Before even writing/selecting type of XML parser, we have to take a look at the XML structure. Based on XML structure, java classes need to be defined which will hold the data parsed by XML parser.

 

Employees.xml

<?xml version="1.0" encoding="UTF-8"?>

<Personnel>

<Employee type="permanent">

<Name>Seagull</Name>

<Id>3674</Id>

<Age>34</Age>

<Telephones>

<TlfnNumber type="Home">5162376160</TlfnNumber >

<TlfnNumber type="Work">5162376128</TlfnNumber >

</Telephones>

</Employee>

<Employee type="contract">

<Name>Robin</Name>

<Id>3675</Id>

<Age>25</Age>

<Telephones>

<TlfnNumber type="Home">7322376160</TlfnNumber >

<TlfnNumber type="Work">7322376128</TlfnNumber >

</Telephones>

</Employee>

<Employee type="permanent">

<Name>Crow</Name>

<Id>3676</Id>

<Age>28</Age>

<Telephones>

<TlfnNumber type="Home">7182376160</TlfnNumber >

<TlfnNumber type="Work">7182376128</TlfnNumber >

</Telephones>

</Employee>

</Personnel>

 

 

Let’s convert this xml into a class structure. In this case Personnel Structure holds multiple employee structures and employee structure hold multiple telephone structure. Our lowest class would be Phone, followed by Employee and finally Personnel.

 

Class TelePhones.java

public class TelePhones {

public String type;

public String tlfnNumber;

}

 

Class Employee.java

public class Employee {

String name;

String id;

String age;

ArrayList telephoneList;

}

 

Class Personnel.java

public class Personnel {

ArrayList employeeList;

}

 

After looking at the structure you can easily understand after parsing Employee.xml we will get one object of Personnel class which will contain complete data present in xml file. Thus now xml will be converted in java object and can be manipulated easily different operations like writing to database, validating data, creating another xml files etc.

 

Writing a DOM parser

 

While writing DOM parser first a Document object is obtained and java provides rich API to manipulate. Hence first call is usually

Element root = doc.getDocumentElement();

This gets the root element of the Document as an instance of the Element class. Element subclasses Node and has methods getType(), getName(), and getValue(), and getChildNodes.

There are many types of Nodes i.e. subclasses of Node: Attr, CDATASection, Comment, Document, DocumentFragment, DocumentType, Element, Entity, EntityReference, Notation, ProcessingInstruction, Text.

Each of these has a special and non-obvious associated type, value, and name.

 

One should keep following chart nearby when using DOM

 

Node

Node Name

Node Value

Attributes

Note Type

Attr

Attr name

Value of attribute

null

2

CDATASection

#cdata-section

CDATA content

null

4

Comment

#comment

Comment content

null

8

Document

#document

Null

null

9

DocumentFragment

#document-fragment

null

null

11

DocumentType

Doc type name

null

null

10

Element

Tag name

null

NamedNodeMap

1

Entity

Entity name

null

null

6

EntityReference

Name entitry referenced

null

null

5

Notation

Notation name

null

null

1

ProcessingInstruction

target

Entire string

null

7

Text

#text

Actual text

null

3

Following steps should be following write writing DOM parser

  • Get a document builder using document builder factory and parse the xml file to create a DOM object
  • Get a list of employee elements from the DOM
  • For each employee element get the id,name,age and type. Create an employee value object and add it to the list.
  • At the end iterate through the list and print the employees to verify we parsed it right.

Getting a document builder

private void parseXmlFile(){

//get the factory

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

try {

//Using factory get an instance of document builder

DocumentBuilder db = dbf.newDocumentBuilder();

//parse using builder to get DOM representation of the XML file

dom = db.parse("employees.xml");

}catch(ParserConfigurationException pce) {

pce.printStackTrace();

}catch(SAXException se) {

se.printStackTrace();

}catch(IOException ioe) {

ioe.printStackTrace();

}

}

 

 

Get a list of employee elements

private void parseDocument(){

//get the root element

Personnel personnel = new Personnel();

Personnel. employeeList = new ArrayList();

Element docEle = dom.getDocumentElement();

 

 

//get a nodelist of elements

NodeList nl = docEle.getElementsByTagName("Employee");

if(nl != null && nl.getLength() > 0) {

for(int i = 0 ; i < nl.getLength();i++) {

 

//get the employee element

Element el = (Element)nl.item(i);

 

//get the Employee object

Employee e = getEmployee(el);

 

//add it to list

Personnel. employeeList.add(e);

}

}

}

 

Reading in data from each employee.

 

private Employee getEmployee(Element empEl) {

//for each <employee> element get text or int values of

//name ,id, age and name

String name = getTextValue(empEl,"Name");

String id = getTextValue(empEl,"Id");

String age = getTextValue(empEl,"Age");

 

String type = empEl.getAttribute("type");

 

//Create a new Employee with the value read from the xml nodes

Employee e = new Employee(name,id,age,type);

 

return e;

}

 

/**

* I take a xml element and the tag name, look for the tag and get

* the text content

* i.e for <employee><name>John</name></employee> xml snippet if

* the Element points to employee node and tagName is 'name' I will return John

*/

private String getTextValue(Element ele, String tagName) {

String textVal = null;

NodeList nl = ele.getElementsByTagName(tagName);

if(nl != null && nl.getLength() > 0) {

Element el = (Element)nl.item(0);

textVal = el.getFirstChild().getNodeValue();

}

return textVal;

}

}

 

Writing a SAX parser

SAX parser is a Event Driven Parser. You provide the callback methods, and the parser invokes them as it reads the XML data. The following Packages need to be imported in your Java File to use the SAX parser.

 

 

Create a new instance of ParserFactory and from this one we create a new Instance of SaxParser. To the Parse Method of SaxParser, we pass two parameters, name of the XML File and the class which implements interface HandlerBase

 

The Following Methods needs to be implemented from the DocumentHandlerBase Interface, and Description above each methods explains when this Method is called. These Methods are called automatically while parsing the XML file and when the corresponding data element is reached.

 

startDocument ()

 

Called when the Parser starts parsing the Current XML File.

 

endDocument ()

 

Called when the Parser Completes parsing the Current XML File.

 

startElement (String name, AttributeList attrs)

 

Called when the starting of the Element is reached. For Example if we have Tag called <Title> ... </Title>, then this method is called when <Title> tag is Encountered while parsing the Current XML File. The AttributeList Parameter has the list of all Attributes declared for the Current Element in the XML File.

endElement (String name)

 

Called when the Ending of the current Element is reached. For example in the above explanation, this method is called when </Title> tag is reached.

 

characters (char buf [], int offset, int len)

 

While Parsing the XML file, if extra characters like space or enter Character are encountered then this method is called. If you don't want to do anything special with these characters, then you can normally leave this method blank.

 

 

Import Section

import java.io.IOException;

import java.io.OutputStreamWriter;

import java.io.PrintWriter;

import java.io.UnsupportedEncodingException;

import org.xml.sax.AttributeList;

import org.xml.sax.HandlerBase;

import org.xml.sax.Parser;

import org.xml.sax.SAXException;

import org.xml.sax.SAXParseException;

import org.xml.sax.XMLReader;

import org.xml.sax.helpers.ParserFactory;

import org.xml.sax.XMLReader;

import org.apache.xerces.parsers.SAXParser;

public class SAXhandler extends HandlerBase {…

}

 

Create a Sax Parser and parse the xml

public static void main(String argv[]) {

 

String parserName = "org.apache.xerces.parsers.SAXParser";

String arg = "Employees.XML";

try {

SAXhandler counter = new SAXhandler();

 

Parser parser = ParserFactory.makeParser(parserName);

parser.setDocumentHandler(counter);

parser.setErrorHandler(counter);

parser.parse(arg);

}catch (Exception ex) {

}

}

 

In the event handlers create the Employee object and call the corresponding setter methods.

//Event Handlers

public void startElement(String uri, String localName, String qName,

Attributes attributes) throws SAXException {

//reset

tempVal = "";

if(qName.equalsIgnoreCase("Personnel")) {

Personnel personnel = new Personnel();

personnel.employeeList = new ArrayList();

}

if(qName.equalsIgnoreCase("Employee")) {

//create a new instance of employee

tempEmp = new Employee();

tempEmp.setType(attributes.getValue("type"));

}

}

 

public void characters(char[] ch, int start, int length) throws SAXException {

tempVal = new String(ch,start,length);

}

 

public void endElement(String uri, String localName,

String qName) throws SAXException {

 

if(qName.equalsIgnoreCase("Employee")) {

//add it to the list

personnel.employeeList.add(tempEmp);

 

}else if (qName.equalsIgnoreCase("Name")) {

tempEmp.setName(tempVal);

}else if (qName.equalsIgnoreCase("Id")) {

tempEmp.setId(Integer.parseInt(tempVal));

}else if (qName.equalsIgnoreCase("Age")) {

tempEmp.setAge(Integer.parseInt(tempVal));

}

}

 

Validating XML

 

Following code need to be added to validate a given XML with schema definition (XSD) or DTD which is mentioned inside XMLs

 

String JAXP_SCHEMA_LANGUAGE =

"http://java.sun.com/xml/jaxp/properties/schemaLanguage";

String W3C_XML_SCHEMA =

"http://www.w3.org/2001/XMLSchema";

 

Next, you need to configure DocumentBuilderFactory to generate a

namespace-aware, validating parser that uses XML Schema:

 

… DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance()

factory.setNamespaceAware(true);

factory.setValidating(true);

try {

factory.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA); }

catch (IllegalArgumentException x) {

// Happens if the parser does not support JAXP 1.2 ...

}

 

 

 

Conclusion:

 

After this discussion of writing XML parsers, let’s talk about their usage in relation to E-Business Suite. When XML is given as data files, then there is a need of writing Java XML parsers. The JAR files of these custom XML parsers can be deployed on Custom TOPs of operating systems hosting Oracle Applications. These java programs can be called from shell scripts which in turned can be registered as concurrent programs.

 

One should avoid using PLSQL for parsing XMLs because PLSQL has limitations in file handling capabilities and doesn’t have capabilities to handle XML tags/elements. One has to do lot of hard coding while using PLSQL for XML processing.

 

This calls for increased usage Java XML Parsers in E-Business Suite customizations and interface developments.

 


Prasad Bhogle

Comments   

0 #1 Justin Michael Raj 2008-05-27 06:33
Hi Prasad,
This article is impressive. An ice breaker kind of stuff. Thanks for sharing it with us.

Can you please explain the features of Oracle XML Toolkit.
And how can we process and handle XML documents using them.

Thanks and Regards
Justin
Quote
0 #2 Brad Moreland 2008-06-02 18:31
Very helpful for the typical pl/sql ebs developer that does not have much experience with java yet. Please keep in mind that the java class can be registered directly as a concurrent program and need not be called from a shell script.
Quote
0 #3 Rehan Yusuf 2008-06-07 15:46
Awesome article Prasad ! .. Cheers ..
I have a request to make though, could you conjure a similar one on XPath ? I'm trying to understand how I can use it in my XMLP Templates (RTF)

regards,
Rehan Yusuf
Quote

Add comment


Security code
Refresh

Search Trainings

Fully verifiable testimonials

Apps2Fusion - Event List

<<  Apr 2024  >>
 Mon  Tue  Wed  Thu  Fri  Sat  Sun 
  1  2  3  4  5  6  7
  8  91011121314
15161718192021
22232425262728
2930     

Enquire For Training

Fusion Training Packages

Get Email Updates


Powered by Google FeedBurner