If you're retrieving data from a stream, but you don't want to wait to process the data
until it's completely read, you can use streaming.
3.1. The XML Builder
3.2. Registering an XML Builder
3.1. The XML Builder
The XML data tree is created using an XML builder. By default, the builder creates
a tree of IXMLElement.
While the parser parses the data, it notifies the builder of any elements it encounters.
Using this information, the builder generate the object tree. When the parser is done
processing the data, it retrieves the object tree from the builder using getResult.
The following example shows a simple builder that prints the notifications on the
standard output.
import java.io.*;
import net.n3.nanoxml.*;
public class MyBuilder
implements IXMLBuilder
{
public void startBuilding(String systemID,
//1
int lineNr)
{
System.out.println("Document started");
}
public void
//2
newProcessingInstruction(String target,
Reader reader)
throws IOException
{
System.out.println("New PI with target "
+ target);
}
public void startElement(String name,
//3
String nsPrefix,
String nsSystemID,
String systemID,
int lineNr)
{
System.out.println("Element started: " + name);
}
public void endElement(String name,
//4
String nsPrefix,
String nsSystemID)
{
System.out.println("Element ended: " + name);
}
public void addAttribute(String key,
//5
String nsPrefix,
String nsSystemID,
String value,
String type)
{
System.out.println(" " + key + ": "
+ type + " = " + value);
}
public void elementAttributesProcessed(
//6
String name,
String nsPrefix,
String nsSystemID)
{
// nothing to do
}
public void addPCData(Reader reader,
//7
String systemID,
int lineNr)
throws IOException
{
System.out.println("#PCDATA");
}
public Object getResult()
//8
{
return null;
}
}
- The XML parser started parsing the document. The lineNr parameter
contains the line number where the document starts.
- The XML parser encountered a processing instruction (PI) which is not
handled by the parser itself. The target contains the target of the PI.
The contents of the PI can be read from reader.
- A new element has been started at line lineNr. The name of the element
is name.
- The current element has ended. For convenience, the name of that element
is put in the parameter name.
- An attribute is added to the current element.
- This method is called when all the attributes of the current element
have been processed.
- A #PCDATA section has been encountered. The contents of the section
can be read from reader.
- This method is called when the parsing has finished. If the builder has a
result, it has to return it to the parser in this method.
3.1. Registering an XML Builder
You can register the builder to the parser using the method setBuilder.
The following example shows how to create a parser which uses the builder we created
in the previous section:
import net.n3.nanoxml.*;
import java.io.*;
public class DumpXML
{
public static void main(String args[])
throws Exception
{
IXMLParser parser
= XMLParserFactory
.createDefaultXMLParser();
IXMLReader reader
= StdXMLReader.fileReader("test.xml");
parser.setReader(reader);
parser.setBuilder(new MyBuilder());
parser.parse();
}
}