Parsing XHTML using SAX to change only the PCDATA

360 Views Asked by At

I am looking for an example of using Sax parser to parse an XHTML string and change every single occurence of a string (s1) to (s2) if s1 is in the PCDATA not attributes. Can somebody shed some lights here....

thanks!

1

There are 1 best solutions below

0
On

You would just implement the

characters(char[] ch, int start, int length) 

method of ContentHandler and look for s1 and replace with s2.

Of course, this assumes that your content handler is outputing an xml to some stream.

From your description, really you probably want to use an XMLFilter to modify the xml stream in place.