• No results found

When would you not use an XML?

Java Messaging using JMS & MOM

Application 1 similar setup as

Q. When would you not use an XML?

XML is verbose and it can be 4-6 times larger in size compared to a csv or a tab delimited file. If your network lacked bandwidth and/or your content is too large and network throughput is vital to the application then you may consider using a csv or tab delimited format instead of an XML.

Q 93: What is the difference between a SAX parser and a DOM parser? SF PI MI FAQ

A 93:

SAX parser DOM parser

A SAX (Simple API for XML) parser does not create any internal structure. Instead, it takes the occurrences of components of an input document as events (i.e., event driven), and tells the client what it reads as it reads through the input document.

A DOM (Document Object Model) parser creates a tree structure in memory from an input document and then waits for requests from client.

A SAX parser serves the client application always only with pieces of the document at any given time.

A DOM parser always serves the client application with the entire document no matter how much is actually needed by the client.

A SAX parser, however, is much more space efficient in case of a big input document (because it creates no internal structure). What's more, it runs faster and is easier to learn than DOM parser because its API is really simple. But from the functionality point of view, it provides a fewer functions, which means that the users themselves have to take care of more, such as creating their own data structures.

A DOM parser is rich in functionality. It creates a DOM tree in memory and allows you to access any part of the document repeatedly and allows you to modify the DOM tree. But it is space inefficient when the document is huge, and it takes a little bit longer to learn how to work with it.

Use SAX parser when

ƒ Input document is too big for available memory.

ƒ When only a part of the document is to be read and we create the data structures of our own.

ƒ If you use SAX, you are using much less memory and performing much less dynamic memory allocation.

Use DOM when

ƒ Your application has to access various parts of the document and using your own structure is just as complicated as the DOM tree.

ƒ Your application has to change the tree very frequently and data has to be stored for a significant amount of time.

SAX Parser example: Xerces, Crimson etc

Use JAXP (Java API for XML Parsing) which enables applications to parse and transform XML documents independent of the particular XML parser. Code can be developed with one SAX parser in mind and later on can be changed to another SAX parser without changing the application code.

DOM Parser example: XercesDOM, SunDOM, OracleDOM etc.

Use JAXP (Java API for XML Parsing) which enables applications to parse and transform XML documents independent of the particular XML parser. Code can be developed with one DOM parser in mind and later on can be changed to another DOM parser without changing the application code.

Q 94: Which is better to store data as elements or as attributes? DC

A 94: A question arising in the mind of XML/DTD designers is whether to model and encode certain information using an element, or alternatively, using an attribute. The answer to the above question is not clear-cut. But the general guideline is:

ƒ Using an element: <book><title>Lord of the Rings</title>...</book>: If you consider the information in question to be part of the essential material that is being expressed or communicated in the XML, put it in an element

ƒ Using an attribute: <book title=" Lord of the Rings "/>: If you consider the information to be peripheral or incidental to the main communication, or purely intended to help applications process the main communication, use attributes.

The principle is data goes in elements and metadata goes in attributes. Elements are also useful when they contain special characters like “<”, “>”, etc which are harder to use in attributes. The most important reason to use element is its extensibility. It is far easier to create child elements to reflect complex content than to break an attribute into pieces. You can use attributes along with elements to refine your understanding of that element with extra information. Attributes are less verbose but using attributes instead of child elements with the view of optimizing document size is a short term strategy, which can have long term consequences.

Q 95: What is XPATH? What is XSLT/XSL/XSL-FO/XSD/DTD etc? What is JAXB? What is JAXP? SF FAQ

A 95:

What

is Explanation Example

XML XML stands for eXtensible Markup Language Sample.xml

<?xml version="1.0"?>

<note>

<to>Peter</to>

<from>Paul</from>

<title>Invite</title>

<content language=”English”>Not Much</content>

< content language=”Spanish”>No Mucho</content >

</note>

DTD DTD stands for Document Type Definition. XML provides an application independent way of sharing data. With a DTD, independent groups of people can agree to use a common DTD for interchanging data. Your application can use a standard DTD to verify that data that you receive from the outside world is valid. You can also use a DTD to verify your own data. So the DTD is the building blocks or schema definition of the XML document.

Sample.dtd

<!ELEMENT note (to, from, title, content)>

<!ELEMENT to (#PCDATA)>

<!ELEMENT from (#PCDATA)>

<!ELEMENT title (#PCDATA)>

<!ELEMENT content (#PCDATA)>

<!ATTLIST content language CDATA #Required>

XSD XSD stands for Xml Schema Definition, which is a successor of DTD. So XSD is a building block of an XML document.

If you have DTD then why use XSD you may ask?

XSD is more powerful and extensible than DTD. XSD has:

Support for simple and complex data types.

Uses XML syntax. So XSD are extensible just like

Sample.xsd

XML because they are written in XML.

• Better data communication with the help of data types. For example a date like 03-04-2005 will be interpreted in some countries as 3rd of April 2005 and in some other countries as 04th March 2005.

<xs:sequence>

<xs:element name="to" type="xs:string"/>

<xs:element name="from" type="xs:string"/>

<xs:element name="title" type="xs:string"/>

<xs:element name="content" type="xs:string"/>

</xs:sequence>

</xs:complexType>

<xs:attribute name="language" type=”xs:string”

use=”Required” />

</xs:element>

</xs:schema>

XSL XSL stands for eXtensible Stylesheet Language. The XSL consists of 3 parts:

XSLT: Language for transforming XML documents from one to another.

XPath: Language for defining the parts of an XML document.

XSL-FO: Language for formatting XML documents.

For example to convert an XML document to a PDF document etc.

XSL can be thought of as a set of languages that can :

Define parts of an XML.

Transform an XML document to XHTML (eXtensible Hyper Text Markup Language) document.

Convert an XML document to a PDF document.

Filter and sort XML data.

XSLT processor example: Xalan (from Apache).

PDF Processor example: FOP (Formatting Objects Processor from Apache)

To convert the Sample.xml file to a XHTML file let us apply the following Sample.xsl through XALAN parser.

Sample.xsl

<?xml version="1.0"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">

<xsl:template match="/">

<xsl:apply-templates select="note " />

</xsl:template>

You get the following output XHTML file:

Sample.xhtml

<html>

<head>

<title>English</title>

</head>

</html>

Now to convert the Sample.xml into a PDF file apply the following FO (Formatting Objects) file Through the FOP processor.

<xsl:value-of select="content[@language='English']">

</fo:block>

</fo:flow>

</fo:page-sequence>

</fo:root>

which gives a basic Sample.pdf which has the following line Not Much

XPath Xml Path Language, a language for addressing parts of an As per Sample.xsl

XML document, designed to be used by both XSLT and XPointer. We can write both the patterns (context-free) and expressions using the XPATH Syntax. XPATH is also used in XQuery.

<xsl:template match=”content[@language=’English’]”>

………

<td><xsl:value-of select=”content/@language” /></td>

JAXP Stands for Java API for XML Processing. This provides a common interface for creating and using SAX, DOM, and XSLT APIs in Java regardless of which vendor’s implementation is actually being used (just like the JDBC, JNDI interfaces). JAXP has the following packages:

JAXP

• javax.xml.parsers Æ common interface for different vendors of SAX, DOM parsers).

• org.xml.sax Æ Defines basic SAX API.

• org.w3c.dom Æ Defines Document Object Model and its componenets.

• javax.xml.transform Æ Defines the XSLT API which allows you to transform XML into other forms like PDF, XHTML etc.

Required JAR files are jaxp.jar, dom.jar, xalan.jar, xercesImpl.jar.

DOM example using JAXP:

DocumentBuilderFactory dbf =

DocumentBuilderFactory.newInstance();

DocumentBuilder db = dbf.newDocumentBuilder();

Document doc =

db.parse(new File("xml/Test.xml"));

NodeList nl = doc.getElementsByTagName("to");

SAXExample se = new SAXExample();

sp.parse(new File("xml/Sample.xml"),se);

where SAXExample.Java code snippet

public class SAXExample extends DefaultHandler { public void startElement(

String uri,

The DefaultHandler implements ContentHandler, DTDHandler, EntityResolver, ErrorHandler XSLT example using JAXP:

StreamSource xml =

new StreamSource(new File("/xml/Sample.xml"));

StreamSource xsl = new StreamSource(

new File("xml/Sample.xsl"));

StreamResult result =

new StreamResult(new File("xml/Sample.xhtml"));

TransformerFactory tf =

<title>English</title>

</head>

</html>

JAXB Stands for Java API for XML Binding. This standard defines a mechanism for writing out Java objects as XML (Marshaling) and for creating Java objects from XML structures (unMarshaling). (You compile a class description to create the Java classes, and use those classes in your application.)

Let’s look at some code:

For binding:

xjc.sh –p com.binding sample.xsd –d work

JAXB

XML schema Sample.xsd

Java files

(*.java interfaces

&

implementations)

Java class files

*.class

Application

JAXB API XML

Sample.

xml

Java content Objects

note

to from use

javacxjc binding compiler

marshall unmarshall

-p identifies the package for the generated Java files (i.e.

*.Java)

-d option identifies the target.

UnMarshaling the XML document:

JAXBContext jc = JAXBContext.newInstance(

“com.binding”);

Unmarshaller um = jc.createUnmarshaller();

Object o = um.unMarshall(

new File(“/xml/”));

Note n = (Note) n;

System.out.println(n.getFrom().getContent().get(0));

System.out.println(n.getTo().getContent().get(0));

Now to modify the in memory object content:

n. getFrom().getContent().set(0, “newValue”);

Marshaling the change back to different XML file:

Marshaller m = jc.createMarshaller();

FileOutputStream fos = new FileOutputStream(

new File(“/xml/SampleNew.xml”));

m.marshall(n, fos);

Refer Q14 in How would you go about section for XML based standards/protocols like SOAP, WSDL, and UDDI relating to Web services, which enable interoperability between disparate systems (e.g. Between .Net and J2EE etc). These standards provide a common and interoperable approach for defining (i.e. WSDL), publishing (i.e. UDDI) and using (i.e. SOAP) Web services. The J2EE 1.4 platform provides comprehensive support for Web services through the JAX-RPC (Java API for XML based RPC (Remote Procedure Call)) and JAXR (Java API for XML Registries).

Outline

Related documents