org.jdom
Class Document


public class Document
implements Cloneable, Serializable

Document defines behavior for an XML Document, modeled in Java. Methods allow the user to the root element as well as processing instructions and other document-level information.

Author:
Brett McLaughlin, Jason Hunter
Version: 1.0

Constructor Summary
Document(Element rootElement, DocType docType)
           This will create a new Document, with the supplied Element as the root element and the supplied DocType declaration.
Document(Element rootElement)
           This will create a new Document, with the supplied Element as the root element, and no DocType declaration.

Method Summary
 DocumentaddContent(ProcessingInstruction pi)
           Adds the specified PI to the document.
 DocumentaddContent(Comment comment)
           This will add a comment to the Document.
 Objectclone()
           This will return a deep clone of this Document.
 final booleanequals(Object ob)
           This tests for equality of this Document to the supplied Object.
 DocTypegetDocType()
           This will return the DocType declaration for this Document, or null if none exists.
 ListgetMixedContent()
           This will return all content for the Document.
 ProcessingInstructiongetProcessingInstruction(String target)
           This returns the first processing instruction for this Document located at the document level (outside the root element) for the supplied target, or null if no such processing instruction exists.
 ListgetProcessingInstructions()
           This will return the list of ProcessingInstructions for this Document located at the document level (outside the root element).
 ListgetProcessingInstructions(String target)
           This returns the processing instructions for this Document located at the document level (outside the root element) which have the supplied target.
 ElementgetRootElement()
           This will return the root Element for this Document, or return null in the case the root element hasn't been yet set.
 final StringgetSerializedForm()
           This will return the Document in XML format, usable in an XML document.
 final inthashCode()
           This returns the hash code for this Document.
 booleanremoveContent(ProcessingInstruction pi)
           This removes the specified ProcessingInstruction.
 booleanremoveContent(Comment comment)
           This removes the specified Comment.
 booleanremoveProcessingInstruction(String target)
           This will remove the first PI with the specified target.
 booleanremoveProcessingInstructions(String target)
           This will remove all PIs with the specified target.
 DocumentsetDocType(DocType docType)
           This will set the DocType declaration for this Document.
 DocumentsetMixedContent(List content)
           This will set all content for the Document.
 DocumentsetProcessingInstructions(List pis)
           This sets the PIs for this Document to those in the List
 DocumentsetRootElement(Element rootElement)
           This sets the root Element for the Document.
 StringtoString()
           This returns a String representation of the Document, suitable for debugging.

Constructor Detail

Document

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 for document root.
docType - DocType declaration.

Document

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 for document root
Method Detail

addContent

public Document addContent(ProcessingInstruction pi)

Adds the specified PI to the document.

Parameters:
pi - the PI to add.
Returns: Document this document modified.

addContent

public Document addContent(Comment comment)

This will add a comment to the Document.

Parameters:
comment - Comment to add.
Returns: Document - this object modified.

clone

public Object clone()

This will return a deep clone of this Document.

Returns: Object - clone of this Document.

equals

public final boolean equals(Object ob)

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

Parameters:
ob - Object to compare to.
Returns: boolean - whether the Document is equal to the supplied Object.

getDocType

public DocType getDocType()

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

Returns: DocType - the DOCTYPE declaration.

getMixedContent

public List getMixedContent()

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

Returns: List - all Document content

getProcessingInstruction

public ProcessingInstruction getProcessingInstruction(String target)

This returns the first processing instruction for this Document located at the document level (outside the root element) for the supplied target, or null if no such processing instruction exists.

Parameters:
target - String target of PI to return.
Returns: ProcessingInstruction - the first PI with the specified target, or null if no such PI exists.

getProcessingInstructions

public List getProcessingInstructions()

This will return the list of ProcessingInstructions for this Document located at the document level (outside the root element). The returned list is "live" and changes to it affect the document's actual content.

Returns: List - PIs for document.

getProcessingInstructions

public List getProcessingInstructions(String target)

This returns the processing instructions for this Document located at the document level (outside the root element) which have the supplied target. The returned list is "live" and changes to it affect the document's actual content.

Parameters:
target - String target of PI to return.
Returns: List - all PIs with the specified target.

getRootElement

public Element getRootElement()

This will return the root Element for this Document, or return null in the case the root element hasn't been yet set.

Returns: Element - the document's root element, or null if none has been yet set

getSerializedForm

public final String getSerializedForm()

This will return the Document in XML format, usable in an XML document.

Returns: String - the serialized form of the Document.

hashCode

public final int hashCode()

This returns the hash code for this Document.

Returns: int - hash code.

removeContent

public boolean removeContent(ProcessingInstruction pi)

This removes the specified ProcessingInstruction.

Parameters:
child - ProcessingInstruction to delete
Returns: whether deletion occurred

removeContent

public boolean removeContent(Comment comment)

This removes the specified Comment.

Parameters:
comment - Comment to delete
Returns: whether deletion occurred

removeProcessingInstruction

public boolean removeProcessingInstruction(String target)

This will remove the first PI with the specified target.

Parameters:
target - String target of PI to remove.
Returns: boolean - whether the requested PI was removed.

removeProcessingInstructions

public boolean removeProcessingInstructions(String target)

This will remove all PIs with the specified target.

Parameters:
target - String target of PI to remove.
Returns: boolean - whether the requested PIs were removed.

setDocType

public Document setDocType(DocType docType)

This will set the DocType declaration for this Document.

Parameters:
docType - DocType declaration.

setMixedContent

public Document setMixedContent(List content)

This will set all content for the Document. The List may contain only objects of type Element, Comment, and ProcessingInstruction; and only one Element that becomes the root.

Parameters:
content - the new mixed content
Returns: the modified Document
Throws:
IllegalAddException - if the List contains more than one Element or objects of illegal types

setProcessingInstructions

public Document setProcessingInstructions(List pis)

This sets the PIs for this Document to those in the List

Parameters:
pis - List of PIs to use.
Returns: Document - this Document modified.

setRootElement

public Document setRootElement(Element rootElement)

This sets the root Element for the Document.

Parameters:
rootElement - Element to be new root.
Returns: Document - modified Document.

toString

public String toString()

This returns a String representation of the Document, suitable for debugging. If the XML representation of the Comment is desired, getSerializedForm() should be used.

Returns: String - information about the Document

Association Links

to Class java.util.List

This Document's {@link Comment}s, {@link ProcessingInstruction}s and the root {@link Element}

to Class org.jdom.Element

The root {@link Element} of the Document.

to Class org.jdom.DocType

The {@link DocType} declaration