Tutorial Blog – Online Free Tutorial Free online Tutorial blog

30Jan/100

Serialize and deserialize Java objects to and from XML

A unique open source Java library called XStream is available for serializing and deserializing Java objects to and from XML. A small team of developers contribute to the development of the XStream Java library.

To use this library you have to use the following import statement,

import com.thoughtworks.xstream.XStream;

Once you have this import statement you can create the XStream object using the statement like,

XStream xs = new XStream();

Let us suppose you have a class called 'person' with member variables like 'name' and 'location', which is defined as below,

class person {
string name;
string location;
}

The above class can be serialized using the XStream object as given below,

String xml = xs.toXML(person);

Now you can print the string xml using the print statement. Assuming that you have set the values of the member variables within the program, you would get an XML output with the string values set as the text values of the elements 'name' and 'location'. The output would also include the <xml version="1.0"?> line as the first line in the XML document.

Thus by using XStream you can serialize or deserialize Java objects to XML and from XML. Lot of examples using the XStream library is available in the internet.

Your Own MP3 Duration Calculator (Dr. Dobb's Journal)
Calculator - Math - IPhone - Mathematics - Windows

A Quick Introduction to the Spring Python Framework (Developer.com)
The original Spring, written for Java, is a framework which at its heart uses an "Inversion of Control" container (IoC).

Source: http://www.xml-training-guide.com/e-xml18.html

  • Share/Bookmark
Filed under: XML No Comments
23Jan/100

The Role of XML in SQL Server 2000

One of the most common problems that one is faced with managing data is the exchange of data between multiple data sources. This situation is more serious in business-to-business or business to consumer environment.

Solutions such as Electronic Data Exchange have not become popular due their heavy costs and limited flexibility. This was solved using the eXtensible Markup Language, that was developed under W3C.XML is often compared with HTML, where the only difference is that in XML, our own tags can be created.

SQL 2000 has a built in feature for XML.This includes the following features.

• RETREIVES DATA IN XML FORMAT USING THE FOR XML CLAUSE IN SELECT STATEMENT:

XML results can be retrieved using SQL queries. These Queries can be executed directly or using a stored procedure. If the results have to be retrieved directly then the FOR XML clause of the SELECT statement can be used. Within the statement the mode can be specified as either RAW, AUTO OR EXPLICIT. The data can be retrieved as an XML document by modifying the SELECT statement. The general format is,

FOR XML mode [, XMLDATA] [, ELEMENTS] [, BINARY BASE 64]

An example for the above Syntax is:

Select cus_Id, cus_lname, cus_fname from customer
FOR XML RAW

Mode can be RAW, AUTO, OR EXPLICIT.

* RAW MODE:

RAW mode transforms the query results into an XML element with the generic identifier row. Each column value that is not null is mapped to an attribute in which the attribute takes the same name as the column name.

* EXPLICIT MODE:

In the EXPLICIT mode the Query writer controls the shape of the XML document returned by the execution of the Query. Query must be written in a specific way so that the additional information about the nesting of the statements is explicitly stated.

* AUTO MODE:

In the AUTO mode, the Query results are displayed in the nested form.

The format of the XML document returned by the query is completely returned in the EXPLICIT mode.

* XML mode specifies whether the mode is RAW, AUTO or EXPLICIT.
* XMLDATA specifies that an XML schema has to be returned.
* If the ELEMENTS option is specified then the columns are returned as sub elements.
* If BINARY BASE 64 is specified then the binary data returned by the Query is represented as base64-encoded format.

II) INSERTION OF THE XML DATA INTO THE DATABASE USING THE OpenXML ROWSET PROVIDER

* Run the sp_xml_preparedocument stored procedure that creates memory resident representation of the XML document and then returns an integer value.
* Execute OpenXML function that uses the XML document created in the first step. Use the OpenXML function into the INSERT statement. This inserts the converted data into any SQL database.
* Run the sp_xml_removedocument stored procedure to free the memory allocated to the XML document by sp_xml_preparedocument.

OpenXML has the following format:

OpenXML (idoc, rowpattern [, flags])
WITH (SchemaDeclaration | TableName)]

Idoc:

It is the document handle of the internal representation of the XML document.

Rowpattern:

It is the Xpath that is used to identify the nodes to be processed as rows.

Flags:

These specify that the mappings should be made between XML data and the relational rowsets. It is an optional value.

Schema declaration:

It is the schema definition of the form:

ColName ColType [ColPattern | MetaProperty][, ColName ColType [ColPattern | MetaProperty]...]
* Colname is the name in the rowset
* Coltype is the data type of the column in the rowset.
* Colpattern: This is optional and is generally used to define how XML nodes have to be mapped to the columns.
* Tablename is the name of the table.

An example of the above syntax is given below:

SELECT *
FROM OPENXML (@idoc, ‘/ROOT/Student’, 1) WITH (Rollno varchar (5), Marks varchar (10))
The above example displays the name of the first student, along with the Roll number and marks from the XML document.

Source: http://www.xml-training-guide.com/xml-sql-server.html

  • Share/Bookmark
Filed under: XML No Comments
23Jan/100

Converting XML to PDF Using FOP

XML reports can be converted to PDF format by using the Formatting Objects Processor (FOP). FOP is an Open source Java API that can convert your XML data to other formats like TXT, SVG, AWT, MIF and PS.This is free software that was developed under Apache XML Project.

One of the major advantages of using an FOP is its ability to convert XML data into reports in the PDF format, using a formatting tree. FOP can be down loaded from http://www.apache.org/dyn/closer.cgi/xml/fop

FOP is a tool that understands formatting objects as specified by the W3C in the XSL specification. First part of this specification deals with the XSLT transformations. The second part deals with the Formatting objects (FO). This is a output independent specification that, which can compose a vocabulary for style and layout of a document. An FO is a simple XML document. Its namespace is defined in www.w3.org/1999/xsl/format

You can manually create this document and specify exact values for every element in the output. However, the general approach is to write an XSLT style sheet to take your XML data file, transform it according to your style sheet and produce a final FO document. Although the main idea of FOP is to work on the FO document, it can also transform the existing data (XML) using a style sheet.

XSL: FO is an XML based document layout and formatting language that is used for creating Postscript, and the other common formats from XMl document. To design the XSL: FO follow these steps:

1. Open an existing schema, DTD or a Database, the XML content model appears on the left hand pane.
2. Select the item or attribute that you want in your output.
3. Drag it from the left panel to the main output window and specify the new node to be handled.
4. The XSL: FO style sheet thus resulted is generated automatically and can be previewed by clicking on the XSL: FO tab.

  • Share/Bookmark
Filed under: XML No Comments
23Jan/101

Introduction to VTD – XML

VTD-XML is a new, open-source, non-validating, non-extractive eXtensible Markup Lanugauge (XML) processing Application Programming Interface (API) written in Java. The VTD-XML is the best alternative to Simple API for XML (SAX) and Document Object Model (DOM), as it does not force you to trade processing performance for usability.

The Java-based, non-validating VTD - XML parser is faster than DOM and better than SAX. Unlike the other XML processing technologies, VTD-XML is designed to be random-access capable without incurring excessive resource overhead.

An important optimization feature of VTD-XML is non-extractive tokenization. Internally, VTD-XML retains the XML message in memory intact and un-decoded, and tokens represent tokens using starting offset and length exclusively. Tokenization of VTD-XML is based on the Virtual Token Descriptor (VTD) core binary encoding specification. A VTD record is a 64-bit integer that encodes the token length, starting offset, type and nesting depth of a token in XML.

Memory buffers can be allocated in bulk to store the VTD records, as the records are constant in length. This avoids the creation of a multitude of string/node objects usually associated with other XML processing technologies. As a result, both memory usage and object creation cost are greatly reduced by using VTD-XML, which leads to significantly higher processing performance. For example, on a 1.5 Ghz Athlon machine, VTD-XML delivers random access at a performance level of 25 to 35 MB/sec, outperforming most SAX parsers with null content handlers. An in-memory VTD-XML document typically consumes only 1.3 to 1.5 times the size of the XML document.

VTD-XML provides several benefits for software developers. For example, you require a processing model to start work on a project involving XML. The DOM is slow and consumes too much memory, particularly for large documents. The SAX difficult to use especially for XML documents with complex structures. As a result, the best option is to choose the VTD-XML, as the features of VTD-XML does not force you to trade processing performance for usability. The random-access capability of VTD-XML provides the best possible performance. Even though SAX is fast due to ifs forward only nature, it does not suit for all the conditions.

In some situations, you perform lots of buffering to extract the data needed, while in others, you may have to repeat SAX parsing on the same document multiple times. Irrespective of what you do, SAX programming usually results in ugly and unmaintainable code, while the performance benefit over DOM is not always significant. The VTD-XML enables you to simultaneously achieve ease-of-use and high-performance. Also the performance benefit of the VTD-XML over DOM is substantial.

VTD-XML can be used for an XML project, only if the two criteria are met. The first criteria is that the current version of VTD-XML does not support entity declarations in document type definitions (DTDs). The VTD-XML recognizes only the five built-in entities such as &s;, &apos;, <, >, and ". The VTD-XML works well when Simple Object Access Protocol (SOAP), Resource Description Framework (RDF), Financial Information Exchange Markup Language (FIXML), or Really Simple Syndication (RSS) are used in the XML project. The next criterion is that the VTD-XML's internal parsed representation of XML is slightly larger than the XML, which as a result demands sufficient RAM. To provide true, random access to the entire document, the document needs to be placed in memory. When both the criteria are met, the VTD-XML is the most efficient XML processing API.

The Java API of VTD-XML consists of three essential components which include VTDGen (VTD generator) that encapsulates the parsing routine that produces the internal parsed representation of XML, the VTDNav (VTD navigator) which is a cursor-based API that allows for DOM-like random access to the hierarchical structure of XML, and the Autopilot which is the class that allows for document-order element traversal.

The following steps need to be performed to use VTD-XML for processing an XML document either from disk or via HTTP. The first step is to find out the length of the XML document, allocate adequate memory big enough to hold the document, and then read the entire document into memory. The next step is to create an instance of VTDGen and assign the byte array to it using setDoc(). The final step is to call parse(boolean ns), to generate the parsed XML representation. When ns is set to true, subsequent document navigation is namespace aware. If parsing succeeds, you can retrieve an instance of VTDNav by calling getNav().

At the onset of navigation, the cursor of the VTDNav instance points at the root element of the XML document. You can use one of the overloaded versions of toElement() function, to move the cursor manually to different positions in the hierarchy. The toElement() function when declared as toElement(int direction) takes an integer as the input, to indicate the direction in which the cursor moves. Defined as class variables of VTDNav, the six possible values of this integer are ROOT, PARENT, FIRST_CHILD, LAST_CHILD, NEXT_SIBLING, and PREV_SIBLING. Each has its respective acronym such as R, P, FC, LC, NS, and PS. The method toElement() returns a Boolean value indicating the status of the operation. The toElement() returns true when the cursor is moved successfully. When the cursor is moved to a non-existent location, for example, the first child of a childless element, then the cursor does not move and the toElement() returns false.

The method getAttrVal(String attrName) retrieves the attribute value of the element at the cursor position.
Similarly, the getText() method retrieves the text content of the cursor element. In addition, you can use the toElementNS() and getAttrValNS() methods to navigate the document hierarchy in a namespace-aware fashion, if the namespace is turned on during parsing. Autopilot is the other mode of navigation. An instance of Autopilot can automatically move the cursor through the node hierarchy in document order. To use Autopilot, first you need to call the constructor, which accepts an instance variable of VTDNav as the input. Then, you need to call the selectElement() or selectElementNS() method, to specify the descendent elements to be filtered out. Whenever this is done, each call to the iterate() method moves the cursor to the next matching element.

Now let us see some of the unique properties of VTD-XML compared to other similar XML APIs, such as DOM and XMLCursor. The hierarchy of VTD-XML consists exclusively of element nodes. This is very different from DOM, which treats every node, whether it is an attribute node or a text node, as a part of the hierarchy. In VTD-XML, every instance of VTDNav has only one cursor. The cursor can be moved back and forth in the hierarchy, but you cannot duplicate it. However, you can temporarily save the location of the cursor on a global stack. VTDNav has two stack access methods which include Calling push() which saves the cursor state and Calling pop() which restores the cursor state. For example, consider that you are somewhere in the element hierarchy and you want to move to a different area of the document after saving the current location and then continue at the saved point. To accomplish this task, you need to first push() the location onto the stack. After moving the cursor to a different part of the document, you can very quickly jump back to the saved location by popping it off the stack.

One of the most unique aspect of VTD-XML that distinguishes it from any other XML processing API, is its non-extractive tokenization based on Virtual Token Descriptor. Non-extractive parsing enables you to achieve optimal processing and memory efficiency in VTD-XML. VTD-XML manifests this non-extractiveness in the following ways. Most of the member methods of VTDNav, such as getAttrVal(), getCurrentIndex(), and getText() return an integer. This integer is a VTD record index that describes the token as requested by the calling functions. VTD-XML produces a linear buffer filled with VTD records, after parsing. You can access any VTD record in the buffer if you know its index value, as all the VTD records are have the same length. In addition, the VTD records cannot be addressed using pointers, as the records are not objects. When a VTDNav function does not evaluate to any meaningful value, it returns -1 which is more or less equivalent to a NULL pointer in DOM.

VTD-XML implements its own set of comparison functions that directly operate on VTD records, as the parsing process does not create any string objects. For example, the matchElement() method of VTDNav, tests whether the element name, which effectively is the VTD record of the cursor, matches a given string. Similarly, the matchTokenString(), matchRawTokenString(), and matchNormalizedTokenString() methods of VTDNav perform a direct comparison between a string and a VTD record. This is advantageous as you need not pull tokens out into string objects, which are expensive to create, especially when you create lots of them. Bypassing excessive object creation is the main reason why VTD-XML significantly outperforms DOM and SAX. VTD-XML can also implement its own set of string-to-numeric data conversion functions that operate directly on VTD records. VTDNav has four member methods which include parseInt(), parseLong(), parseFloat() and parseDouble(). These functions take a VTD record index value and convert it directly into a numeric data type.

  • Share/Bookmark
Filed under: XML 1 Comment
23Jan/100

Introduction to ConciseXML

ConciseXML is an open, language-independent markup syntax compatible with XML 1.0. Concise XML is designed to handle different types of data which includes non-hierarchial data, program logic, document markup, and binary data. As ConciseXML is both concise and precise, it eliminates the two major limitations of XML and extending the use of XML. Most of the developers avoid XML in many circumstances due to its verbosity.

ConciseXML is as concise at representing logic as the semi-colon delimited syntax of C, C++, Java, and C#. In addition, ConciseXML is as concise as the Comma Separated Value (CSV) syntax for data. ConciseXML has no ambiguity of meaning. There is only one way to represent parts/fields of an object. Whereas in XML, there can be many syntactic forms for the identical meaning or semantics.

ConciseXML is compatible with XML, but does not conform to XML or SGML. ConciseXML is a superset of XML. This means that the only extensions are the ones that remove the XML constraints and does not change the fundamental features of XML. ConciseXML is capable enough of maintaining both forward-compatibility as well as backward-compatibility with XML. Any XML document is also a valid ConciseXML document. You can represent any ConciseXML document in XML without any loss of information. There is a corresponding form in XML for each ConciseXML extension to XML. A ConciseXML expression or document can mix and match ConciseXML and XML syntax at all levels. ConciseXML is mistaken to be not compatible with XML, as ConciseXML uses a different syntax and not the XML syntax. The syntax of ConciseXML eliminates many constraints from SGML's document-centric perspective. However, ConciseXML is compatible with XML, as any XML document is also a valid ConciseXML expression.

ConciseXML makes XML more flexible by eliminating many of the unnecessary constraints of XML. Some of the extensions of ConciseXML include attribute values can be any expression, attribute keys can be any object, tagname of an element can be any expression, attribute keys are optional, closing tagname is optional, and top-level can be any expression, not just an element. Additional extensions of ConciseXML include multiple top-level expressions, attribute type, and paths. Now let us see each of the extensions of ConciseXML briefly in the following sections.

  • Share/Bookmark
Filed under: XML No Comments