OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.

 


Help: OASIS Mailing Lists Help | MarkMail Help

 


 

   XML, Java, & filters question...

[ Lists Home | Date Index | Thread Index ]

hello again,

i am trying to write a very simple Java app to demonstrate the use of SAX
filters on XML. i am new to XML, and i am reading Brett McLaughlin's "Java &
XML" [O'Reilly]. i am also using the Xerces 1.0 parser from Apache and
Megginson's add-on XMLWriter class.

here is all i want to do: read in an XML document and, when i encounter data
between tags, capitalize it. to paraphrase my code, here is what i am doing:

   1. create an XMLReader
   2. create an XMLWriter, which is chained to the reader and a FileWriter
   3. create a filter object with the writer
   4. create an InputSource 
   5. tell the filter to parse the InputSource object

my custom filter extends the XMLFilterImpl class and overrides the
characters() method. i have included my code below. it compiles and executes
just fine, and seems to behave appropriately, but the generated output file
is unchanged.

what am i doing wrong here?

thanks in advance for your help - sorry if this is too much of an
entry-level topic for this list!

happy holidays,
james
---------------------------------------------------------------------------
James A. Cubeta                                        jcubeta@verisign.com
VeriSign Global Registry Services                           v: 703.948.3326
21345 Ridgetop Circle, #LS2-2-1                             f: 703.421.8709
Dulles, VA 20166                                       www.verisign-grs.com

------------------------------
my input file:
<?xml version="1.0"?>
<james>
<data>convert this to all caps, please!</data>
</james>

------------------------------
my source code:

// compile: javac XMLCapitalizer.java
// run: java XMLCapitalizer infile outfile

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);
      }
   }
}




 

News | XML in Industry | Calendar | XML Registry
Marketplace | Resources | MyXML.org | Sponsors | Privacy Statement

Copyright 2001 XML.org. This site is hosted by OASIS