public class Document extends Content
XMLReader returns an instance of Document when it successfully reads an XML text file. The Document provides in-memory Java access to all parts of the XML document, through the APIs of the different classes of node in the XML tree, such as Element, Text, Data and Comment. A Document may also be constructed in memory directly by Java programs and then written to an XML text file using XMLWriter.
Modifier and Type | Field and Description |
---|---|
private static int |
CONTENT_LIST_LENGTH
The default length of this Document's content list.
|
private java.util.List<Content> |
contentList
The list of this Document's contents, initialised by default to hold
up to eight content-nodes, including possible layout text.
|
private static Filter |
documentContent
A Filter accepting the legal kinds of node that may be supplied as
the contents of a Document.
|
Constructor and Description |
---|
Document()
Creates a default empty Document with the standard XML Declaration.
|
Document(Declaration declaration)
Creates a custom empty Document with the supplied XML Declaration.
|
Document(Declaration declaration,
Element root)
Creates a custom Document with the supplied root Element.
|
Document(Element root)
Creates a default Document with the supplied root Element.
|
Modifier and Type | Method and Description |
---|---|
Document |
addContent(Content node)
Adds a Content node to this Document.
|
Document |
clone()
Clones this Document node.
|
private Document |
deepCopy(Document other)
Secret method to deep copy a partly-cloned Document.
|
java.util.List<Content> |
getContents()
Returns all of this Document's contents, of any type.
|
Declaration |
getDeclaration()
Returns the XML declaration for this Document.
|
Doctype |
getDoctype()
Returns any Doctype associated with this Document.
|
java.lang.String |
getEncoding()
Returns the character set encoding of this Document, by default UTF-8.
|
Element |
getRootElement()
Returns the single root Element, the principal content of this Document.
|
int |
getType()
Returns the type of this Content.
|
java.lang.String |
getVersion()
Returns the XML version of this Document, by default 1.0.
|
boolean |
hasContents()
Reports whether this Document has any contents.
|
boolean |
isStandalone()
Reports whether this XML Document stands alone.
|
boolean |
isWellFormed()
Reports if this Document is well-formed.
|
Content |
removeContent(Content node)
Removes a Content node from this Document's immediate contents.
|
private void |
replaceUnique(Content node)
Secret method to replace or add a uniquely-occurring Content node in
this Document.
|
Document |
setDeclaration(Declaration declaration)
Sets the XML declaration associated with this Document.
|
Document |
setDoctype(Doctype doctype)
Sets the optional XML Doctype associated with this Document.
|
Document |
setEncoding(java.lang.String encoding)
Sets the character encoding associated with this Document.
|
protected void |
setParent(Content node)
Disallows adding this Document to any other Content node.
|
Document |
setRootElement(Element root)
Sets the root Element of this Document.
|
Document |
setVersion(java.lang.String version)
Sets the XML version associated with this Document.
|
addAttribute, addContent, descendants, descendants, detach, getAttributes, getComment, getContent, getContent, getContent, getContents, getDocument, getIdentifier, getParent, getText, iterator, iterator, removeContent, removeContents, toString
private static final int CONTENT_LIST_LENGTH
private static final Filter documentContent
private java.util.List<Content> contentList
public Document()
public Document(Declaration declaration) throws SemanticError
declaration
- the XML declaration.SemanticError
- if the header is not a valid XML declaration.public Document(Element root)
root
- the root Element.public Document(Declaration declaration, Element root) throws SemanticError
declaration
- the XML Declaration.root
- the root Element.SemanticError
- if the header is not a valid XML declaration.private void replaceUnique(Content node)
node
- the new uniquely-occurring Content node.private Document deepCopy(Document other)
other
- the Document to be deep copied.protected void setParent(Content node) throws SemanticError
setParent
in class Content
node
- the desired parent node, possibly null.SemanticError
- deliberately.public Document clone()
public int getType()
public Document addContent(Content node) throws SemanticError
addContent
in class Content
node
- a Content node.SemanticError
- if a Declaration is an invalid XML declaration,
or a root Element conflicts with the root specified by a Doctype
declaration.public Content removeContent(Content node)
removeContent
in class Content
node
- another Content node.public boolean hasContents()
hasContents
in class Content
public java.util.List<Content> getContents()
getContents
in class Content
public Declaration getDeclaration()
public Doctype getDoctype()
public Element getRootElement()
public java.lang.String getVersion()
getDeclaration().getVersion()
.public java.lang.String getEncoding()
getDeclaration().getEncoding()
.public boolean isWellFormed()
public boolean isStandalone()
getDeclaration().isStandalone()
.public Document setDeclaration(Declaration declaration) throws SemanticError
declaration
- the new XML declaration.SemanticError
- if an invalid XML declaration is supplied.public Document setDoctype(Doctype doctype)
doctype
- the Doctype node.SemanticError
- if a mismatch is detected between the expected
and actual name of the current root Element.public Document setRootElement(Element root) throws SemanticError
root
- the root Element.SemanticError
- if a mismatch is detected between the actual
and expected name of the supplied root Element.public Document setVersion(java.lang.String version)
getDeclaration().setVersion(version)
version
- the XML version, typically "1.0".public Document setEncoding(java.lang.String encoding)
getDeclaration().setEncoding(encoding)
.encoding
- the character encoding, for example "UTF-8".