Class StdXMLReader
net.n3.nanoxml
public class StdXMLReader
extends java.lang.Object
implements net.n3.nanoxml.IXMLReader
The StdXMLReader class implements IXMLReader, an interface called by StdXMLParser to read XML data from a data source. It is the default implementation of IXMLReader used by XMLParserFactory.
Since you probably won't be implementing your own reader or parser, we'll only discuss two methods in this class. They allow you to choose one of two different data sources: java.io.Reader or java.lang.String.
public StdXMLReader(Reader reader)
public static IXMLReader stringReader(String str)
The first method, the constructor, is the one to use to read from a Reader. The second, static method, stringReader(), is the method to use to read from a String.
Class StdXMLParser
net.n3.nanoxml
public class StdXMLParser
extends java.lang.Object
implements net.n3.nanoxml.IXMLParser
The StdXMLParser class implements the IXMLParser interface. This interface is provided so that you can write your own parser and plug it into your application, if needed. StdXMLParser is a default implementation and is used by XMLParserFactory.
Unless you plan on implementing your own parser that meets the IXMLParser contract, you should use instances of StdXMLParser. Here are its methods:
public StdXMLParser()
public void setBuilder(IXMLBuilder builder)
public void setReader(IXMLReader reader)
public void setValidator(IXMLValidator validator)
public Object parse()
throws IOException
You really will not need to call any of these methods unless you:
- Do not want to use the default parser class (StdXMLParser) when calling XMLParserFactory for parser instances
- Want to avoid using XMLParserFactory altogether (as discussed in the section Class XMLParserFactory, page 608)
The parse() method returns the top-most XMLElement after parsing completes. You can safely narrow the object returned by parse() to an XMLElement. Here are the arguments.
Arguments
| Arguments
|
Type
|
Effect
|
| builder |
net.n3.nanoxml. IXMLBuilder |
The object that
builds a tree of XMLElement nodes from a data source |
| reader |
net.n3.nanoxml.
IXMLReader |
The objects that
reads the data to be parsed |
| validator |
net.n3.nanoxml.
IXMLValidator |
The object that
processes the DTD and resolves entity references. No document validation is
performed! |
Usage and Examples
Here is an example that parses a document using the XMLParserFactory and StdXMLParser classes.
StdXMLBuilder builder = new StdXMLBuilder();
IXMLParser parser = XMLParserFactory.createDefaultXMLParser(builder,
new StdXMLReader(new BufferedReader(
new FileReader("request.xml"))), new NonValidator());
XMLElement root = (XMLElement)parser.parse();
Class XMLWriter
net.n3.nanoxml
public class XMLWriter
extends java.lang.Object
The XMLWriter class is used to output an XML document. In version 1.6.7, this functionality was included in the XMLElement class as write() methods. In version 2.0, however, a new class has been created to handle output, making the library more object-oriented.
public XMLWriter(Writer writer)
public XMLWriter(OutputStream st)
public void write(XMLElement elem)
public void write(XMLElement elem, int indent)
The entire document, or a document fragment, can be written to any class extending java.io.Writer or java.io.OutputStream. There are no output options; all attributes and #PCDATA are always output for each element.
Arguments
| Arguments |
Type |
Effect |
| writer |
java.io. Writer |
The writer
object to which output is written |
| st |
java.io. OutputStream |
The stream to which
output is written |
| elem |
net.n3.nanoxml.
XMLElement |
The element to
output. Its children are also output. |
| indent |
int |
Number of spaces to
indent for each new child of element |
Usage and Examples
We'll use the example we've been building upon to write a full application that parses an XML document called request.xml and echoes its contents to System.out:
import net.n3.nanoxml.*;
import java.io.*;
public class Echo {
public Echo() throws Exception {
StdXMLBuilder builder = new StdXMLBuilder();
IXMLParser parser =
XMLParserFactory.createDefaultXMLParser(builder,
new StdXMLReader(new BufferedReader(new
FileReader("request.xml"))), new NonValidator());
XMLElement root = (XMLElement)parser.parse();
//create a writer and output the document to System.out
XMLWriter writer = new XMLWriter(System.out);
writer.write(root);
}
public static void main(String[] args) throws Exception {
Echo e = new Echo();
}
}
Coming Up Next: MinML