An example
There are now many data formats that implement data storage, export, import and transmit functions within or between software. The most common are delimited formats, such as comma or tab-separated data formats and fixed-length data formats. Suppose we have an address book program that provides the function of exporting data into comma-separated and fixed-length formats.
In comma-separated format, use commas to separate different fields in the same data record, such asList AShown. In the fixed-length data format, each field recorded should have a standard length.List BA fixed-length format address book is given.
Create XML Document
Now, let's parse the input data and convert it into an XML document. XML documents (i.e. org.) are the original data type of the All Document Object Model (DOM) and it provides access to document data.
You can use the buildDocument(InputStream is) method to create a document corresponding to your data, such asCode List CShown. This method reads the input data stream line by line and analyzes it line by line according to the given frame.
If you want to parse delimited format data, you need to create an instance of the class with the constructor PlainTextToXmlFormatter(String[] colName,String delim), whose delimiter can be any string. In the case of fixed-length format, you should use the second constructor PlainTextToXmlFormatter(String[] colName,int[] colLen), one of its arguments is the field length array. 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 of names that hold the target data record. In the example we give, the names are firstName, lastName, email, and phone number.
The actual parsing process of converting data lines 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 from the above two methods. If the data format is wrong, an exception will be thrown and the parsing error will be terminated. Calling the setSkipError(true) method can ignore exceptions and complete the data parsing process. Calling this method prevents exceptions from being thrown, but it still allows the program to print error messages 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 row of records placed in the element has a default nameline, you can also call setDataLineName() to specify the 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 fully read, you get a valid XML document that you can further process. Now, since the data is organized in a well-known tree structure, it is very simple to deal with it. For example, you can hand this document over to a third party. As long as the third party knows the document type definition (Document Type Definition, DTD), it can easily process the document. You can also call the writeDocument(Document doc, OutputStream osOut) method to save this document.Code List DLet's give an example of saving an XML document into a file.
Use XSLT conversion to view data
You can also convert XML data into other formats and use different views to represent its content. The easiest way is to use XSLT conversion, which provides a powerful tree-oriented conversion language implementation that converts XML instances using one vocabulary into simple text, HTML, or XML using other vocabulary.
For a given XML input, you can use XSLT 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 List EAn example of XSLT conversion is provided, andList FThe HTML view of the address book entry is given.
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 represent XML documents with different views with the help of XSLT transformations. When you need to process data in various formats, adopting the above techniques may be a good solution, saving you valuable time and reducing the possibility of errors.