Home > Backend Development > XML/RSS Tutorial > Detailed introduction to the benefits of converting data into XML format

Detailed introduction to the benefits of converting data into XML format

黄舟
Release: 2017-03-04 17:15:35
Original
1494 people have browsed it

We often encounter situations where we need to process data saved or transmitted in various formats (from comma or tab-delimited files to more loadable formats), and you need to respond to each format. parser. This shortcoming slows down development and can lead to errors. One solution is to convert data in commonly used formats into xml documents, and then save, process or convert them into other formats.

An example

There are now many data formats that implement data saving, exporting, importing and transferring functions within or between software. The most common are delimited formats, such as comma or tab-delimited data formats and fixed-length data formats. Suppose we have an address book program that provides functionality for exporting data into comma-separated and fixed-length formats.

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

Create XML Document

Now, let us parse the input data and convert it into an XML document. The XML document (ie org.w3c.dom.Document) is the primitive data type of the entire Document Object Model (DOM), and it provides access to document data.

You can create a document corresponding to your data by executing the buildDocument(InputStream is) method, as shown in code listing C. This method reads the input data stream line by line and analyzes it line by line according to the given grid.

If you want to parse delimited format data, you need to create an instance of the class whose constructor is PlainTextToXmlFormatter(String[ ] colName, String delim), and its delimiter can be any string. In the case of fixed-length formats, you should use the second constructor PlainTextToXmlFormatter(String[ ] colName,int[ ] colLen), which takes an array of field lengths as a parameter. In the address book example we gave, the lengths of each field are 10, 10, 30, and 10 characters respectively. The parameter colName is an array that stores the names of the target data records. In the example we gave, the names are first name (firstName), last name (lastName), email, and phone number (phone).

The actual parsing process of converting data rows into data symbols is the process of executing the getStringArray(String read, String delim) or parseFixedLengthMessage(String read, int[ ] colLen) method. The return value is a String array, which is obtained by decomposing the given input by the above two methods. If the data format is incorrect, an exception will be thrown and the parsing error will terminate. Call the setSkipError(true) method to ignore the exception and complete the data parsing process. Calling this method prevents the exception from being thrown, but it still allows the program to print error information to the error output stream.

When lines are parsed into tags, they are added to the XML document as elements of the XML document. Each line of records placed into the element has a default name line. You can also call setDataLineName() to specify a name. Each data record is a column element, the name of the column element is provided by the corresponding class constructor, and the child elements are added to the row element.

After the input data is completely read, you have a valid XML document that you can further process. Now, since the data is organized in the well-known tree structure, working with it is very simple. For example, you can send this document to a third party. As long as the third party knows the document type definition (Document Type Definition, DTD) of the document, then he can easily process the document. You can also call the writeDocument(Document doc, OutputStream osOut) method to save this document. Code Listing D gives an example of saving an XML document to a file.

Use XSLT transformation to view data

You can also convert XML data into other formats and use different views to represent its content. The simplest method is to use XSLT transformation, which provides a powerful tree-oriented transformation language implementation that can transform XML instances using one vocabulary into simple text, HTML, or XML using other vocabularies.

For a given XML input, you can use the XSLT language to create your desired output. For example, you can convert XML data into an HTML document by executing transformData(InputStream xmlIn, InputStream xslIn, OutputStream transfOut). Listing E provides an example of an XSLT transformation, while Listing F gives an HTML view of an address book entry.

Simplify data management

In this article, we learned how to convert commonly used 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 various formats, adopting the above technology may be a good solution, saving you valuable time and reducing the possibility of errors.

The above is a detailed introduction to the benefits of converting data into XML format. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template