Describes in detail the benefits of converting data into XML format.

Source: Internet
Author: User
Tags xslt
We often encounter situations where we need to process the data stored or transmitted in various formats (from files with commas or tabs as separators to more load formats, you need a parser for each format ). This shortcoming slows down the development progress and may cause errors. A solution is to convert data in common formats into xml documents and save, process, or convert it to other formats. We often encounter situations where we need to process the data stored or transmitted in various formats (from files with commas or tabs as separators to more load formats, you need a parser for each format ). This shortcoming slows down the development progress and may cause errors. A solution is to convert data in common formats into xml documents and save, process, or convert it to other formats.

One instance

There are already many data formats for saving, exporting, importing, and transmitting data within or between software. The most common format is delimited format, such as comma-separated data formats and fixed-length data formats. Suppose we have an address book program that provides the ability to export data into two formats: comma-separated and fixed-length.

In the comma-separated format, use A comma to separate different fields in the same data record, as shown in listing. In a fixed-length data format, each field of the record should have a standard length. Listing B provides an address book with a fixed length format.

Create an XML document

Now, Let's parse the input data and convert it into an XML document. XML Document (org. w3c. dom. Document) is the original data type of all Document object models (DOM), and it provides access to Document data.

You can use the buildDocument (InputStream is) method to create a document corresponding to your data, as shown in Code List C. This method reads the input data stream row by row and performs row-by-row analysis based on the given frame.

If you want to parse the demarcation format data, you need to create an instance of the class whose constructor is PlainTextToXmlFormatter (String [] colName, String delim). its demarcation can be any String. In fixed length format, you should use the second constructor PlainTextToXmlFormatter (String [] colName, int [] colLen). one of its parameters is the field length array. In the address book example, each field contains 10, 10, 30, and 10 characters. The colName parameter is an array that stores the name of the target data record. In this example, the names are firstName and lastName), email, and phone number ).

The actual parsing process of converting data rows to data symbols is the process of executing the getStringArray (String read, String delim) or parseFixedLengthMessage (String read, int [] colLen) method. The returned value is a String array, which is obtained by decomposing the given input by the preceding two methods. If the data format is incorrect, an exception is thrown and the parsing error is terminated. The setSkipError (true) method can be called to ignore exceptions and complete the data parsing process. This method can be called to prevent exceptions from being thrown, but it still allows the program to output error messages to the error stream.

When rows are parsed as tags, they are added to the XML document as elements of the XML document. Each record in the element has a default name line. you can also call setDataLineName () to specify the name. Each data record is a column element. the column element name 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, which you can further process. Since data is organized in a well-known tree structure, it is very easy to process. For example, you can send this Document to a third party. as long as the third party knows the Document Type Definition (DTD) of this Document, it can easily process the Document. You can also call the writeDocument (Document doc, OutputStream osOut) method to save this Document. Code List D provides an example of saving an XML document to a file.

Use XSLT conversion to view data

You can also convert XML data to other formats and express the content in different views. The simplest method is to use XSLT for conversion. It provides a powerful tree-oriented conversion language, you can convert an XML instance that uses a vocabulary into a simple text, HTML, or XML that uses other vocabulary.

For the given XML input, you can use XSLT to create the expected output. For example, by executing transformData (InputStream xmlIn, InputStream processing in, OutputStream transfOut), you can convert XML data into an HTML document. Code List E provides an example of XSLT conversion, while list F provides the HTML view of address book entries.

Simplify Data management

In this article, we learned how to convert common format data into XML documents by analyzing the PlainTextToXmlFormatter class. We also see how to use different views to represent XML documents by means of XSLT conversion. When you need to process data in various formats, adopting the above technology may be a good solution, which saves your valuable time and reduces the possibility of errors.

The above section details the benefits of converting data into XML format. For more information, see PHP Chinese website (www.php1.cn )!

Related Article

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.