Professional Documents
Culture Documents
<myC:address xmlns:myC=“http://myCompany.com/hr/employees”>
<myC:name>John Smith</myC:name>
<myC:street>43 Walcut St</myC:street>
<myC:city>Dublin</myC:city>
<myC:state>Ohio</myC:state>
<myC:postal-code>45561</myC:postal-code>
</myC:address>
Namespaces
Namespaces
another way to declare and use a namespace without a prefix.
It is called a default namespace. Example
<address xmlns= "http//myCompany.com/hr/employees" >
All subordinate elements are assumed to belong to the default
namespace, provided they are not prefixed
<orderperson>John Smith</orderperson>
<shipto>
<name>Ola Nordmann</name>
<address>Langgt 23</address>
<city>4000 Stavanger</city>
<country>Norway</country>
</shipto>
<item>
<title>Empire Burlesque</title>
<note>Special Edition</note>
<quantity>1</quantity>
<price>10.90</price>
</item>
<item>
<title>Hide your heart</title>
<quantity>1</quantity>
<price>9.90</price>
</item>
</shiporder>
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="shiporder">
<xs:complexType>
<xs:sequence>
<xs:element name="orderperson" type="xs:string"/>
<xs:element name="shipto">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="item" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="note" type="xs:string" minOccurs="0"/>
<xs:element name="quantity" type="xs:positiveInteger"/>
<xs:element name="price" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="orderid" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>
XML Processing and Parsing Models
the service provider and service consumer application
exchange data through XML instance documents.
Five choices for XML processing models:
SAX The Simple API for XML is an event-driven parsing
/programming model.
StAX The Streaming API for XML provides a pull, event-
based parsing /programming model.
DOM The Document Object Model provides an in-memory
tree transversal programming model.
XML data-binding Provides an in-memory Java content
class-bound programming model.
XSLT Extensible Stylesheet Language Transformation
provides a template-based programming model.
XML Processing and Parsing Models
SAX, DOM more common. StAX catching up.
JAXP APIs (Java APIs for XML Processing) – SAX, DOM, StAX
JAXB technology - XML data binding
The processing includes
Parsing the XML document for processing
Making changes
Creating a new XML document
Deserialization or Unmarshalling Parsing an XML document into an
equivalent data structure
Serialization or Marshalling writing a data structure to an XML
Document
DOM supports both Deserialization and Serialization
SAX supports only Deserialization
SAX Processing Model
Need to implement event handlers to manage
events generated by the parser
A SAX parser generates a transient, flow of events
To process the source document
intercept the relevant type of events generated by
the parser. You can use the information passed as
parameters of the events to help identify the relevant
information
the application logic can be applied to the information
obtained.
Some times need to maintain the context to logically
aggregate information from the flow of events.
SAX Processing Model
There are two ways of applying business logic
when using SAX parsing:
First Approach:
The business logic is invoked as soon as the information is
extracted or after minimal aggregation. This approach is
referred to as stream processing
The document can be processed in one step.
Second Approach:
invokes the business logic after complete parsing and
consolidating the extracted information.
Needs two steps to complete the processing of an XML
document.
SAX Processing Model
SAX first approach (stream processing) advantage:
it lets an application immediately start processing the content of the
source document.
application does not even have to wait for the entire document to be
retrieved.
Useful when - retrieving document from an earlier processing stage
(pipeline) or from the network
disadvantages :
a document may appear to be well and has unexpected errors at the
end
notices these problems only when it comes across erroneous tokens
or when it cannot resolve an entity reference.
the input stream from which it is reading the document unexpectedly
closes
Use stream processing model within the context of a transaction
SAX Processing Model
With the second approach,
parsing the document and applying business logic are
performed in two steps
first ensures that the document and the information
extracted from the document are valid
Once the document data is validated, the application
invokes the business logic
Disadvantages of the SAX model
no facility to modify or produce XML documents.
still possible to generate an XML document by writing
custom serialization handler
SAX Processing Model
SAX processing model well suited when
the XML document is very large and needs more memory
Your applications do not make any structural
modifications and no need to generate XML documents.
the document needs only be processed once
you want to implement stream process very large
documents
extract and process only part of the XML document
the XML document structure and the order of
information map well to the domain-specific objects
familiar with event-based programming
no need to generate/write XML documents
SAX processing follows a push model
SAX Processing Model
StAX Processing Model
StAX (Streaming API for XML) is a new API that is becoming
most common processing model for JAVA applications
Like SAX, it is based on streaming
Streaming
a programming model in which XML infosets (sections of XML
document) are transmitted and parsed serially at application
runtime, often in real time, and often from dynamic sources
stream-based parsers can start generating output
immediately
infoset elements can be discarded and garbage collected
immediately after they are used.
stream processing provides a smaller memory footprint,
reduced processor requirements
StAX Processing Model
Limited to cardboard tube
view of is document
need to know what
processing you want to do
before reading the XML
StAX follows pull streaming
model (a client application
calls methods on an XML
parsing library)
StAX Processing Model
Pull model has several advantages over push model:
the client controls the application thread (in pull) vs
the parser controls the application thread
Pull parsing libraries can be much smaller, and the
client code is simpler than with push libraries
Pull clients can read multiple documents at one time
with a single thread.
A StAX pull parser can filter XML documents such
that elements unnecessary to the client can be
ignored
StAX provides marshalling (creating XML docs)
StAX Processing Model
Use the StAX processing model when :
You are dealing with very large documents and
memory usage is an issue
You are familiar with event-based programming
The document must be processed only once
There is need for marshalling and unmarshalling
XML documents
You want high performance
DOM Processing Model
processing the XML input data involves two
steps:
The DOM parser generates a tree-like data
structure that models the XML source
document. This structure is called a DOM
tree.
The application searches for the relevant
information in the tree and extracts,
consolidates, and processes it further. Then
the application can create domain-specific
objects from the consolidated data.
The cycle for searching, extracting, and
processing can be repeated as many times as
is needed because the DOM tree persists in
memory.
DOM Processing Model
Limitations:
The DOM model designed to be a platform-and language-
independent interface. (does not use the java.util.Collection API )
Advantages:
it is slightly easier to use than the SAX model.
allow both marshalling and unmarshalling of the XML document
XSLT identity transformation can be used to achieve serialization
back to the XML document
use of XPath with the DOM processing model simplifies many
tasks
XPath allows to specify path patterns to locate element content,
attribute values, and subtrees, may eliminate tree-transversal code.
XPath expressions are strings, they can be easily parameterized and
externalized in a configuration file
DOM Processing Model
DOM processing model should be used when
You want to process or generate XML documents
You want to process the XML document more than once
The document is not very large and memory usage is not an
issue
You want to manipulate an XML document and need fine-
grained control over the document structure that you want to
create or edit
You want random access to parts of the document. For example,
you want to go back and forth within the document.
You want to benefit from the flexibility of XPath and apply XPath
expressions on the DOM tree.
You want to implement data binding but you cannot use JAXB
technology because the document either has no schema or it
conforms to a DTD
XML Data-Binding Processing Model
allows the processing a XML document without being concerned with XML
representation
binding compiler is used to generate Java content classes from a source schema