[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: XML, Java, & filters question...

  • To: "Cubeta, James" <jcubeta@v...>, xml-dev@l...
  • Subject: Re: XML, Java, & filters question...
  • From: Niels Peter Strandberg <nielspeter@n...>
  • Date: Wed, 26 Dec 2001 15:32:28 +0100
  • In-reply-to: <3CD14E451751BD42BA48AAA50B07BAD6016063BC@v...6>

java pretty print xml
You can set the XMLWriter to be the receiving ContentHandler.


Code:

public class XMLCapitalizer {

    public XMLCapitalizer(String infile, String outfile) {

       try {
          XMLReader eppReader = 
XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
	
	 XMLFilter capfilter = new CapitalizerFilter(eppReader);

	 XMLWriter xmlwriter = new XMLWriter(new FileWriter(outfile));
	
	 capfilter.setContentHandler(xmlwriter);
	
          capfilter.parse(infile);

       }
       catch (Exception e) {
          e.printStackTrace();
       }
    }


Regards, Niels Peter


On lördag, december 22, 2001, at 07:26 , Cubeta, James wrote:

> hello,
>
>> -----Original Message-----
>> 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!
>
> thanks for the info and modified code, however i still cannot get it to
> work. i pretty much copied-n-pasted what you gave me into my program, 
> but
> now it doesn't write *anything* to the output file! i'm sure i'm doing
> something really stupid, and i apologize for needed to be handheld. i 
> guess
> i gotta read more...
>
> feeling really dumb,
> james
> ---------------------------------------------------------------------------
> James A. Cubeta                                        
> jcubeta@v...
> 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 updated code:
> -----------------------------------
>
> 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));
>          XMLFilter capfilter = new CapitalizerFilter(eppReader);
>          ContentHandler capitalizer = new Capitalizer();
>          capfilter.setContentHandler(capitalizer);
>          capfilter.parse(infile);
>       }
>       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 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);
>       }
>    }
>
>    // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>
>    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);
>       }
>    }
> }
>


PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
 

Stylus Studio has published XML-DEV in RSS and ATOM formats, enabling users to easily subcribe to the list from their preferred news reader application.


Stylus Studio Sponsored Links are added links designed to provide related and additional information to the visitors of this website. they were not included by the author in the initial post. To view the content without the Sponsor Links please click here.

Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.