public class XmlElement extends Object
| 限定符 | 构造器和说明 |
|---|---|
|
XmlElement()
Creates and initializes a new XML element.
|
|
XmlElement(boolean skipLeadingWhitespace)
Creates and initializes a new XML element.
|
|
XmlElement(Map<String,char[]> entities)
Creates and initializes a new XML element.
|
|
XmlElement(Map<String,char[]> entities,
boolean skipLeadingWhitespace)
Creates and initializes a new XML element.
|
protected |
XmlElement(Map<String,char[]> entities,
boolean skipLeadingWhitespace,
boolean fillBasicConversionTable)
Creates and initializes a new XML element.
|
| 限定符和类型 | 方法和说明 |
|---|---|
void |
addChild(XmlElement child)
Adds a child element.
|
protected boolean |
checkCDATA(StringBuilder buf)
Scans a special tag and if the tag is a CDATA section, append its
content to
buf. |
protected boolean |
checkLiteral(String literal)
Scans the data for literal text.
|
protected XmlElement |
createAnotherElement()
Creates a new similar XML element.
|
protected XmlParseException |
expectedInput(String charSet)
Creates a parse exception for when the next character read is not
the character that was expected.
|
Object |
getAttribute(String name)
Returns an attribute of the element.
|
Object |
getAttribute(String name,
Object defaultValue)
Returns an attribute of the element.
|
Set<String> |
getAttributeNames()
Get the attribute names.
|
boolean |
getBooleanAttribute(String name,
String trueValue,
String falseValue,
boolean defaultValue)
Returns an attribute of the element.
|
XmlElement |
getChild(int index) |
XmlElement |
getChild(String name) |
String |
getChildContent(String name) |
List<XmlElement> |
getChildren()
Returns the child elements as a List.
|
List<XmlElement> |
getChildren(String name) |
int |
getChildrenCount()
Returns the number of child elements of the element.
|
String |
getContent()
Returns the PCDATA content of the object.
|
double |
getDoubleAttribute(String name)
Returns an attribute of the element.
|
double |
getDoubleAttribute(String name,
double defaultValue)
Returns an attribute of the element.
|
int |
getIntAttribute(String name)
Returns an attribute of the element.
|
int |
getIntAttribute(String name,
int defaultValue)
Returns an attribute of the element.
|
int |
getLineNr()
Returns the line nr in the source data on which the element is found.
|
long |
getLongAttribute(String name)
Returns an attribute of the element.
|
long |
getLongAttribute(String name,
long defaultValue)
Returns an attribute of the element.
|
String |
getName()
Returns the name of the element.
|
XmlElement |
getParent()
Returns the parent of the element.
|
String |
getStringAttribute(String name)
Returns an attribute of the element.
|
String |
getStringAttribute(String name,
String defaultValue)
Returns an attribute of the element.
|
boolean |
hasAttribute(String name)
Returns true if this element contains a attribute for the specified
name.
|
protected XmlParseException |
invalidValue(String name,
String value)
Creates a parse exception for when an invalid value is given to a
method.
|
protected XmlParseException |
invalidValueSet(String name)
Creates a parse exception for when an invalid valueset is given to
a method.
|
boolean |
isIgnoreWhitespace() |
void |
parseCharArray(char[] input,
int offset,
int end)
Reads one XML element from a char array and parses it.
|
void |
parseCharArray(char[] input,
int offset,
int end,
int startingLineNr)
Reads one XML element from a char array and parses it.
|
void |
parseFromReader(Reader reader)
Reads one XML element from a java.io.Reader and parses it.
|
void |
parseFromReader(Reader reader,
int startingLineNr)
Reads one XML element from a java.io.Reader and parses it.
|
void |
parseString(String string)
Reads one XML element from a String and parses it.
|
void |
parseString(String string,
int offset)
Reads one XML element from a String and parses it.
|
void |
parseString(String string,
int offset,
int end)
Reads one XML element from a String and parses it.
|
void |
parseString(String string,
int offset,
int end,
int startingLineNr)
Reads one XML element from a String and parses it.
|
protected char |
readChar()
Reads a character from a reader.
|
void |
removeAttribute(String name)
Removes an attribute.
|
void |
removeChild(XmlElement child)
Removes a child element.
|
protected void |
resolveEntity(StringBuilder buf)
Resolves an entity.
|
protected void |
scanElement(XmlElement elt)
Scans an XML element.
|
protected void |
scanIdentifier(StringBuilder result)
Scans an identifier from the current reader.
|
protected boolean |
scanPCData(StringBuilder buf)
Scans a #PCDATA element.
|
protected void |
scanString(StringBuilder buf)
This method scans a delimited string from the current reader.
|
protected char |
scanWhitespace()
This method scans an identifier from the current reader.
|
protected char |
scanWhitespace(StringBuilder buf)
This method scans an identifier from the current reader.
|
void |
setAttribute(String name,
Object value)
Adds or modifies an attribute.
|
void |
setContent(String content)
Changes the content string.
|
void |
setDoubleAttribute(String name,
double value)
Adds or modifies an attribute.
|
void |
setIntAttribute(String name,
int value)
Adds or modifies an attribute.
|
void |
setLongAttribute(String name,
long value)
Adds or modifies an attribute.
|
void |
setName(String name)
Changes the name of the element.
|
protected void |
setParent(XmlElement parent) |
protected void |
skipComment()
Skips a comment.
|
protected void |
skipSpecialTag(int bracketLevel)
Skips a special tag or comment.
|
protected XmlParseException |
syntaxError(String context)
Creates a parse exception for when a syntax error occured.
|
String |
toCompactString() |
String |
toString()
Writes the XML element to a string.
|
protected XmlParseException |
unexpectedEndOfData()
Creates a parse exception for when the end of the data input has been
reached.
|
protected XmlParseException |
unknownEntity(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.
|
void |
write(Writer writer)
Writes the XML element to a writer.
|
void |
write(Writer writer,
boolean standalone,
int indent)
Writes the XML element to a writer.
|
void |
write(Writer writer,
int indent)
Writes the XML element to a writer.
|
protected static void |
write(Writer writer,
XmlElement element,
boolean standalone,
int indent,
int level)
Writes the XML element to a writer.
|
protected static void |
writeEncoded(Writer writer,
String str)
Writes a string encoded to a writer.
|
public XmlElement()
new XmlElement(new Hashtable(), false, true)
public XmlElement(Map<String,char[]> entities)
new XmlElement(entities, false, true)
entities - The entity conversion table.
entities != null
public XmlElement(boolean skipLeadingWhitespace)
new XmlElement(new Hashtable(), skipLeadingWhitespace, true)
skipLeadingWhitespace - true if leading and trailing whitespace in PCDATA
content has to be removed.
public XmlElement(Map<String,char[]> entities, boolean skipLeadingWhitespace)
new XmlElement(entities, skipLeadingWhitespace, true)
entities - The entity conversion table.skipLeadingWhitespace - true if leading and trailing whitespace in PCDATA
content has to be removed.
entities != null
protected XmlElement(Map<String,char[]> entities, boolean skipLeadingWhitespace, boolean fillBasicConversionTable)
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.
entities != null
fillBasicConversionTable == false
then entities contains at least the following
entries: amp, lt, gt,
apos and quot
public boolean isIgnoreWhitespace()
true if the leading and trailing whitespace of #PCDATA
sections have to be ignored.public void addChild(XmlElement child)
child - The child element to add.
child != null
child.getName() != null
child does not have a parent element
public void removeChild(XmlElement child)
child - The child element to remove.
child != null
child is a child element of the receiver
public String getName()
public void setName(String name)
name - The new name.
name != null
name is a valid XML identifier
public XmlElement getParent()
protected void setParent(XmlElement parent)
parent - public int getChildrenCount()
result >= 0
public XmlElement getChild(int index)
index - public XmlElement getChild(String name)
name - public List<XmlElement> getChildren()
result != null
public List<XmlElement> getChildren(String name)
name - public int getLineNr()
0 there is no associated source data.
result >= 0
public String getContent()
null is returned.public void setContent(String content)
content - The new content string.public void setAttribute(String name, Object value)
name - The name of the attribute.value - The value of the attribute.
name != null
name is a valid XML identifier
value != null
public void removeAttribute(String name)
name - The name of the attribute.
name != null
name is a valid XML identifier
null
public Set<String> getAttributeNames()
result != null
public boolean hasAttribute(String name)
name - public Object getAttribute(String name)
null is returned.name - The name of the attribute.
name != null
name is a valid XML identifier
public Object getAttribute(String name, Object defaultValue)
defaultValue is returned.name - The name of the attribute.defaultValue - Key to use if the attribute is missing.
name != null
name is a valid XML identifier
public int getIntAttribute(String name)
0 is returned.name - The name of the attribute.
name != null
name is a valid XML identifier
public int getIntAttribute(String name, int defaultValue)
defaultValue is returned.name - The name of the attribute.defaultValue - Key to use if the attribute is missing.
name != null
name is a valid XML identifier
public void setIntAttribute(String name, int value)
name - The name of the attribute.value - The value of the attribute.
name != null
name is a valid XML identifier
public long getLongAttribute(String name)
0 is returned.name - The name of the attribute.
name != null
name is a valid XML identifier
public long getLongAttribute(String name, long defaultValue)
defaultValue is returned.name - The name of the attribute.defaultValue - Key to use if the attribute is missing.
name != null
name is a valid XML identifier
public void setLongAttribute(String name, long value)
name - The name of the attribute.value - The value of the attribute.
name != null
name is a valid XML identifier
public String getStringAttribute(String name)
null is returned.name - The name of the attribute.
name != null
name is a valid XML identifier
public String getStringAttribute(String name, String defaultValue)
defaultValue is returned.name - The name of the attribute.defaultValue - Key to use if the attribute is missing.
name != null
name is a valid XML identifier
public double getDoubleAttribute(String name)
0.0 is returned.name - The name of the attribute.
name != null
name is a valid XML identifier
public double getDoubleAttribute(String name, double defaultValue)
defaultValue is returned.name - The name of the attribute.defaultValue - Key to use if the attribute is missing.
name != null
name is a valid XML identifier
public void setDoubleAttribute(String name, double value)
name - The name of the attribute.value - The value of the attribute.
name != null
name is a valid XML identifier
public boolean getBooleanAttribute(String name, String trueValue, String falseValue, boolean defaultValue)
defaultValue is returned.
If the value of the attribute is equal to trueValue,
true is returned.
If the value of the attribute is equal to falseValue,
false is returned.
If the value doesn't match trueValue or
falseValue, an exception is thrown.name - The name of the attribute.trueValue - The value associated with true.falseValue - The value associated with true.defaultValue - Value to use if the attribute is missing.
name != null
name is a valid XML identifier
trueValue and falseValue
are different strings.
public void parseFromReader(Reader reader) throws IOException, XmlParseException
reader - The reader from which to retrieve the XML data.
reader != null
reader is not closed
IOException - If an error occured while reading the input.XmlParseException - If an error occured while parsing the read data.public void parseFromReader(Reader reader, int startingLineNr) throws 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
IOException - If an error occured while reading the input.XmlParseException - If an error occured while parsing the read data.public void parseString(String string) throws XmlParseException
string - The reader from which to retrieve the XML data.
string != null
string.length() > 0
XmlParseException - If an error occured while parsing the string.public void parseString(String string, int offset) throws XmlParseException
string - The reader from which to retrieve the XML data.offset - The first character in string to scan.
string != null
offset < string.length()
offset >= 0
XmlParseException - If an error occured while parsing the string.public void parseString(String string, int offset, int end) throws XmlParseException
string - The reader from which to retrieve the XML data.offset - The first character in string to scan.end - The character where to stop scanning.
This character is not scanned.
string != null
end <= string.length()
offset < end
offset >= 0
XmlParseException - If an error occured while parsing the string.public void parseString(String string, int offset, int end, int startingLineNr) throws XmlParseException
string - The reader from which to retrieve the XML data.offset - The first character in string to scan.end - The character where to stop scanning.
This character is not scanned.startingLineNr - The line number of the first line in the data.
string != null
end <= string.length()
offset < end
offset >= 0
XmlParseException - If an error occured while parsing the string.public void parseCharArray(char[] input,
int offset,
int end)
throws XmlParseException
input - The reader from which to retrieve the XML data.offset - The first character in string to scan.end - The character where to stop scanning.
This character is not scanned.
input != null
end <= input.length
offset < end
offset >= 0
XmlParseException - If an error occured while parsing the string.public void parseCharArray(char[] input,
int offset,
int end,
int startingLineNr)
throws XmlParseException
input - The reader from which to retrieve the XML data.offset - The first character in string to scan.end - The character where to stop scanning.
This character is not scanned.startingLineNr - The line number of the first line in the data.
input != null
end <= input.length
offset < end
offset >= 0
XmlParseException - If an error occured while parsing the string.public void write(Writer writer) throws IOException
writer - The writer to write the XML data to.
writer != null
writer is not closed
IOException - If the data could not be written to the writer.toString()public void write(Writer writer, boolean standalone, int indent) throws IOException
writer - The writer to write the XML data to.standalone - If true then element on a separate line.indent - How many spaces to indent the element.
writer != null
writer is not closed
IOException - If the data could not be written to the writer.toString()public void write(Writer writer, int indent) throws IOException
writer - The writer to write the XML data to.indent - How many spaces to indent the element.
writer != null
writer is not closed
IOException - If the data could not be written to the writer.toString()protected static void write(Writer writer, XmlElement element, boolean standalone, int indent, int level) throws IOException
writer - The writer to write the XML data to.standalone - indent - How many spaces to indent the element.
writer != null
writer is not closed
IOException - If the data could not be written to the writer.toString()protected static void writeEncoded(Writer writer, String str) throws IOException
writer - The writer to write the XML data to.str - The string to write encoded.
writer != null
writer is not closed
str != null
IOExceptionpublic String toCompactString()
public String toString()
toString 在类中 Objectwrite(Writer)protected XmlElement createAnotherElement()
You should override this method when subclassing XmlElement.
protected void scanIdentifier(StringBuilder result) throws IOException
result.result - The buffer in which the scanned identifier will be put.
result != null
IOExceptionprotected char scanWhitespace()
throws IOException
IOExceptionprotected char scanWhitespace(StringBuilder buf) throws IOException
result.result != null
IOExceptionprotected void scanString(StringBuilder buf) throws IOException
string.
string != null
IOExceptionprotected boolean scanPCData(StringBuilder buf) throws IOException
data.
data != null
IOExceptionprotected boolean checkCDATA(StringBuilder buf) throws IOException
buf.
buf != null
IOExceptionprotected void skipComment()
throws IOException
IOExceptionprotected void skipSpecialTag(int bracketLevel)
throws IOException
bracketLevel - The number of open square brackets ([) that have
already been read.
bracketLevel >= 0
IOExceptionprotected boolean checkLiteral(String literal) throws IOException
literal - the literal to check.
literal != null
IOExceptionprotected char readChar()
throws IOException
IOExceptionprotected void scanElement(XmlElement elt) throws IOException
elt - The element that will contain the result.
elt != null
IOExceptionprotected void resolveEntity(StringBuilder buf) throws IOException
buf.buf - Where to put the entity value.
buf != null
IOExceptionprotected void unreadChar(char ch)
ch - The character to push back.
ch != '\0'
protected XmlParseException invalidValueSet(String name)
name - The name of the entity.
name != null
protected XmlParseException invalidValue(String name, String value)
name - The name of the entity.value - The value of the entity.
name != null
value != null
protected XmlParseException unexpectedEndOfData()
protected XmlParseException syntaxError(String context)
context - The context in which the error occured.
context != null
context.length() > 0
protected XmlParseException expectedInput(String charSet)
charSet - The set of characters (in human readable form) that was
expected.
charSet != null
charSet.length() > 0
protected XmlParseException unknownEntity(String name)
name - The name of the entity.
name != null
name.length() > 0
Copyright © 2020. All rights reserved.