public class XmlParser extends Object
When you create a class for parsing XML files, it is necessary to create a subclass of org.xml.sax.ContentHandler for a content handler object. The XML parser calls methods in this class when various events occur while reading the XML files. Events would be such things as the start or end of a tagged element.
In order to use this class, this class it is necessary to have xercesImpl.jar and xml-apis.jar from the Apache Xerces project in the CLASSPATH.
There are two sets of source code for this class, one for versions previous to Java 5 and the other for Java 5 and later.
When constructing code using Vector classes, it is necessary to code differently for Java 5 and later versions because version 5 introduced the idea of parameterizing Vector objects.
For Java 5 and later, you will see the syntax
Vector<String>
. This means
objects making up the Vector are all of type String.
For versions before Java 5, the syntax is simply
Vector
. This is because there is no method in
the earlier versions of restricting Vectors and other list
to a single class.
The following is an example of a file to be processed by this object.
<html><head></head>
<body>aaaa</body></html>
getTags
,
parseFile
,
ContentHandler
Modifier and Type | Class and Description |
---|---|
protected class |
XmlParser.MyHandler
This class provides the means of responding to
the Xerces parser from the Apache Xerces
parser.
|
Modifier and Type | Field and Description |
---|---|
private int |
debugLevel
Amount of diagnostic listing to be generated.
|
private Vector<String> |
items
Vector containing strings or lists of tags in document
|
static int |
LISTTAGS
Option for listing tags in document.
|
private int |
mode
Indicates mode of parsing operation.
|
static int |
SEARCH
Option for searching for strings with specified tags.
|
Constructor and Description |
---|
XmlParser() |
Modifier and Type | Method and Description |
---|---|
int |
getMode()
Getter for mode
|
private Vector<String> |
internalParse(Vector<String> start,
InputSource document,
int mode) |
private Vector<String> |
internalParse(Vector<String> start,
InputSource document,
String search,
int mode) |
Vector<String> |
listTags(File document)
This method lists tags contained in an XML document.
|
Vector<String> |
listTags(String document)
List strings contained in document.
|
Vector<String> |
listTags(Vector<String> start,
File document)
This method lists tags contained in an XML document.
|
Vector<String> |
listTags(Vector<String> start,
String document)
List tags contained in an XML document.
|
Vector<String> |
parseString(File document,
String search)
This method parses an XML document for strings
|
Vector<String> |
parseString(String document,
String search)
This method parses an XML document for strings
|
Vector<String> |
parseString(Vector<String> start,
File document,
String search)
This method parses an XML document for strings
|
Vector<String> |
parseString(Vector<String> start,
String document,
String search)
This method parses an XML document for strings
|
void |
setDebugLevel(int level)
Determine amount of diagnostic output.
|
private int mode
public static final int LISTTAGS
public static final int SEARCH
private int debugLevel
public XmlParser()
public int getMode()
public void setDebugLevel(int level)
level
- Amount of diagnostic material to be printed. 0
is default and results in no diagnostic messages. Higher values
produce more diagnostic messages.public Vector<String> parseString(Vector<String> start, String document, String search)
start
- Initial Vector of String objectsdocument
- This string contains the document to be parsedsearch
- This string indicates the set of tags to be searched
for. If the value is
<Envelope><Body><FetchHandle>
,
the program will return the contents of all
FetchHandle
tags which are within
Body
tags which are within
Envelope
tags.public Vector<String> parseString(String document, String search)
document
- This string contains the document to be parsedsearch
- This string indicates the set of tags to be searched
for. If the value is
<Envelope><Body><FetchHandle>
,
the program will return the contents of all
FetchHandle
tags which are within
Body
tags which are within
Envelope
tags.public Vector<String> parseString(File document, String search)
document
- This string contains the File object representing
the file to be parsed.search
- This string indicates the set of tags to be searched
for. If the value is
<Envelope><Body><FetchHandle>
,
the program will return the contents of all
FetchHandle
tags which are within
Body
tags which are within
Envelope
tags.public Vector<String> parseString(Vector<String> start, File document, String search)
start
- Vector containing the String objects at the
start executing the methoddocument
- This string is the File object to be parsedsearch
- This string indicates the set of tags to be searched
for. If the value is
<Envelope><Body><FetchHandle>
,
the program will return the contents of all
FetchHandle
tags which are within
Body
tags which are within
Envelope
tags.public Vector<String> listTags(Vector<String> start, String document)
This method returns a Vector containing String objects.
Each String object contains a sequence of tags found in the document.
start
- Initial vector of String objects to which
items are to be appended.document
- Document to be parsed.public Vector<String> listTags(String document)
document
- Document to be parsed.public Vector<String> listTags(File document)
document
- This string contains the File object representing
the file to be parsed.public Vector<String> listTags(Vector<String> start, File document)
start
- Vector containing the String objects at the
start executing the methoddocument
- Object containing the file to be parsedprivate Vector<String> internalParse(Vector<String> start, InputSource document, int mode)
private Vector<String> internalParse(Vector<String> start, InputSource document, String search, int mode)