How to get the text kept between two html tags

Ronald Bourret rbourret at ito.tu-darmstadt.de
Thu Aug 12 16:18:34 BST 1999


Srinivasan_KS wrote:

> I want to get the information kept between  html tags or attributes.
> How do I do this using EXPAT or using SAX?
> EG:-
> <TEXT1>INFO1<TEXT1>
> <TEXT2>INFO2<TEXT2>
> How to extract INFO1 and INFO2 and store it in some other file.

Presumably, you mean XHTML or XML?  Expat and other XML parsers won't work 
on non-well-formed HTML documents, which is most.

In SAX, implement the DocumentHandler interface and register your 
implementation with a SAX-enabled parser. When the parser calls 
startElement(), you'll need to keep the element name (TEXT1 or TEXT2) as 
state. You can also retrieve attribute values at this time (they are passed 
to startElement). The parser will call characters() to pass you the text. 
Based on the state (TEXT1 or TEXT2), you can store it appropriately. Note 
that the parser can make multiple calls to characters() -- that is, it 
could call characters() five times (once for each character), once (with 
all the characters), or anything in between.

-- Ron Bourret


xml-dev: A list for W3C XML Developers. To post, mailto:xml-dev at ic.ac.uk
Archived as: http://www.lists.ic.ac.uk/hypermail/xml-dev/ and on CD-ROM/ISBN 981-02-3594-1
To (un)subscribe, mailto:majordomo at ic.ac.uk the following message;
(un)subscribe xml-dev
To subscribe to the digests, mailto:majordomo at ic.ac.uk the following message;
subscribe xml-dev-digest
List coordinator, Henry Rzepa (mailto:rzepa at ic.ac.uk)





More information about the Xml-dev mailing list