NanoXML/Lite 2.2

Chapter 2. Retrieving Data From An XML Datasource

This chapter shows how to retrieve XML data from a standard data source. Such source can be a file, an HTTP object or a text string.

2.1. A Very Simple Example
2.2. Analyzing The Data
2.3. Generating XML

2.1. A Very Simple Example

This section describes a very simple XML application. It parses XML data from a stream and dumps it to the standard output. While its use is very limited, it shows how to set up a parser and parse an XML document.

import nanoxml.*;                                    //1
import java.io.*;

public class DumpXML
{
    public static void main(String[] args)
        throws Exception
    {
        XMLElement xml = new XMLElement();           //2
        FileReader reader
            = new FileReader("test.xml");
        xml.parseFromReader(reader);                 //3
        System.out.println(xml);                     //4
    }
}

  1. The NanoXML classes are located in the package nanoxml.
  2. This command creates an empty XML element.
  3. The method parseFromReader parses the data in the file test.xml and fills the empty element.
  4. The XML element is dumped to the standard output.

2.2. Analyzing The Data

You can easily traverse the logical tree generated by the parser. By calling on of the parse* methods, you fill an empty XML element with the parsed contents. Every such object can have a name, attributes, #PCDATA content and child objects.

The following XML data:

<FOO attr1="fred" attr2="barney">
    <BAR a1="flintstone" a2="rubble">
        Some data.
    </BAR>
    <QUUX/>
</FOO>

is parsed to the following objects:

Element FOO:
    Attributes = { "attr1"="fred", "attr2"="barney" }
    Children = { BAR, QUUX }
    PCData = null

Element BAR:
    Attributes = { "a1"="flintstone", "a2"="rubble" }
    Children = {}
    PCData = "Some data."

Element QUUX:
    Attributes = {}
    Children = {}
    PCData = null

You can retrieve the name of an element using the method getName, thus:

FOO.getName() ==> "FOO"

You can enumerate the attribute keys using the method enumerateAttributeNames:

Enumeration enum = FOO.enumerateAttributeNames();
while (enum.hasMoreElements()) {
    System.out.print(enum.nextElement());
    System.out.print(' ');
}
    ==>attr1 attr2

You can retrieve the value of an attribute using getAttribute:

FOO.getAttribute("attr1") ==> "fred"

The child elements can be enumerated using the method enumerateChildren:

Enumeration enum = FOO.enumerateChildren();
while (enum.hasMoreElements()) {
    XMLElement child = (XMLElement) enum.nextElement();     System.out.print(child.getName() + ' ');
}
    ==> BAR QUUX

If the element contains parsed character data (#PCDATA) as its only child. You can retrieve that data using getContent:

BAR.getContent() ==> "Some data."

Note that in NanoXML/Lite, a child cannot have children and #PCDATA content at the same time.

2.3. Generating XML

You can very easily create a tree of XML elements or modify an existing one.

To create a new tree, just create an XMLElement object:

XMLElement elt = new XMLElement("ElementName");

You can add an attribute to the element by calling setAttribute.

elt.setAttribute("key", "value");

You can add a child element to an element by calling addChild:

XMLElement child = new XMLElement("Child");
elt.addChild(child);

If an element has no children, you can add #PCDATA content to it using setContent:

child.setContent("Some content");

Note that in NanoXML/Lite, a child cannot have children and #PCDATA content at the same time.

When you have created or edited the XML element tree, you can write it out to an output stream or writer using the method toString:

java.io.Writer output = ...;
XMLElement xmltree = ...;
output.println(xmltree);

Copyright ©2000-2002 Marc De Scheemaecker, All Rights Reserved.
Last update: January 8th, 2002.
Valid HTML 4.01! Valid CSS! SourceForge