public class XmlParser extends Object
You need to define a class implementing the XmlHandler
interface: an object belonging to this class will receive the
callbacks for the events. (As an alternative to implementing
the full XmlHandler interface, you can simply extend the
HandlerBase
convenience class.)
Usage (assuming that MyHandler
is your implementation
of the XmlHandler
interface):
XmlHandler handler = new MyHandler(); XmlParser parser = new XmlParser(); parser.setHandler(handler); try { parser.parse("http://www.host.com/doc.xml", null); } catch (Exception e) { [do something interesting] }
Alternatively, you can use the standard SAX interfaces
with the SAXDriver
class as your entry point.
XmlHandler
,
HandlerBase
,
SAXDriver
Modifier and Type | Field and Description |
---|---|
static int |
ATTRIBUTE_CDATA
Constant: the attribute value is a string value.
|
static int |
ATTRIBUTE_DEFAULT_FIXED
Constant: the attribute was declared #FIXED.
|
static int |
ATTRIBUTE_DEFAULT_IMPLIED
Constant: the attribute was declared #IMPLIED.
|
static int |
ATTRIBUTE_DEFAULT_REQUIRED
Constant: the attribute was declared #REQUIRED.
|
static int |
ATTRIBUTE_DEFAULT_SPECIFIED
Constant: the attribute has a literal default value specified.
|
static int |
ATTRIBUTE_DEFAULT_UNDECLARED
Constant: the attribute is not declared.
|
static int |
ATTRIBUTE_ENTITIES
Constant: the attribute value is a list of entity names.
|
static int |
ATTRIBUTE_ENTITY
Constant: the attribute value is the name of an entity.
|
static int |
ATTRIBUTE_ENUMERATED
Constant: the attribute value is a token from an enumeration.
|
static int |
ATTRIBUTE_ID
Constant: the attribute value is a unique identifier.
|
static int |
ATTRIBUTE_IDREF
Constant: the attribute value is a reference to a unique identifier.
|
static int |
ATTRIBUTE_IDREFS
Constant: the attribute value is a list of ID references.
|
static int |
ATTRIBUTE_NMTOKEN
Constant: the attribute value is a name token.
|
static int |
ATTRIBUTE_NMTOKENS
Constant: the attribute value is a list of name tokens.
|
static int |
ATTRIBUTE_NOTATION
Constant: the attribute is the name of a notation.
|
static int |
ATTRIBUTE_UNDECLARED
Constant: the attribute has not been declared for this element type.
|
static int |
CONTENT_ANY
Constant: the element has a content model of ANY.
|
static int |
CONTENT_ELEMENTS
Constant: the element has element content.
|
static int |
CONTENT_EMPTY
Constant: the element has declared content of EMPTY.
|
static int |
CONTENT_MIXED
Constant: the element has mixed content.
|
static int |
CONTENT_UNDECLARED
Constant: an element has not been declared.
|
static int |
ENTITY_INTERNAL
Constant: the entity is internal.
|
static int |
ENTITY_NDATA
Constant: the entity is external, non-XML data.
|
static int |
ENTITY_TEXT
Constant: the entity is external XML data.
|
static int |
ENTITY_UNDECLARED
Constant: the entity has not been declared.
|
Constructor and Description |
---|
XmlParser()
Construct a new parser with no associated handler.
|
Modifier and Type | Method and Description |
---|---|
Enumeration |
declaredAttributes(String elname)
Get the declared attributes for an element type.
|
Enumeration |
declaredElements()
Get the declared elements for an XML document.
|
Enumeration |
declaredEntities()
Get declared entities.
|
Enumeration |
declaredNotations()
Get declared notations.
|
String |
getAttributeDefaultValue(String name,
String aname)
Retrieve the default value of a declared attribute.
|
int |
getAttributeDefaultValueType(String name,
String aname)
Retrieve the default value type of a declared attribute.
|
String |
getAttributeEnumeration(String name,
String aname)
Retrieve the allowed values for an enumerated attribute type.
|
String |
getAttributeExpandedValue(String name,
String aname)
Retrieve the expanded value of a declared attribute.
|
int |
getAttributeType(String name,
String aname)
Retrieve the declared type of an attribute.
|
int |
getColumnNumber()
Return the current column number.
|
String |
getElementContentModel(String name)
Look up the content model of an element.
|
int |
getElementContentType(String name)
Look up the content type of an element.
|
String |
getEntityNotationName(String eName)
Get the notation name associated with an NDATA entity.
|
String |
getEntityPublicId(String ename)
Return an external entity's public identifier, if any.
|
String |
getEntitySystemId(String ename)
Return an external entity's system identifier.
|
int |
getEntityType(String ename)
Find the type of an entity.
|
String |
getEntityValue(String ename)
Return the value of an internal entity.
|
int |
getLineNumber()
Return the current line number.
|
String |
getNotationPublicId(String nname)
Look up the public identifier for a notation.
|
String |
getNotationSystemId(String nname)
Look up the system identifier for a notation.
|
String |
intern(char[] ch,
int start,
int length)
Create an internalised string from a character array.
|
String |
intern(String s)
Return an internalised version of a string.
|
void |
parse(String systemId,
String publicId,
InputStream stream,
String encoding)
Parse an XML document from a byte stream.
|
void |
parse(String systemId,
String publicId,
Reader reader)
Parse an XML document from a character stream.
|
void |
parse(String systemId,
String publicId,
String encoding)
Parse an XML document from a URI.
|
void |
setHandler(XmlHandler handler)
Set the handler that will receive parsing events.
|
public static final int CONTENT_UNDECLARED
public static final int CONTENT_ANY
public static final int CONTENT_EMPTY
public static final int CONTENT_MIXED
public static final int CONTENT_ELEMENTS
public static final int ENTITY_UNDECLARED
public static final int ENTITY_INTERNAL
public static final int ENTITY_NDATA
public static final int ENTITY_TEXT
public static final int ATTRIBUTE_UNDECLARED
public static final int ATTRIBUTE_CDATA
public static final int ATTRIBUTE_ID
public static final int ATTRIBUTE_IDREF
public static final int ATTRIBUTE_IDREFS
public static final int ATTRIBUTE_ENTITY
public static final int ATTRIBUTE_ENTITIES
public static final int ATTRIBUTE_NMTOKEN
public static final int ATTRIBUTE_NMTOKENS
public static final int ATTRIBUTE_ENUMERATED
public static final int ATTRIBUTE_NOTATION
public static final int ATTRIBUTE_DEFAULT_UNDECLARED
public static final int ATTRIBUTE_DEFAULT_SPECIFIED
public static final int ATTRIBUTE_DEFAULT_IMPLIED
public static final int ATTRIBUTE_DEFAULT_REQUIRED
public static final int ATTRIBUTE_DEFAULT_FIXED
public void setHandler(XmlHandler handler)
handler
- The handler to receive callback events.parse(java.lang.String, java.lang.String, java.lang.String)
,
XmlHandler
public void parse(String systemId, String publicId, String encoding) throws Exception
You may parse a document more than once, but only one thread may call this method for an object at one time.
systemId
- The URI of the document.publicId
- The public identifier of the document, or null.encoding
- The suggested encoding, or null if unknown.Exception
- Any exception thrown by your
own handlers, or any derivation of java.io.IOException
thrown by the parser itself.public void parse(String systemId, String publicId, InputStream stream, String encoding) throws Exception
The URI that you supply will become the base URI for resolving relative links, but Ælfred will actually read the document from the supplied input stream.
You may parse a document more than once, but only one thread may call this method for an object at one time.
systemId
- The base URI of the document, or null if not
known.publicId
- The public identifier of the document, or null
if not known.stream
- A byte input stream.encoding
- The suggested encoding, or null if unknown.Exception
- Any exception thrown by your
own handlers, or any derivation of java.io.IOException
thrown by the parser itself.public void parse(String systemId, String publicId, Reader reader) throws Exception
The URI that you supply will become the base URI for resolving relative links, but Ælfred will actually read the document from the supplied input stream.
You may parse a document more than once, but only one thread may call this method for an object at one time.
systemId
- The base URI of the document, or null if not
known.publicId
- The public identifier of the document, or null
if not known.reader
- A character stream.Exception
- Any exception thrown by your
own handlers, or any derivation of java.io.IOException
thrown by the parser itself.public String intern(String s)
Ælfred uses this method to create an internalised version
of all names and attribute values, so that it can test equality
with ==
instead of String.equals()
.
If you want to be able to test for equality in the same way, you can use this method to internalise your own strings first:
String PARA = handler.intern("PARA");
Note that this will not return the same results as String.intern().
s
- The string to internalise.intern(char[],int,int)
,
String.intern()
public String intern(char[] ch, int start, int length)
This is much more efficient than constructing a non-internalised string first, and then internalising it.
Note that this will not return the same results as String.intern().
ch
- an array of characters for building the string.start
- the starting position in the array.length
- the number of characters to place in the string.intern(String)
,
String.intern()
public Enumeration declaredElements()
The results will be valid only after the DTD (if any) has been parsed.
getElementContentType(java.lang.String)
,
getElementContentModel(java.lang.String)
public int getElementContentType(String name)
name
- The element type name.getElementContentModel(java.lang.String)
,
CONTENT_UNDECLARED
,
CONTENT_ANY
,
CONTENT_EMPTY
,
CONTENT_MIXED
,
CONTENT_ELEMENTS
public String getElementContentModel(String name)
The result will always be null unless the content type is CONTENT_ELEMENTS or CONTENT_MIXED.
name
- The element type name.getElementContentType(java.lang.String)
public Enumeration declaredAttributes(String elname)
elname
- The name of the element type.getAttributeType(java.lang.String, java.lang.String)
,
getAttributeEnumeration(java.lang.String, java.lang.String)
,
getAttributeDefaultValueType(java.lang.String, java.lang.String)
,
getAttributeDefaultValue(java.lang.String, java.lang.String)
,
getAttributeExpandedValue(java.lang.String, java.lang.String)
public int getAttributeType(String name, String aname)
name
- The name of the associated element.aname
- The name of the attribute.ATTRIBUTE_UNDECLARED
,
ATTRIBUTE_CDATA
,
ATTRIBUTE_ID
,
ATTRIBUTE_IDREF
,
ATTRIBUTE_IDREFS
,
ATTRIBUTE_ENTITY
,
ATTRIBUTE_ENTITIES
,
ATTRIBUTE_NMTOKEN
,
ATTRIBUTE_NMTOKENS
,
ATTRIBUTE_ENUMERATED
,
ATTRIBUTE_NOTATION
public String getAttributeEnumeration(String name, String aname)
name
- The name of the associated element.aname
- The name of the attribute.ATTRIBUTE_ENUMERATED
,
ATTRIBUTE_NOTATION
public String getAttributeDefaultValue(String name, String aname)
name
- The name of the associated element.aname
- The name of the attribute.getAttributeExpandedValue(java.lang.String, java.lang.String)
public String getAttributeExpandedValue(String name, String aname)
All general entities will be expanded.
name
- The name of the associated element.aname
- The name of the attribute.getAttributeDefaultValue(java.lang.String, java.lang.String)
public int getAttributeDefaultValueType(String name, String aname)
public Enumeration declaredEntities()
getEntityType(java.lang.String)
,
getEntityPublicId(java.lang.String)
,
getEntitySystemId(java.lang.String)
,
getEntityValue(java.lang.String)
,
getEntityNotationName(java.lang.String)
public int getEntityType(String ename)
ENTITY_UNDECLARED
,
ENTITY_INTERNAL
,
ENTITY_NDATA
,
ENTITY_TEXT
public String getEntityPublicId(String ename)
ename
- The name of the external entity.getEntityType(java.lang.String)
public String getEntitySystemId(String ename)
ename
- The name of the external entity.getEntityType(java.lang.String)
public String getEntityValue(String ename)
ename
- The name of the internal entity.getEntityType(java.lang.String)
public String getEntityNotationName(String eName)
ename
- The NDATA entity name.getEntityType(java.lang.String)
public Enumeration declaredNotations()
getNotationPublicId(java.lang.String)
,
getNotationSystemId(java.lang.String)
public String getNotationPublicId(String nname)
nname
- The name of the notation.getNotationSystemId(java.lang.String)
public String getNotationSystemId(String nname)
nname
- The name of the notation.getNotationPublicId(java.lang.String)
public int getLineNumber()
public int getColumnNumber()
Copyright © 2024 Central Laboratory of the Research Councils. All Rights Reserved.