public class XMLElement
extends java.lang.Object
XMLElement xml = new XMLElement();
FileReader reader = new FileReader("filename.xml");
xml.parseFromReader(reader);
enumerateAttributeNames
.
The attribute values can be retrieved using the method
getAttribute
.
The following example shows how to list the attributes of an element:
XMLElement element = ...;
Enumeration enum = element.enumerateAttributeNames();
while (enum.hasMoreElements()) {
String key = (String) enum.nextElement();
String value = (String) element.getAttribute(key);
System.out.println(key + " = " + value);
}
enumerateChildren
.
The number of child elements can be retrieved using
countChildren
.
<title>The Title</title>
you can retrieve that data using the method
getContent
.
createAnotherElement
which has to return a new copy of the receiver.
XMLParseException
Modifier | Constructor and Description |
---|---|
|
XMLElement()
Creates and initializes a new XML element.
|
protected |
XMLElement(java.util.Map<java.lang.String,char[]> entities,
boolean skipLeadingWhitespace,
boolean fillBasicConversionTable,
boolean ignoreCase)
Creates and initializes a new XML element.
|
Modifier and Type | Method and Description |
---|---|
void |
addChild(XMLElement child)
Adds a child element.
|
protected boolean |
checkCDATA(java.lang.StringBuffer buf)
Scans a special tag and if the tag is a CDATA section, append its
content to
buf . |
protected boolean |
checkLiteral(java.lang.String literal)
Scans the data for literal text.
|
int |
countChildren() |
protected XMLElement |
createAnotherElement()
Creates a new similar XML element.
|
java.util.Enumeration<java.lang.String> |
enumerateAttributeNames() |
java.util.Enumeration<XMLElement> |
enumerateChildren() |
protected XMLParseException |
expectedInput(java.lang.String charSet)
Creates a parse exception for when the next character read is not
the character that was expected.
|
protected XMLParseException |
expectedInput(java.lang.String charSet,
char ch)
Creates a parse exception for when the next character read is not
the character that was expected.
|
java.lang.Object |
getAttribute(java.lang.String name) |
java.lang.String |
getContent() |
int |
getLineNr() |
java.lang.String |
getName()
Returns the name of the element.
|
protected XMLParseException |
invalidValue(java.lang.String name,
java.lang.String value)
Creates a parse exception for when an invalid value is given to a
method.
|
protected XMLParseException |
invalidValueSet(java.lang.String name)
Creates a parse exception for when an invalid valueset is given to
a method.
|
boolean |
isBOM() |
void |
parseFromReader(java.io.Reader reader)
Reads one XML element from a
Reader and parses it. |
void |
parseFromReader(java.io.Reader reader,
int startingLineNr)
Reads one XML element from a java.io.Reader and parses it.
|
protected char |
readChar()
Reads a character from a reader.
|
protected void |
resolveEntity(java.lang.StringBuffer buf)
Resolves an entity.
|
void |
sanitizeInput(java.io.Reader isr,
java.io.OutputStream pout)
Reads an xml file and removes the comments, leaving only relevant
xml code.
|
protected void |
scanElement(XMLElement elt)
Scans an XML element.
|
protected void |
scanIdentifier(java.lang.StringBuffer result)
Scans an identifier from the current reader.
|
protected void |
scanPCData(java.lang.StringBuffer data)
Scans a
#PCDATA element. |
protected void |
scanString(java.lang.StringBuffer string)
This method scans a delimited string from the current reader.
|
protected char |
scanWhitespace(java.lang.StringBuffer result)
This method scans an identifier from the current reader.
|
void |
setAttribute(java.lang.String name,
java.lang.Object value)
Adds or modifies an attribute.
|
void |
setContent(java.lang.String content)
Changes the content string.
|
void |
setName(java.lang.String name)
Changes the name of the element.
|
protected void |
skipComment()
Skips a comment.
|
protected void |
skipSpecialTag(int bracketLevel)
Skips a special tag or comment.
|
protected XMLParseException |
syntaxError(java.lang.String context)
Creates a parse exception for when a syntax error occured.
|
protected XMLParseException |
unexpectedEndOfData()
Creates a parse exception for when the end of the data input has been
reached.
|
protected XMLParseException |
unknownEntity(java.lang.String name)
Creates a parse exception for when an entity could not be resolved.
|
protected void |
unreadChar(char ch)
Pushes a character back to the read-back buffer.
|
public XMLElement()
Calling the construction is equivalent to:
new XMLElement(new HashMap(), false, true)
protected XMLElement(java.util.Map<java.lang.String,char[]> entities, boolean skipLeadingWhitespace, boolean fillBasicConversionTable, boolean ignoreCase)
This constructor should only be called from
createAnotherElement()
to create child elements.
entities
- The entity conversion table.skipLeadingWhitespace
- true
if leading and trailing whitespace in PCDATA
content has to be removed.fillBasicConversionTable
- true
if the basic entities need to be added to
the entity list (client code calling this constructor).ignoreCase
- true
if the case of element and attribute names have
to be ignored.
entities != null
fillBasicConversionTable == false
then entities
contains at least the following
entries: amp
, lt
, gt
, apos
and
quot
public void addChild(XMLElement child)
child
- The child element to add.
child != null
child.getName() != null
child
does not have a parent elementpublic void setAttribute(java.lang.String name, java.lang.Object value)
name
- The name of the attribute.value
- The value of the attribute.
name != null
name
is a valid XML identifiervalue != null
public int countChildren()
result >= 0
public java.util.Enumeration<java.lang.String> enumerateAttributeNames()
result != null
public java.util.Enumeration<XMLElement> enumerateChildren()
result != null
public java.lang.String getContent()
null
is returned.public int getLineNr()
0
there is no associated source data.
result >= 0
public java.lang.Object getAttribute(java.lang.String name)
name
- The name of the attribute.
name != null
name
is a valid XML identifier
If the attribute doesn't exist, null
is returned.
public java.lang.String getName()
XMLElement
object's namepublic void parseFromReader(java.io.Reader reader) throws java.io.IOException, XMLParseException
Reader
and parses it.reader
- The reader from which to retrieve the XML data.
reader != null
reader
is not closed'>'
character of the XML elementjava.io.IOException
- If an error occured while reading the input.XMLParseException
- If an error occured while parsing the read data.public void parseFromReader(java.io.Reader reader, int startingLineNr) throws java.io.IOException, XMLParseException
reader
- The reader from which to retrieve the XML data.startingLineNr
- The line number of the first line in the data.
reader != null
reader
is not closed'>'
character of the XML elementjava.io.IOException
- If an error occured while reading the input.XMLParseException
- If an error occured while parsing the read data.protected XMLElement createAnotherElement()
You should override this method when subclassing XMLElement.
public void setContent(java.lang.String content)
content
- The new content string.public void setName(java.lang.String name)
name
- The new name.
name != null
name
is a valid XML identifierprotected void scanIdentifier(java.lang.StringBuffer result) throws java.io.IOException
result
.result
- The buffer in which the scanned identifier will be put.
result != null
java.io.IOException
- if something goes wrongprotected char scanWhitespace(java.lang.StringBuffer result) throws java.io.IOException
The scanned whitespace is appended to result
.
result
- where to append scanned textresult != null
java.io.IOException
- if something goes wrongprotected void scanString(java.lang.StringBuffer string) throws java.io.IOException
The scanned string without delimiters is appended to string
.
string != null
string
- where to append the resultjava.io.IOException
- if something goes wrongprotected void scanPCData(java.lang.StringBuffer data) throws java.io.IOException
#PCDATA
element. CDATA sections and entities are
resolved.
The next < char is skipped.
The scanned data is appended to data
.
data != null
data
- where to append datajava.io.IOException
- if something goes wrongprotected boolean checkCDATA(java.lang.StringBuffer buf) throws java.io.IOException
buf
.
buf != null
buf
- buffer where to append datajava.io.IOException
- if something goes wrongprotected void skipComment() throws java.io.IOException
java.io.IOException
- if something goes wrongprotected void skipSpecialTag(int bracketLevel) throws java.io.IOException
bracketLevel
- The number of open square brackets ([) that have
already been read.
bracketLevel >= 0
java.io.IOException
- if something goes wrongprotected boolean checkLiteral(java.lang.String literal) throws java.io.IOException
Scanning stops when a character does not match or after the complete text has been checked, whichever comes first.
literal
- the literal to check.
literal != null
java.io.IOException
- if something goes wrongprotected char readChar() throws java.io.IOException
java.io.IOException
- if something goes wrongprotected void scanElement(XMLElement elt) throws java.io.IOException
elt
- The element that will contain the result.
elt != null
java.io.IOException
- if something goes wrongprotected void resolveEntity(java.lang.StringBuffer buf) throws java.io.IOException
The value of the entity is appended to buf
.
buf
- Where to put the entity value.
buf != null
java.io.IOException
- if something goes wrongprotected void unreadChar(char ch)
ch
- The character to push back.
ch != '\0'
protected XMLParseException invalidValueSet(java.lang.String name)
name
- The name of the entity.
name != null
protected XMLParseException invalidValue(java.lang.String name, java.lang.String value)
name
- The name of the entity.value
- The value of the entity.
name != null
value != null
protected XMLParseException unexpectedEndOfData()
protected XMLParseException syntaxError(java.lang.String context)
context
- The context in which the error occured.
context != null
context.length() > 0
protected XMLParseException expectedInput(java.lang.String charSet)
charSet
- The set of characters (in human readable form) that was
expected.
charSet != null
charSet.length() > 0
protected XMLParseException expectedInput(java.lang.String charSet, char ch)
charSet
- The set of characters (in human readable form) that was
expected.ch
- The character that was received instead.
charSet != null
charSet.length() > 0
protected XMLParseException unknownEntity(java.lang.String name)
name
- The name of the entity.name != null
name.length() > 0
public void sanitizeInput(java.io.Reader isr, java.io.OutputStream pout)
isr
- The reader of the InputStream
containing the xml.pout
- The PipedOutputStream
that will be receiving the
filtered xml file.public boolean isBOM()