类 XmlElement
java.lang.Object
net.apexes.commons.xml.XmlElement
- 直接已知子类:
Xml
XmlElement is a representation of an XML object. The object is able to parse
XML code.
- 版本:
- $Name: RELEASE_2_2_1 $, $Revision: 1.5 $
- 作者:
- Marc De Scheemaecker <cyberelf@mac.com>
-
构造器概要
构造器限定符构造器说明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.protectedXmlElement(Map<String, char[]> entities, boolean skipLeadingWhitespace, boolean fillBasicConversionTable) Creates and initializes a new XML element. -
方法概要
修饰符和类型方法说明voidaddChild(XmlElement child) Adds a child element.protected booleancheckCDATA(StringBuilder buf) Scans a special tag and if the tag is a CDATA section, append its content tobuf.protected booleancheckLiteral(String literal) Scans the data for literal text.protected XmlElementCreates a new similar XML element.protected XmlParseExceptionexpectedInput(String charSet) Creates a parse exception for when the next character read is not the character that was expected.getAttribute(String name) Returns an attribute of the element.getAttribute(String name, Object defaultValue) Returns an attribute of the element.Get the attribute names.booleangetBooleanAttribute(String name, String trueValue, String falseValue, boolean defaultValue) Returns an attribute of the element.getChild(int index) getChildContent(String name) Returns the child elements as a List.getChildren(String name) intReturns the number of child elements of the element.Returns the PCDATA content of the object.doublegetDoubleAttribute(String name) Returns an attribute of the element.doublegetDoubleAttribute(String name, double defaultValue) Returns an attribute of the element.intgetIntAttribute(String name) Returns an attribute of the element.intgetIntAttribute(String name, int defaultValue) Returns an attribute of the element.intReturns the line nr in the source data on which the element is found.longgetLongAttribute(String name) Returns an attribute of the element.longgetLongAttribute(String name, long defaultValue) Returns an attribute of the element.getName()Returns the name of the element.Returns the parent of the element.getStringAttribute(String name) Returns an attribute of the element.getStringAttribute(String name, String defaultValue) Returns an attribute of the element.booleanhasAttribute(String name) Returns true if this element contains a attribute for the specified name.protected XmlParseExceptioninvalidValue(String name, String value) Creates a parse exception for when an invalid value is given to a method.protected XmlParseExceptioninvalidValueSet(String name) Creates a parse exception for when an invalid valueset is given to a method.booleanvoidparseCharArray(char[] input, int offset, int end) Reads one XML element from a char array and parses it.voidparseCharArray(char[] input, int offset, int end, int startingLineNr) Reads one XML element from a char array and parses it.voidparseFromReader(Reader reader) Reads one XML element from a java.io.Reader and parses it.voidparseFromReader(Reader reader, int startingLineNr) Reads one XML element from a java.io.Reader and parses it.voidparseString(String string) Reads one XML element from a String and parses it.voidparseString(String string, int offset) Reads one XML element from a String and parses it.voidparseString(String string, int offset, int end) Reads one XML element from a String and parses it.voidparseString(String string, int offset, int end, int startingLineNr) Reads one XML element from a String and parses it.protected charreadChar()Reads a character from a reader.voidremoveAttribute(String name) Removes an attribute.voidremoveChild(XmlElement child) Removes a child element.protected voidResolves an entity.protected voidscanElement(XmlElement elt) Scans an XML element.protected voidscanIdentifier(StringBuilder result) Scans an identifier from the current reader.protected booleanscanPCData(StringBuilder buf) Scans a #PCDATA element.protected voidscanString(StringBuilder buf) This method scans a delimited string from the current reader.protected charThis method scans an identifier from the current reader.protected charThis method scans an identifier from the current reader.voidsetAttribute(String name, Object value) Adds or modifies an attribute.voidsetContent(String content) Changes the content string.voidsetDoubleAttribute(String name, double value) Adds or modifies an attribute.voidsetIntAttribute(String name, int value) Adds or modifies an attribute.voidsetLongAttribute(String name, long value) Adds or modifies an attribute.voidChanges the name of the element.protected voidsetParent(XmlElement parent) protected voidSkips a comment.protected voidskipSpecialTag(int bracketLevel) Skips a special tag or comment.protected XmlParseExceptionsyntaxError(String context) Creates a parse exception for when a syntax error occured.toString()Writes the XML element to a string.protected XmlParseExceptionCreates a parse exception for when the end of the data input has been reached.protected XmlParseExceptionunknownEntity(String name) Creates a parse exception for when an entity could not be resolved.protected voidunreadChar(char ch) Pushes a character back to the read-back buffer.voidWrites the XML element to a writer.voidWrites the XML element to a writer.voidWrites the XML element to a writer.protected static voidwrite(Writer writer, XmlElement element, boolean standalone, int indent, int level) Writes the XML element to a writer.protected static voidwriteEncoded(Writer writer, String str) Writes a string encoded to a writer.
-
构造器详细资料
-
XmlElement
public XmlElement()Creates and initializes a new XML element. Calling the construction is equivalent to:new XmlElement(new Hashtable(), false, true)- Postconditions:
-
- countChildren() => 0
- enumerateChildren() => empty enumeration
- enumeratePropertyNames() => empty enumeration
- getChildren() => empty vector
- getContent() => ""
- getLineNr() => 0
- getName() => null
-
XmlElement
Creates and initializes a new XML element. Calling the construction is equivalent to:new XmlElement(entities, false, true)- 参数:
entities- The entity conversion table.
- Preconditions:
-
entities != null
- Postconditions:
-
- countChildren() => 0
- enumerateChildren() => empty enumeration
- enumeratePropertyNames() => empty enumeration
- getChildren() => empty vector
- getContent() => ""
- getLineNr() => 0
- getName() => null
-
XmlElement
public XmlElement(boolean skipLeadingWhitespace) Creates and initializes a new XML element. Calling the construction is equivalent to:new XmlElement(new Hashtable(), skipLeadingWhitespace, true)- 参数:
skipLeadingWhitespace-trueif leading and trailing whitespace in PCDATA content has to be removed.
- Postconditions:
-
- countChildren() => 0
- enumerateChildren() => empty enumeration
- enumeratePropertyNames() => empty enumeration
- getChildren() => empty vector
- getContent() => ""
- getLineNr() => 0
- getName() => null
-
XmlElement
Creates and initializes a new XML element. Calling the construction is equivalent to:new XmlElement(entities, skipLeadingWhitespace, true)- 参数:
entities- The entity conversion table.skipLeadingWhitespace-trueif leading and trailing whitespace in PCDATA content has to be removed.
- Preconditions:
-
entities != null
- Postconditions:
-
- countChildren() => 0
- enumerateChildren() => empty enumeration
- enumeratePropertyNames() => empty enumeration
- getChildren() => empty vector
- getContent() => ""
- getLineNr() => 0
- getName() => null
-
XmlElement
protected XmlElement(Map<String, char[]> entities, boolean skipLeadingWhitespace, boolean fillBasicConversionTable) Creates and initializes a new XML element.This constructor should only be called from
createAnotherElementto create child elements.- 参数:
entities- The entity conversion table.skipLeadingWhitespace-trueif leading and trailing whitespace in PCDATA content has to be removed.fillBasicConversionTable-trueif the basic entities need to be added to the entity list.
- Preconditions:
-
entities != null- if
fillBasicConversionTable == falsethenentitiescontains at least the following entries:amp,lt,gt,aposandquot
- Postconditions:
-
- countChildren() => 0
- enumerateChildren() => empty enumeration
- enumeratePropertyNames() => empty enumeration
- getChildren() => empty vector
- getContent() => ""
- getLineNr() => 0
- getName() => null
-
-
方法详细资料
-
isIgnoreWhitespace
public boolean isIgnoreWhitespace()- 返回:
trueif the leading and trailing whitespace of #PCDATA sections have to be ignored.
-
addChild
Adds a child element.- 参数:
child- The child element to add.
- Preconditions:
-
child != nullchild.getName() != nullchilddoes not have a parent element
- Postconditions:
-
- countChildren() => old.countChildren() + 1
- enumerateChildren() => old.enumerateChildren() + child
- getChildren() => old.enumerateChildren() + child
-
removeChild
Removes a child element.- 参数:
child- The child element to remove.
- Preconditions:
-
child != nullchildis a child element of the receiver
- Postconditions:
-
- countChildren() => old.countChildren() - 1
- enumerateChildren() => old.enumerateChildren() - child
- getChildren() => old.enumerateChildren() - child
-
getName
Returns the name of the element. -
setName
Changes the name of the element.- 参数:
name- The new name.
- Preconditions:
-
name != nullnameis a valid XML identifier
-
getParent
Returns the parent of the element.- 返回:
-
setParent
- 参数:
parent-
-
getChildrenCount
public int getChildrenCount()Returns the number of child elements of the element.- Postconditions:
-
result >= 0
-
getChild
- 参数:
index-- 返回:
-
getChild
- 参数:
name-- 返回:
-
getChildContent
- 参数:
name-- 返回:
-
getChildren
Returns the child elements as a List.- Postconditions:
-
result != null
-
getChildren
- 参数:
name-- 返回:
-
getLineNr
public int getLineNr()Returns the line nr in the source data on which the element is found. This method returns0there is no associated source data.- Postconditions:
-
result >= 0
-
getContent
Returns the PCDATA content of the object. If there is no such content,nullis returned. -
setContent
Changes the content string.- 参数:
content- The new content string.
-
setAttribute
Adds or modifies an attribute.- 参数:
name- The name of the attribute.value- The value of the attribute.
- Preconditions:
-
name != nullnameis a valid XML identifiervalue != null
- Postconditions:
-
- enumerateAttributeNames() => old.enumerateAttributeNames() + name
- getAttribute(name) => value
-
removeAttribute
Removes an attribute.- 参数:
name- The name of the attribute.
- Preconditions:
-
name != nullnameis a valid XML identifier
- Postconditions:
-
- enumerateAttributeNames() => old.enumerateAttributeNames() - name
- getAttribute(name) =>
null
-
getAttributeNames
Get the attribute names.- Postconditions:
-
result != null
-
hasAttribute
Returns true if this element contains a attribute for the specified name.- 参数:
name-- 返回:
-
getAttribute
Returns an attribute of the element. If the attribute doesn't exist,nullis returned.- 参数:
name- The name of the attribute.
- Preconditions:
-
name != nullnameis a valid XML identifier
-
getAttribute
Returns an attribute of the element. If the attribute doesn't exist,defaultValueis returned.- 参数:
name- The name of the attribute.defaultValue- Key to use if the attribute is missing.
- Preconditions:
-
name != nullnameis a valid XML identifier
-
getIntAttribute
Returns an attribute of the element. If the attribute doesn't exist,0is returned.- 参数:
name- The name of the attribute.
- Preconditions:
-
name != nullnameis a valid XML identifier
-
getIntAttribute
Returns an attribute of the element. If the attribute doesn't exist,defaultValueis returned.- 参数:
name- The name of the attribute.defaultValue- Key to use if the attribute is missing.
- Preconditions:
-
name != nullnameis a valid XML identifier
-
setIntAttribute
Adds or modifies an attribute.- 参数:
name- The name of the attribute.value- The value of the attribute.
- Preconditions:
-
name != nullnameis a valid XML identifier
- Postconditions:
-
- enumerateAttributeNames() => old.enumerateAttributeNames() + name
- getIntAttribute(name) => value
-
getLongAttribute
Returns an attribute of the element. If the attribute doesn't exist,0is returned.- 参数:
name- The name of the attribute.
- Preconditions:
-
name != nullnameis a valid XML identifier
-
getLongAttribute
Returns an attribute of the element. If the attribute doesn't exist,defaultValueis returned.- 参数:
name- The name of the attribute.defaultValue- Key to use if the attribute is missing.
- Preconditions:
-
name != nullnameis a valid XML identifier
-
setLongAttribute
Adds or modifies an attribute.- 参数:
name- The name of the attribute.value- The value of the attribute.
- Preconditions:
-
name != nullnameis a valid XML identifier
- Postconditions:
-
- enumerateAttributeNames() => old.enumerateAttributeNames() + name
- getIntAttribute(name) => value
-
getStringAttribute
Returns an attribute of the element. If the attribute doesn't exist,nullis returned.- 参数:
name- The name of the attribute.
- Preconditions:
-
name != nullnameis a valid XML identifier
-
getStringAttribute
Returns an attribute of the element. If the attribute doesn't exist,defaultValueis returned.- 参数:
name- The name of the attribute.defaultValue- Key to use if the attribute is missing.
- Preconditions:
-
name != nullnameis a valid XML identifier
-
getDoubleAttribute
Returns an attribute of the element. If the attribute doesn't exist,0.0is returned.- 参数:
name- The name of the attribute.
- Preconditions:
-
name != nullnameis a valid XML identifier
-
getDoubleAttribute
Returns an attribute of the element. If the attribute doesn't exist,defaultValueis returned.- 参数:
name- The name of the attribute.defaultValue- Key to use if the attribute is missing.
- Preconditions:
-
name != nullnameis a valid XML identifier
-
setDoubleAttribute
Adds or modifies an attribute.- 参数:
name- The name of the attribute.value- The value of the attribute.
- Preconditions:
-
name != nullnameis a valid XML identifier
- Postconditions:
-
- enumerateAttributeNames() => old.enumerateAttributeNames() + name
- getDoubleAttribute(name) => value
-
getBooleanAttribute
public boolean getBooleanAttribute(String name, String trueValue, String falseValue, boolean defaultValue) Returns an attribute of the element. If the attribute doesn't exist,defaultValueis returned. If the value of the attribute is equal totrueValue,trueis returned. If the value of the attribute is equal tofalseValue,falseis returned. If the value doesn't matchtrueValueorfalseValue, an exception is thrown.- 参数:
name- The name of the attribute.trueValue- The value associated withtrue.falseValue- The value associated withtrue.defaultValue- Value to use if the attribute is missing.
- Preconditions:
-
name != nullnameis a valid XML identifiertrueValueandfalseValueare different strings.
-
parseFromReader
Reads one XML element from a java.io.Reader and parses it.- 参数:
reader- The reader from which to retrieve the XML data.
- Preconditions:
-
reader != nullreaderis not closed
- Postconditions:
-
- the state of the receiver is updated to reflect the XML element parsed from the reader
- the reader points to the first character following the last '>' character of the XML element
- 抛出:
IOException- If an error occured while reading the input.XmlParseException- If an error occured while parsing the read data.
-
parseFromReader
public void parseFromReader(Reader reader, int startingLineNr) throws IOException, XmlParseException Reads one XML element from a java.io.Reader and parses it.- 参数:
reader- The reader from which to retrieve the XML data.startingLineNr- The line number of the first line in the data.
- Preconditions:
-
reader != nullreaderis not closed
- Postconditions:
-
- the state of the receiver is updated to reflect the XML element parsed from the reader
- the reader points to the first character following the last '>' character of the XML element
- 抛出:
IOException- If an error occured while reading the input.XmlParseException- If an error occured while parsing the read data.
-
parseString
Reads one XML element from a String and parses it.- 参数:
string- The reader from which to retrieve the XML data.
- Preconditions:
-
string != nullstring.length() > 0
- Postconditions:
-
- the state of the receiver is updated to reflect the XML element parsed from the reader
- 抛出:
XmlParseException- If an error occured while parsing the string.
-
parseString
Reads one XML element from a String and parses it.- 参数:
string- The reader from which to retrieve the XML data.offset- The first character instringto scan.
- Preconditions:
-
string != nulloffset < string.length()offset >= 0
- Postconditions:
-
- the state of the receiver is updated to reflect the XML element parsed from the reader
- 抛出:
XmlParseException- If an error occured while parsing the string.
-
parseString
Reads one XML element from a String and parses it.- 参数:
string- The reader from which to retrieve the XML data.offset- The first character instringto scan.end- The character where to stop scanning. This character is not scanned.
- Preconditions:
-
string != nullend <= string.length()offset < endoffset >= 0
- Postconditions:
-
- the state of the receiver is updated to reflect the XML element parsed from the reader
- 抛出:
XmlParseException- If an error occured while parsing the string.
-
parseString
public void parseString(String string, int offset, int end, int startingLineNr) throws XmlParseException Reads one XML element from a String and parses it.- 参数:
string- The reader from which to retrieve the XML data.offset- The first character instringto scan.end- The character where to stop scanning. This character is not scanned.startingLineNr- The line number of the first line in the data.
- Preconditions:
-
string != nullend <= string.length()offset < endoffset >= 0
- Postconditions:
-
- the state of the receiver is updated to reflect the XML element parsed from the reader
- 抛出:
XmlParseException- If an error occured while parsing the string.
-
parseCharArray
Reads one XML element from a char array and parses it.- 参数:
input- The reader from which to retrieve the XML data.offset- The first character instringto scan.end- The character where to stop scanning. This character is not scanned.
- Preconditions:
-
input != nullend <= input.lengthoffset < endoffset >= 0
- Postconditions:
-
- the state of the receiver is updated to reflect the XML element parsed from the reader
- 抛出:
XmlParseException- If an error occured while parsing the string.
-
parseCharArray
public void parseCharArray(char[] input, int offset, int end, int startingLineNr) throws XmlParseException Reads one XML element from a char array and parses it.- 参数:
input- The reader from which to retrieve the XML data.offset- The first character instringto scan.end- The character where to stop scanning. This character is not scanned.startingLineNr- The line number of the first line in the data.
- Preconditions:
-
input != nullend <= input.lengthoffset < endoffset >= 0
- Postconditions:
-
- the state of the receiver is updated to reflect the XML element parsed from the reader
- 抛出:
XmlParseException- If an error occured while parsing the string.
-
write
Writes the XML element to a writer.- 参数:
writer- The writer to write the XML data to.
- Preconditions:
-
writer != nullwriteris not closed
- 抛出:
IOException- If the data could not be written to the writer.- 另请参阅:
-
write
Writes the XML element to a writer.- 参数:
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.
- Preconditions:
-
writer != nullwriteris not closed
- 抛出:
IOException- If the data could not be written to the writer.- 另请参阅:
-
write
Writes the XML element to a writer.- 参数:
writer- The writer to write the XML data to.indent- How many spaces to indent the element.
- Preconditions:
-
writer != nullwriteris not closed
- 抛出:
IOException- If the data could not be written to the writer.- 另请参阅:
-
write
protected static void write(Writer writer, XmlElement element, boolean standalone, int indent, int level) throws IOException Writes the XML element to a writer.- 参数:
writer- The writer to write the XML data to.standalone-indent- How many spaces to indent the element.
- Preconditions:
-
writer != nullwriteris not closed
- 抛出:
IOException- If the data could not be written to the writer.- 另请参阅:
-
writeEncoded
Writes a string encoded to a writer.- 参数:
writer- The writer to write the XML data to.str- The string to write encoded.
- Preconditions:
-
writer != nullwriteris not closedstr != null
- 抛出:
IOException -
toCompactString
-
toString
Writes the XML element to a string. -
createAnotherElement
Creates a new similar XML element.You should override this method when subclassing XmlElement.
-
scanIdentifier
Scans an identifier from the current reader. The scanned identifier is appended toresult.- 参数:
result- The buffer in which the scanned identifier will be put.
- Preconditions:
-
result != null- The next character read from the reader is a valid first character of an XML identifier.
- Postconditions:
-
- The next character read from the reader won't be an identifier character.
- 抛出:
IOException
-
scanWhitespace
This method scans an identifier from the current reader.- 返回:
- the next character following the whitespace.
- 抛出:
IOException
-
scanWhitespace
This method scans an identifier from the current reader. The scanned whitespace is appended toresult.- 返回:
- the next character following the whitespace.
- Preconditions:
-
result != null
- 抛出:
IOException -
scanString
This method scans a delimited string from the current reader. The scanned string without delimiters is appended tostring.- Preconditions:
-
string != null- the next char read is the string delimiter
- 抛出:
IOException
-
scanPCData
Scans a #PCDATA element. CDATA sections and entities are resolved. The next < char is skipped. The scanned data is appended todata.- Preconditions:
-
data != null
- 抛出:
IOException
-
checkCDATA
Scans a special tag and if the tag is a CDATA section, append its content tobuf.- Preconditions:
-
buf != null- The first < has already been read.
- 抛出:
IOException
-
skipComment
Skips a comment.- Preconditions:
-
- The first <!-- has already been read.
- 抛出:
IOException
-
skipSpecialTag
Skips a special tag or comment.- 参数:
bracketLevel- The number of open square brackets ([) that have already been read.
- Preconditions:
-
- The first <! has already been read.
bracketLevel >= 0
- 抛出:
IOException -
checkLiteral
Scans the data for literal text. Scanning stops when a character does not match or after the complete text has been checked, whichever comes first.- 参数:
literal- the literal to check.
- Preconditions:
-
literal != null
- 抛出:
IOException -
readChar
Reads a character from a reader.- 抛出:
IOException
-
scanElement
Scans an XML element.- 参数:
elt- The element that will contain the result.
- Preconditions:
-
- The first < has already been read.
elt != null
- 抛出:
IOException -
resolveEntity
Resolves an entity. The name of the entity is read from the reader. The value of the entity is appended tobuf.- 参数:
buf- Where to put the entity value.
- Preconditions:
-
- The first & has already been read.
buf != null
- 抛出:
IOException -
unreadChar
protected void unreadChar(char ch) Pushes a character back to the read-back buffer.- 参数:
ch- The character to push back.
- Preconditions:
-
- The read-back buffer is empty.
ch != '\0'
-
invalidValueSet
Creates a parse exception for when an invalid valueset is given to a method.- 参数:
name- The name of the entity.
- Preconditions:
-
name != null
-
invalidValue
Creates a parse exception for when an invalid value is given to a method.- 参数:
name- The name of the entity.value- The value of the entity.
- Preconditions:
-
name != nullvalue != null
-
unexpectedEndOfData
Creates a parse exception for when the end of the data input has been reached. -
syntaxError
Creates a parse exception for when a syntax error occured.- 参数:
context- The context in which the error occured.
- Preconditions:
-
context != nullcontext.length() > 0
-
expectedInput
Creates a parse exception for when the next character read is not the character that was expected.- 参数:
charSet- The set of characters (in human readable form) that was expected.
- Preconditions:
-
charSet != nullcharSet.length() > 0
-
unknownEntity
Creates a parse exception for when an entity could not be resolved.- 参数:
name- The name of the entity.
- Preconditions:
-
name != nullname.length() > 0
-