The benefits of converting data into XML format _xml/rss

Source: Internet
Author: User
Tags xslt
We often encounter situations where you need to handle data saved or transmitted in various formats (from files delimited by commas or tabs to more loaded formats), and you need a corresponding parser (parser) for each format. This shortcoming slows development progress and can lead to errors. One solution is to convert data from a common format into an XML document, and then save, process, or convert it to another format.

An instance

There are now a variety of data formats that enable data to be stored, exported, imported, and transmitted within or between software. The most common is the bounding format (delimited format), such as commas or tab-delimited data formats, and fixed-length data formats. Let's say we have an Address Book program that provides the ability to export data into both comma-delimited and fixed-length formats.

In a comma-delimited format, separate the fields in the same data record with commas, as shown in listing a . In a fixed-length data format, each field in a record should have a standard length. Listing B gives an address book in fixed-length format.

Creating an XML document

Now, let's parse the input data and convert it into an XML document. An XML document (that is, org.w3c.dom.Document) is the original data type for all Document Object Models (DOM), and it provides access to document data.

You can use the Execute builddocument (InputStream is) method to create a document that corresponds to your data, such asCode Listing Cas shown. The method reads the input data stream line by row and analyzes it by line according to the given frame.

If you want to parse the delimited format data, you need to create an instance of the class whose constructor is Plaintexttoxmlformatter (string[] colname,string Delim), and its delimiters can be any string. In the case of a fixed-length format, you should use the second constructor Plaintexttoxmlformatter (string[] colname,int[] collen), and one of its arguments is the character-long degree group. In the Address Book example we gave, the fields were 10, 10, 30, and 10 characters in length. Parameter colname is an array that holds the names of the target data records, in the example we give, the names are named (firstName), Last Name (LastName), email, and telephone number (phone).

The actual parsing process for converting data rows to data symbols is the execution of Getstringarray (string read,string delim) or Parsefixedlengthmessage (string read,int[] collen) Process of the method. The return value is a string array, which is derived from the two methods that decompose the given input. If the data is not well-formed, an exception is thrown and the parsing fault is terminated. Call the Setskiperror (true) method to ignore exceptions and complete the data parsing process. Calling this method prevents an exception from being thrown, but it still allows the program to print an error message to the error output stream.

When rows are parsed into tokens, they are added to an XML document as an element of an XML document. Each row of records that is placed into an element has a default name Line, you can also call Setdatalinename () to specify the name. Each data record is a column element, and the name of the column element is provided by the corresponding class constructor, and the child element is added to the row element.

After the input data is fully read, you get a valid XML document that you can further process. Now, because the data is organized in a well-known tree structure, it is very straightforward to handle it. For example, you can send this document to a third party, so long as a third party knows the document type definition (the document type definition, DTD), then he can easily process the document. You can also call the Writedocument (document Doc,outputstream Osout) method to save the document.Code Listing DGives an example of saving an XML document to a file.

Using XSLT transformations to view data

You can also convert XML data into other formats and represent its contents in different views. The easiest way to do this is to use the XSLT transformation, which provides a powerful, tree-oriented transformation language implementation that converts XML instances that use one glossary to simple text, HTML, or XML that uses other vocabularies.

For a given XML input, you can use the XSLT language to create the output you expect. For example, by executing transformdata (inputstream xmlin,inputstream xslin,outputstream transfout), you can convert XML data into an HTML document. Code Listing E provides an example of an XSLT transformation, and listing F gives an HTML view of the Address book entry.

Simplify the management of data

In this article, we learned how to convert common format data into XML documents by analyzing the Plaintexttoxmlformatter class. We also saw how to represent XML documents in different views with the help of XSLT transformations. When you need to process data in a variety of formats, adopting the above techniques may be a good solution, saving you valuable time and reducing the likelihood of error.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.