[
Lists Home |
Date Index |
Thread Index
]
You need a ContentHandler (you can extend the
org.xml.sax.helpers.DefaultHandler). The ContentHandler is the receiver
of the events from the parser. The XMLFilter is a filter that sits
between a parser and a contenthandler and allow you to filter the events
between the parser and the contenthandler. The parser "parses" the xml
file into sax events like startElement etc. The contenthandler is the
receiver of these events. Now what you do with the events is up to you!
you could build a DOM tree, or just do a pretty print to you screen!
Get a newer version of Xerces!
reg, Niels Peter
Your modified code:
import org.xml.sax.*;
import org.xml.sax.helpers.*;
public class XMLCapitalizer {
public XMLCapitalizer(String infile) {
ContentHandler capitalizer = new Capitalizer();
try {
XMLReader eppReader =
XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
XMLFilter capfilter = new CapitalizerFilter(eppReader);
capfilter.setContentHandler(capitalizer);
capfilter.parse(infile);
}
catch (Exception e) {
e.printStackTrace();
}
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
public static void main (String args[]) {
if (args.length != 1) {
System.out.println("usage: java XMLCapitalizer infile");
System.exit(0);
}
new XMLCapitalizer(args[0]);
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
class Capitalizer extends DefaultHandler {
public void characters(char ch[], int start, int length)
throws SAXException {
String s = new String(ch, start, length);
System.out.println("ch array is now: " + s);
}
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
class CapitalizerFilter extends XMLFilterImpl {
public CapitalizerFilter(XMLReader reader) {
super(reader);
}
public void characters(char ch[], int start, int length)
throws SAXException {
for (int i = start; i < length; i++) {
if (Character.isLowerCase(ch[i])) {
ch[i] = Character.toUpperCase(ch[i]);
}
}
super.characters(ch,start,length);
}
}
}
On lördag, december 22, 2001, at 12:43 , Cubeta, James wrote:
> import java.io.FileWriter;
> import org.xml.sax.*;
> import org.xml.sax.helpers.*;
> import com.megginson.sax.*;
>
> public class XMLCapitalizer {
>
> public XMLCapitalizer(String infile, String outfile) {
>
> try {
> XMLReader eppReader =
> XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
> XMLWriter eppWriter = new XMLWriter(eppReader, new
> FileWriter(outfile));
> CapitalizerFilter filter = new CapitalizerFilter(eppWriter);
> InputSource source = new InputSource(infile);
> filter.parse(source);
>
> }
> catch (Exception e) {
> e.printStackTrace();
> }
> }
>
> // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>
> public static void main (String args[]) {
> if (args.length != 2) {
> System.out.println("usage: java XMLCapitalizer infile
> outfile");
> System.exit(0);
> }
> new XMLCapitalizer(args[0], args[1]);
> }
>
> // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>
> private class CapitalizerFilter extends XMLFilterImpl {
>
> public CapitalizerFilter(XMLReader reader) {
> super(reader);
> }
>
> public void characters(char ch[], int start, int length)
> throws SAXException {
>
> for (int i = start; i < length; i++) {
> if (Character.isLowerCase(ch[i])) {
> ch[i] = Character.toUpperCase(ch[i]);
> }
> }
> String s = new String(ch, start, length);
> System.out.println("ch array is now: " + s);
> super.characters(ch,start,length);
> }
> }
> }
|