[Batchserver-cvs] batchserver/src/org/jmonks/batchserver/io/xml package.html, 1.1, 1.2
Brought to you by:
suresh_pragada
From: Suresh <sur...@us...> - 2006-06-15 03:35:55
|
Update of /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/xml In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv715/xml Modified Files: package.html Log Message: no message Index: package.html =================================================================== RCS file: /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/io/xml/package.html,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** package.html 13 Jun 2006 04:02:33 -0000 1.1 --- package.html 15 Jun 2006 03:35:52 -0000 1.2 *************** *** 5,9 **** </head> <body> ! Explain the usage of XML File IO API. </body> </html> --- 5,101 ---- </head> <body> ! Describes the xml file implementation and its usage. ! <p> ! XML files are becoming popular choice by the enterprises ! to exchange the data. This package provides the implementation to work with ! the XML files. Implementation ! assumes that file consists of set of records and each can be identified using ! the xpath. ! </p> ! ! <p> ! <h3>XML File Implementation</h3> ! XML files consists of set of different records where each record ! can be identified by using xpath expression and all the elements ! beneath the element identified by xpath are treated as fields of that record. ! <h5>Defining the file spec for xml files</h5> ! File spec which descibes the xml file expects the <code>file-type</code> ! attribute value should be "xml". It requires one additional ! attribute along with the <code>file-type</code> attribute, which is ! <code>root-element</code> which will be used highly while generating the ! xml file from the set of records. The value in the attribute specifies the ! root element of the xml document. ! <pre> ! <file-spec file-type="xml" root-element="sample-root> ! </file-spec> ! </pre> ! There could be multiple record specs exists in a file spec. Along with the ! record-type attribute, it requires an additional attribute <code>record-xpath</code> ! which tells the xpath in the document to identify the record. ! <pre> ! <file-spec file-type="xml" root-element="sample-root> ! <record-spec record-type="DETAIL" record-xpath="/sample-root/detail-record"/> ! </file-spec> ! </pre> ! Since all the elements beneath the element represented by record spec will be ! exposed as a field values, record spec doesnt require any field specs. All the ! simple elements in the record element will be exposed as field name and values ! where element name represents the field name and element value represents the field ! value. All the complex elements(nested elements or elements contains child elements) ! will be exposed as a records again. All the repeat elements(elements which can ! repeat more than once) values will be exposed as list. ! ! <h5>Reading the records from xml files</h5> ! Reading the records from the xml files is fairly simple. ! <pre style="color:green"> ! <i> ! InputStream fileSpecInputStream= // Get the input stream for the XML file contains the file structure. ! InputStream fileInputStream= // Get the input stream of the file to be read. ! FileReader reader=FileReader.getFileReader(fileInputStream,fileSpecInputStream); ! ReaderRecord record=null; ! while((record=reader.getNextRecord())!=null) ! { ! if(record.getRecordType().equals(RecordType.DETAIL)) ! { ! XMLFileReader.XMLReaderRecord xmlRecord=(XMLFileReader.XMLReaderRecord)record; ! String fieldValue1=xmlRecord.readSimpleElement("field-name1"); ! ! List repeatElement=xmlRecord.readRepeatElement("field-name2"); ! //Read the repeated values from the list. ! ! XMLFileReader.XMLReaderRecord complexRecord=(XMLFileReader.XMLReaderRecord)xmlRecord.readComplexElement("field-name3"); ! String fieldValue4=complexRecord.readSimpleElement("field-name4"); ! // Read the rest of the field and does the processing. ! } ! } ! reader.close(); ! </i> ! </pre> ! <h5>Writing the records into xml files</h5> ! Writing the records into xml files is fairly simple. ! <pre style="color:green"> ! <i> ! InputStream fileSpecInputStream= // Get the input stream for the XML file contains the file structure. ! OutputStream fileOutputStream= // Get the output stream of the file to be written/generated. ! FileWriter writer=FileWriter.getFileWriter(fileOutputStream,fileSpecInputStream); ! ! XMLFileWriter.XMLWriterRecord record=(XMLFileWriter.XMLWriterRecord)writer.createWriterRecord(RecordType.DETAIL); ! record.writeSimpleElement("field-name1","field-value1"); ! ! List repeateElement=writer.createRepeateElement("field-name2"); ! repeateElement.add("field-value21"); ! repeateElement.add("field-value22"); ! ! XMLFileWriter.XMLWriterRecord complexRecord=(XMLFileWriter.XMLWriterRecord)writer.createComplexElement("field-name3"); ! complexRecord.writeSimpleElement("field-name4","field-value4"); ! // Write all the other field values into the writer record. ! ! writer.writeRecord(record); ! writer.close(); ! </i> ! </pre> ! </p> ! ! </body> </html> |