Top Description Fields Constructors Methods
org.jdom

public Class Document

extends Object
implements Parent
Class Inheritance
All Implemented Interfaces
org.jdom.Parent, java.io.Serializable, java.lang.Cloneable
Imports
java.util.*, org.jdom.filter.*

An XML document. Methods allow access to the root element as well as the DocType and other document-level information.
Authors
Brett McLaughlin, Jason Hunter, Jools Enticknap, Bradley S. Huffman
Version
$Revision: 1.85 $, $Date: 2007/11/10 05:28:58 $

Field Summary

Modifier and TypeField and Description
protected String
baseURI

See http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/core.html#baseURIs-Considerations

pack-priv ContentList
content

This document's content including comments, PIs, a possible DocType, and a root element.

private static final String
private HashMap<K, V>

Constructor Summary

AccessConstructor and Description
public
Document()

Creates a new empty document.

public
Document(Element
Element for document root.
rootElement
,
DocType
DocType declaration.
docType
,
String
the URI from which this doucment was loaded.
baseURI
)

This will create a new Document, with the supplied Element as the root element, the supplied DocType declaration, and the specified base URI.

public
Document(Element
Element for document root.
rootElement
,
DocType
DocType declaration.
docType
)

This will create a new Document, with the supplied Element as the root element and the supplied DocType declaration.

public
Document(Element
Element for document root
rootElement
)

This will create a new Document, with the supplied Element as the root element, and no DocType declaration.

public
Document(List<E>
List of starter content
content
)

This will create a new Document, with the supplied list of content, and a DocType declaration only if the content contains a DocType instance.

Method Summary

Modifier and TypeMethod and Description
public Document

Returns:

the document on which the method was called
addContent
(Content
child to append to end of content list
child
)

Appends the child to the end of the content list.

public Document

Returns:

the document on which the method was called
addContent
(Collection<E>
collection to append
c
)

Appends all children in the given collection to the end of the content list.

public Document

Returns:

the parent on which the method was called
addContent
(int
location for adding the collection
index
,
Content
child to insert
child
)

Inserts the child into the content list at the given index.

public Document

Returns:

the parent on which the method was called
addContent
(int
location for adding the collection
index
,
Collection<E>
collection to insert
c
)

Inserts the content in a collection into the content list at the given index.

public Object

Returns:

Object clone of this Document
clone
()

Overrides java.lang.Object.clone.

Implements org.jdom.Parent.clone.

This will return a deep clone of this Document.
public List<E>
cloneContent()

Implements org.jdom.Parent.cloneContent.

Returns a list containing detached clones of this parent's content list.
public Element

Returns:

removed root Element
detachRootElement
()

Detach the root Element from this document.

public final boolean

Returns:

boolean whether the Document is equal to the supplied Object
equals
(Object
Object to compare to
ob
)

Overrides java.lang.Object.equals.

This tests for equality of this Document to the supplied Object.
public final String

Returns:

the base URI of this document
getBaseURI
()

Returns the URI from which this document was loaded, or null if this is not known.

public Content
getContent(int
location of desired child
index
)

Implements org.jdom.Parent.getContent.

Returns the child at the given index.
public List<E>

Returns:

List - all Document content
getContent
()

Implements org.jdom.Parent.getContent.

This will return all content for the Document.
public List<E>

Returns:

List - filtered Document content
getContent
(Filter
Filter to apply
filter
)

Implements org.jdom.Parent.getContent.

Return a filtered view of this Document's content.
public int
getContentSize()

Implements org.jdom.Parent.getContentSize.

Returns the number of children in this parent's content list.
public Iterator<E>

Returns:

an iterator to walk descendants
getDescendants
()

Implements org.jdom.Parent.getDescendants.

Returns an iterator that walks over all descendants in document order.
public Iterator<E>

Returns:

an iterator to walk descendants within a filter
getDescendants
(Filter
filter to select which descendants to see
filter
)

Implements org.jdom.Parent.getDescendants.

Returns an iterator that walks over all descendants in document order applying the Filter to return only elements that match the filter rule.
public DocType

Returns:

DocType - the DOCTYPE declaration.
getDocType
()

This will return the DocType declaration for this Document, or null if none exists.

public Document
getDocument()

Implements org.jdom.Parent.getDocument.

Return this parent's owning document or null if the branch containing this parent is currently not attached to a document.
public Parent
getParent()

Implements org.jdom.Parent.getParent.

Return this parent's parent, or null if this parent is currently not attached to another parent.
public Object

Returns:

the object associated with the given id
getProperty
(String
the id of the stored object to return
id
)

Returns the object associated with this document under the given "id" string, or null if there is no binding or if the binding explicitly stored a null value.

public Element

Returns:

Element - the document's root element
getRootElement
()

This will return the root Element for this Document

public final int

Returns:

int hash code
hashCode
()

Overrides java.lang.Object.hashCode.

This returns the hash code for this Document.
public boolean

Returns:

true if this document has a root element, false otherwise.
hasRootElement
()

This will return true if this document has a root element, false otherwise.

public int
indexOf(Content
child to search for
child
)

Implements org.jdom.Parent.indexOf.

Returns the index of the supplied child in the content list, or -1 if not a child of this parent.
public List<E>

Returns:

list of the old children detached from this parent
removeContent
()

Implements org.jdom.Parent.removeContent.

Removes all child content from this parent.
public List<E>

Returns:

list of the old children detached from this parent
removeContent
(Filter
filter to select which content to remove
filter
)

Implements org.jdom.Parent.removeContent.

Remove all child content from this parent matching the supplied filter.
public boolean
removeContent(Content
child to remove
child
)

Implements org.jdom.Parent.removeContent.

Removes a single child node from the content list.
public Content
removeContent(int
index of child to remove
index
)

Implements org.jdom.Parent.removeContent.

Removes and returns the child at the given index, or returns null if there's no such child.
public final void
setBaseURI(String
the base URI of this document
uri
)

Sets the effective URI from which this document was loaded, and against which relative URLs in this document will be resolved.

public Document

Returns:

this document modified
setContent
(Collection<E>
List of content to set
newContent
)

This sets the content of the Document.

public Document
setContent(int index, Content child)

public Document

Returns:

object on which the method was invoked
setContent
(int
- index of child to replace.
index
,
Collection<E>
- collection of content to add.
collection
)

Replace the child at the given index whith the supplied collection.

public Document

Returns:

the parent on which the method was called
setContent
(Content
new content to replace existing content
child
)

Set this document's content to be the supplied child.

public Document

Returns:

object on which the method was invoked
setDocType
(DocType
DocType declaration.
docType
)

This will set the DocType declaration for this Document.

public void
setProperty(String
the id of the stored object
id
,
Object
the object to store
value
)

Assigns an arbitrary object to be associated with this document under the given "id" string.

public Document

Returns:

Document - modified Document.
setRootElement
(Element
Element to be new root.
rootElement
)

This sets the root Element for the Document.

public String

Returns:

String - information about the Document
toString
()

Overrides java.lang.Object.toString.

This returns a String representation of the Document, suitable for debugging.
Inherited from java.lang.Object:
finalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

baseURIback to summary
protected String baseURI

See http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/core.html#baseURIs-Considerations

contentback to summary
pack-priv ContentList content

This document's content including comments, PIs, a possible DocType, and a root element. Subclassers have to track content using their own mechanism.

CVS_IDback to summary
private static final String CVS_ID
propertyMapback to summary
private HashMap<K, V> propertyMap

Constructor Detail

Documentback to summary
public Document()

Creates a new empty document. A document must have a root element, so this document will not be well-formed and accessor methods will throw an IllegalStateException if this document is accessed before a root element is added. This method is most useful for build tools.

Documentback to summary
public Document(Element rootElement, DocType docType, String baseURI)

This will create a new Document, with the supplied Element as the root element, the supplied DocType declaration, and the specified base URI.

Parameters
rootElement:Element

Element for document root.

docType:DocType

DocType declaration.

baseURI:String

the URI from which this doucment was loaded.

Exceptions
IllegalAddException:
if the given docType object is already attached to a document or the given rootElement already has a parent
Documentback to summary
public Document(Element rootElement, DocType docType)

This will create a new Document, with the supplied Element as the root element and the supplied DocType declaration.

Parameters
rootElement:Element

Element for document root.

docType:DocType

DocType declaration.

Exceptions
IllegalAddException:
if the given DocType object is already attached to a document or the given rootElement already has a parent
Documentback to summary
public Document(Element rootElement)

This will create a new Document, with the supplied Element as the root element, and no DocType declaration.

Parameters
rootElement:Element

Element for document root

Exceptions
IllegalAddException:
if the given rootElement already has a parent.
Documentback to summary
public Document(List<E> content)

This will create a new Document, with the supplied list of content, and a DocType declaration only if the content contains a DocType instance. A null list is treated the same as the no-arg constructor.

Parameters
content:List<E>

List of starter content

Exceptions
IllegalAddException:
if the List contains more than one Element or objects of illegal types.

Method Detail

addContentback to summary
public Document addContent(Content child)

Appends the child to the end of the content list.

Parameters
child:Content

child to append to end of content list

Returns:Document

the document on which the method was called

Exceptions
IllegalAddException:
if the given child already has a parent.
addContentback to summary
public Document addContent(Collection<E> c)

Appends all children in the given collection to the end of the content list. In event of an exception during add the original content will be unchanged and the objects in the supplied collection will be unaltered.

Parameters
c:Collection<E>

collection to append

Returns:Document

the document on which the method was called

Exceptions
IllegalAddException:
if any item in the collection already has a parent or is of an illegal type.
addContentback to summary
public Document addContent(int index, Content child)

Inserts the child into the content list at the given index.

Parameters
index:int

location for adding the collection

child:Content

child to insert

Returns:Document

the parent on which the method was called

Exceptions
IndexOutOfBoundsException:
if index is negative or beyond the current number of children
IllegalAddException:
if the given child already has a parent.
addContentback to summary
public Document addContent(int index, Collection<E> c)

Inserts the content in a collection into the content list at the given index. In event of an exception the original content will be unchanged and the objects in the supplied collection will be unaltered.

Parameters
index:int

location for adding the collection

c:Collection<E>

collection to insert

Returns:Document

the parent on which the method was called

Exceptions
IndexOutOfBoundsException:
if index is negative or beyond the current number of children
IllegalAddException:
if any item in the collection already has a parent or is of an illegal type.
cloneback to summary
public Object clone()

Overrides java.lang.Object.clone.

Implements org.jdom.Parent.clone.

This will return a deep clone of this Document.

Returns:Object

Object clone of this Document

cloneContentback to summary
public List<E> cloneContent()

Implements org.jdom.Parent.cloneContent.

Doc from org.jdom.Parent.cloneContent.

Returns a list containing detached clones of this parent's content list.

Returns:List<E>

list of cloned child content

detachRootElementback to summary
public Element detachRootElement()

Detach the root Element from this document.

Returns:Element

removed root Element

equalsback to summary
public final boolean equals(Object ob)

Overrides java.lang.Object.equals.

This tests for equality of this Document to the supplied Object.

Parameters
ob:Object

Object to compare to

Returns:boolean

boolean whether the Document is equal to the supplied Object

getBaseURIback to summary
public final String getBaseURI()

Returns the URI from which this document was loaded, or null if this is not known.

Returns:String

the base URI of this document

getContentback to summary
public Content getContent(int index)

Implements org.jdom.Parent.getContent.

Doc from org.jdom.Parent.getContent.

Returns the child at the given index.

Parameters
index:int

location of desired child

Returns:Content

child at the given index

getContentback to summary
public List<E> getContent()

Implements org.jdom.Parent.getContent.

This will return all content for the Document. The returned list is "live" in document order and changes to it affect the document's actual content.

Sequential traversal through the List is best done with a Iterator since the underlying implement of List.size() may require walking the entire list.

Returns:List<E>

List - all Document content

Exceptions
IllegalStateException:
if the root element hasn't been set
getContentback to summary
public List<E> getContent(Filter filter)

Implements org.jdom.Parent.getContent.

Return a filtered view of this Document's content.

Sequential traversal through the List is best done with a Iterator since the underlying implement of List.size() may require walking the entire list.

Parameters
filter:Filter

Filter to apply

Returns:List<E>

List - filtered Document content

Exceptions
IllegalStateException:
if the root element hasn't been set
getContentSizeback to summary
public int getContentSize()

Implements org.jdom.Parent.getContentSize.

Doc from org.jdom.Parent.getContentSize.

Returns the number of children in this parent's content list. Children may be any Content type.

Returns:int

number of children

getDescendantsback to summary
public Iterator<E> getDescendants()

Implements org.jdom.Parent.getDescendants.

Returns an iterator that walks over all descendants in document order.

Returns:Iterator<E>

an iterator to walk descendants

getDescendantsback to summary
public Iterator<E> getDescendants(Filter filter)

Implements org.jdom.Parent.getDescendants.

Returns an iterator that walks over all descendants in document order applying the Filter to return only elements that match the filter rule. With filters you can match only Elements, only Comments, Elements or Comments, only Elements with a given name and/or prefix, and so on.

Parameters
filter:Filter

filter to select which descendants to see

Returns:Iterator<E>

an iterator to walk descendants within a filter

getDocTypeback to summary
public DocType getDocType()

This will return the DocType declaration for this Document, or null if none exists.

Returns:DocType

DocType - the DOCTYPE declaration.

getDocumentback to summary
public Document getDocument()

Implements org.jdom.Parent.getDocument.

Doc from org.jdom.Parent.getDocument.

Return this parent's owning document or null if the branch containing this parent is currently not attached to a document.

Returns:Document

this child's owning document or null if none

See Also
org.jdom.Parent#getDocument()
getParentback to summary
public Parent getParent()

Implements org.jdom.Parent.getParent.

Doc from org.jdom.Parent.getParent.

Return this parent's parent, or null if this parent is currently not attached to another parent. This is the same method as in Content but also added to Parent to allow more easy up-the-tree walking.

Returns:Parent

this parent's parent or null if none

getPropertyback to summary
public Object getProperty(String id)

Returns the object associated with this document under the given "id" string, or null if there is no binding or if the binding explicitly stored a null value.

Parameters
id:String

the id of the stored object to return

Returns:Object

the object associated with the given id

getRootElementback to summary
public Element getRootElement()

This will return the root Element for this Document

Returns:Element

Element - the document's root element

Exceptions
IllegalStateException:
if the root element hasn't been set
hashCodeback to summary
public final int hashCode()

Overrides java.lang.Object.hashCode.

This returns the hash code for this Document.

Returns:int

int hash code

hasRootElementback to summary
public boolean hasRootElement()

This will return true if this document has a root element, false otherwise.

Returns:boolean

true if this document has a root element, false otherwise.

indexOfback to summary
public int indexOf(Content child)

Implements org.jdom.Parent.indexOf.

Doc from org.jdom.Parent.indexOf.

Returns the index of the supplied child in the content list, or -1 if not a child of this parent.

Parameters
child:Content

child to search for

Returns:int

index of child, or -1 if not found

removeContentback to summary
public List<E> removeContent()

Implements org.jdom.Parent.removeContent.

Removes all child content from this parent.

Returns:List<E>

list of the old children detached from this parent

removeContentback to summary
public List<E> removeContent(Filter filter)

Implements org.jdom.Parent.removeContent.

Remove all child content from this parent matching the supplied filter.

Parameters
filter:Filter

filter to select which content to remove

Returns:List<E>

list of the old children detached from this parent

removeContentback to summary
public boolean removeContent(Content child)

Implements org.jdom.Parent.removeContent.

Doc from org.jdom.Parent.removeContent.

Removes a single child node from the content list.

Parameters
child:Content

child to remove

Returns:boolean

whether the removal occurred

removeContentback to summary
public Content removeContent(int index)

Implements org.jdom.Parent.removeContent.

Doc from org.jdom.Parent.removeContent.

Removes and returns the child at the given index, or returns null if there's no such child.

Parameters
index:int

index of child to remove

Returns:Content

detached child at given index or null if no

setBaseURIback to summary
public final void setBaseURI(String uri)

Sets the effective URI from which this document was loaded, and against which relative URLs in this document will be resolved.

Parameters
uri:String

the base URI of this document

setContentback to summary
public Document setContent(Collection<E> newContent)

This sets the content of the Document. The supplied List should contain only objects of type Element, Comment, and ProcessingInstruction.

When all objects in the supplied List are legal and before the new content is added, all objects in the old content will have their parentage set to null (no parent) and the old content list will be cleared. This has the effect that any active list (previously obtained with a call to getContent) will also change to reflect the new content. In addition, all objects in the supplied List will have their parentage set to this document, but the List itself will not be "live" and further removals and additions will have no effect on this document content. If the user wants to continue working with a "live" list, then a call to setContent should be followed by a call to getContent to obtain a "live" version of the content.

Passing a null or empty List clears the existing content.

In event of an exception the original content will be unchanged and the objects in the supplied content will be unaltered.

Parameters
newContent:Collection<E>

List of content to set

Returns:Document

this document modified

Exceptions
IllegalAddException:
if the List contains objects of illegal types or with existing parentage.
setContentback to summary
public Document setContent(int index, Content child)
setContentback to summary
public Document setContent(int index, Collection<E> collection)

Replace the child at the given index whith the supplied collection.

In event of an exception the original content will be unchanged and the content in the supplied collection will be unaltered.

Parameters
index:int

- index of child to replace.

collection:Collection<E>

- collection of content to add.

Returns:Document

object on which the method was invoked

Exceptions
IllegalAddException:
if the collection contains objects of illegal types.
IndexOutOfBoundsException:
if index is negative or greater than the current number of children.
setContentback to summary
public Document setContent(Content child)

Set this document's content to be the supplied child.

If the supplied child is legal content for a Document and before it is added, all content in the current content list will be cleared and all current children will have their parentage set to null.

This has the effect that any active list (previously obtained with a call to one of the getContent methods will also change to reflect the new content. In addition, all content in the supplied collection will have their parentage set to this Document. If the user wants to continue working with a "live" list of this Document's child, then a call to setContent should be followed by a call to one of the getContent methods to obtain a "live" version of the children.

Passing a null child clears the existing content.

In event of an exception the original content will be unchanged and the supplied child will be unaltered.

Parameters
child:Content

new content to replace existing content

Returns:Document

the parent on which the method was called

Exceptions
IllegalAddException:
if the supplied child is already attached or not legal content for this parent
setDocTypeback to summary
public Document setDocType(DocType docType)

This will set the DocType declaration for this Document. Note that a DocType can only be attached to one Document. Attempting to set the DocType to a DocType object that already belongs to a Document will result in an IllegalAddException being thrown.

Parameters
docType:DocType

DocType declaration.

Returns:Document

object on which the method was invoked

Exceptions
IllegalAddException:
if the given docType is already attached to a Document.
setPropertyback to summary
public void setProperty(String id, Object value)

Assigns an arbitrary object to be associated with this document under the given "id" string. Null values are permitted. Strings beginning with "http://www.jdom.org/ are reserved for JDOM use.

Parameters
id:String

the id of the stored object

value:Object

the object to store

setRootElementback to summary
public Document setRootElement(Element rootElement)

This sets the root Element for the Document. If the document already has a root element, it is replaced.

Parameters
rootElement:Element

Element to be new root.

Returns:Document

Document - modified Document.

Exceptions
IllegalAddException:
if the given rootElement already has a parent.
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

This returns a String representation of the Document, suitable for debugging. If the XML representation of the Document is desired, org.jdom.output.XMLOutputter#outputString(Document) should be used.

Returns:String

String - information about the Document