HierarchicalMap Tutorial - Intermediate Level
HierarchicalMap is an interface. So anyone can implement it. This tutorial is based on BasicHierarchicalmap, a reference implementation of HierarchicalMap.
Following subjects are covered in this tutorial:
Intermediate Level8. Handling Large XML
Reading whole XML can be time consuming and may require large amount of memory. In this chapter we will present another approach for XML handling.
First execute the following code:
At this point, the XML written so far is:
Here, we introduce three concepts:
Now, let's append the following piece of HierarchicalMap:
To do so, replace the code between first writing and closing methods by the following snippet of code:
The structure of resulting XML will be the following:
Then, append the next snippet of code before closing the XMLMapOutputStream. Note that the content of second chapter has changed.
The final structure of resulting XML will be the following:
Strictly saying, this is not a valid XML structure because it contains three root nodes. You probablely receive an error message if you try to open the resulting document using XML aware editor. However, this is still a valid HierarchicalMap.
Changing Reference Position
Adding HierarchicalMap at fixed reference is too limitted to write more complex XML. Now we will show you, how to append HierarchicalMap at arbitrary position. To do so, we must use MapWriter. First, prepare the following code and execute it.
The resulting XML will be the following:
First, the instantiation of MapWriter is pretty simple. Just instantiate it passing either MapOutputStream or XMLMapOutputStream. Then write down the map as usual using writeMap() method. Now, we have two more concepts:
The relationship of these positions for "mw", the first MapWriter created in the sample, after calling "mw.writeMap("Document", map);" are shown below:
Therefore, the following snippet of code extracted from previous example, will create a MapWriter "mw2" with reference set to the node "Section".
The line "mw2.writeMap("Chapter", chapter);" will append the chapter under latest position of "mw". The relationship of positions for "mw2" are shown below:
Reading Large XML
For same reason, there is a MapReader to allow partial reading of XML. Try the following code and note how is similar to MapWriter.
The result will be as below (Line brakes are inserted to fit into the screen. Actual result will produce only three lines):
<Document><Author>author's name</Author> <Summary>summary</Summary><Section></Section></Document> <Title>title 1</Title><Content>content 1</Content> <Title>title 2</Title><Content>content 2</Content>
Continue to Intermediate Level - Accessing Data Base