class Poco::XML::Document

Overview

The Document interface represents the entire HTML or XML document. More…

#include <Document.h>

class Document:
    public Poco::XML::AbstractContainerNode,
    public Poco::XML::DocumentEvent
{
public:
    // typedefs

    typedef Poco::AutoReleasePool<DOMObject> AutoReleasePool;

    // construction

    Document(NamePool* pNamePool = 0);
    Document(unsigned long namePoolSize);

    Document(
        DocumentType* pDocumentType,
        NamePool* pNamePool = 0
        );

    Document(
        DocumentType* pDocumentType,
        unsigned long namePoolSize
        );

    // methods

    NamePool&
    namePool();

    AutoReleasePool&
    autoReleasePool();

    void
    collectGarbage();

    void
    suspendEvents();

    void
    resumeEvents();

    virtual
    bool
    eventsSuspended() const;

    virtual
    bool
    events() const;

    const DocumentType*
    doctype() const;

    const DOMImplementation&
    implementation() const;

    Element*
    documentElement() const;

    Element*
    createElement(const XMLString& tagName) const;

    DocumentFragment*
    createDocumentFragment() const;

    Text*
    createTextNode(const XMLString& data) const;

    Comment*
    createComment(const XMLString& data) const;

    CDATASection*
    createCDATASection(const XMLString& data) const;

    ProcessingInstruction*
    createProcessingInstruction(
        const XMLString& target,
        const XMLString& data
        ) const;

    Attr*
    createAttribute(const XMLString& name) const;

    EntityReference*
    createEntityReference(const XMLString& name) const;

    NodeList*
    getElementsByTagName(const XMLString& name) const;

    Node*
    importNode(
        Node* importedNode,
        bool deep
        );

    Element*
    createElementNS(
        const XMLString& namespaceURI,
        const XMLString& qualifiedName
        ) const;

    Attr*
    createAttributeNS(
        const XMLString& namespaceURI,
        const XMLString& qualifiedName
        ) const;

    NodeList*
    getElementsByTagNameNS(
        const XMLString& namespaceURI,
        const XMLString& localName
        ) const;

    Element*
    getElementById(const XMLString& elementId) const;

    virtual
    Event*
    createEvent(const XMLString& eventType) const;

    virtual
    const XMLString&
    nodeName() const;

    virtual
    unsigned short
    nodeType() const;

    virtual
    bool
    dispatchEvent(Event* evt);

    Entity*
    createEntity(
        const XMLString& name,
        const XMLString& publicId,
        const XMLString& systemId,
        const XMLString& notationName
        ) const;

    Notation*
    createNotation(
        const XMLString& name,
        const XMLString& publicId,
        const XMLString& systemId
        ) const;

    Element*
    getElementById(
        const XMLString& elementId,
        const XMLString& idAttribute
        ) const;

    Element*
    getElementByIdNS(
        const XMLString& elementId,
        const XMLString& idAttributeURI,
        const XMLString& idAttributeLocalName
        ) const;

protected:
    // methods

    virtual
    Node*
    copyNode(
        bool deep,
        Document* pOwnerDocument
        ) const;

    DocumentType*
    getDoctype();

    void
    setDoctype(DocumentType* pDoctype);
};

Inherited Members

public:
    // typedefs

    typedef Poco::XML::NamespaceSupport NSMap;

    // enums

    enum
    {
        ELEMENT_NODE                = 1,
        ATTRIBUTE_NODE,
        TEXT_NODE,
        CDATA_SECTION_NODE,
        ENTITY_REFERENCE_NODE,
        ENTITY_NODE,
        PROCESSING_INSTRUCTION_NODE,
        COMMENT_NODE,
        DOCUMENT_NODE,
        DOCUMENT_TYPE_NODE,
        DOCUMENT_FRAGMENT_NODE,
        NOTATION_NODE,
    };

    // methods

    void
    duplicate() const;

    void
    release() const;

    virtual
    void
    autoRelease() = 0;

    virtual
    void
    addEventListener(
        const XMLString& type,
        EventListener* listener,
        bool useCapture
        ) = 0;

    virtual
    void
    removeEventListener(
        const XMLString& type,
        EventListener* listener,
        bool useCapture
        ) = 0;

    virtual
    bool
    dispatchEvent(Event* evt) = 0;

    virtual
    const XMLString&
    nodeName() const = 0;

    const XMLString&
    nodeValue() const;

    virtual
    const XMLString&
    getNodeValue() const = 0;

    virtual
    void
    setNodeValue(const XMLString& value) = 0;

    virtual
    unsigned short
    nodeType() const = 0;

    virtual
    Node*
    parentNode() const = 0;

    virtual
    NodeList*
    childNodes() const = 0;

    virtual
    Node*
    firstChild() const = 0;

    virtual
    Node*
    lastChild() const = 0;

    virtual
    Node*
    previousSibling() const = 0;

    virtual
    Node*
    nextSibling() const = 0;

    virtual
    NamedNodeMap*
    attributes() const = 0;

    virtual
    Document*
    ownerDocument() const = 0;

    virtual
    Node*
    insertBefore(
        Node* newChild,
        Node* refChild
        ) = 0;

    virtual
    Node*
    replaceChild(
        Node* newChild,
        Node* oldChild
        ) = 0;

    virtual
    Node*
    removeChild(Node* oldChild) = 0;

    virtual
    Node*
    appendChild(Node* newChild) = 0;

    virtual
    bool
    hasChildNodes() const = 0;

    virtual
    Node*
    cloneNode(bool deep) const = 0;

    virtual
    void
    normalize() = 0;

    virtual
    bool
    isSupported(
        const XMLString& feature,
        const XMLString& version
        ) const = 0;

    virtual
    const XMLString&
    namespaceURI() const = 0;

    virtual
    XMLString
    prefix() const = 0;

    virtual
    const XMLString&
    localName() const = 0;

    virtual
    bool
    hasAttributes() const = 0;

    virtual
    XMLString
    innerText() const = 0;

    virtual
    Node*
    getNodeByPath(const XMLString& path) const = 0;

    virtual
    Node*
    getNodeByPathNS(
        const XMLString& path,
        const NSMap& nsMap
        ) const = 0;

    virtual
    const XMLString&
    nodeName() const;

    virtual
    const XMLString&
    getNodeValue() const;

    virtual
    void
    setNodeValue(const XMLString& value);

    virtual
    Node*
    parentNode() const;

    virtual
    NodeList*
    childNodes() const;

    virtual
    Node*
    firstChild() const;

    virtual
    Node*
    lastChild() const;

    virtual
    Node*
    previousSibling() const;

    virtual
    Node*
    nextSibling() const;

    virtual
    NamedNodeMap*
    attributes() const;

    virtual
    Document*
    ownerDocument() const;

    virtual
    Node*
    insertBefore(
        Node* newChild,
        Node* refChild
        );

    virtual
    Node*
    replaceChild(
        Node* newChild,
        Node* oldChild
        );

    virtual
    Node*
    removeChild(Node* oldChild);

    virtual
    Node*
    appendChild(Node* newChild);

    virtual
    bool
    hasChildNodes() const;

    virtual
    Node*
    cloneNode(bool deep) const;

    virtual
    void
    normalize();

    virtual
    bool
    isSupported(
        const XMLString& feature,
        const XMLString& version
        ) const;

    virtual
    const XMLString&
    namespaceURI() const;

    virtual
    XMLString
    prefix() const;

    virtual
    const XMLString&
    localName() const;

    virtual
    bool
    hasAttributes() const;

    virtual
    void
    addEventListener(
        const XMLString& type,
        EventListener* listener,
        bool useCapture
        );

    virtual
    void
    removeEventListener(
        const XMLString& type,
        EventListener* listener,
        bool useCapture
        );

    virtual
    bool
    dispatchEvent(Event* evt);

    virtual
    XMLString
    innerText() const;

    virtual
    Node*
    getNodeByPath(const XMLString& path) const;

    virtual
    Node*
    getNodeByPathNS(
        const XMLString& path,
        const NSMap& nsMap
        ) const;

    virtual
    void
    autoRelease();

    virtual
    Node*
    firstChild() const;

    virtual
    Node*
    lastChild() const;

    virtual
    Node*
    insertBefore(
        Node* newChild,
        Node* refChild
        );

    virtual
    Node*
    replaceChild(
        Node* newChild,
        Node* oldChild
        );

    virtual
    Node*
    removeChild(Node* oldChild);

    virtual
    Node*
    appendChild(Node* newChild);

    virtual
    bool
    hasChildNodes() const;

    virtual
    bool
    hasAttributes() const;

    virtual
    Node*
    getNodeByPath(const XMLString& path) const;

    virtual
    Node*
    getNodeByPathNS(
        const XMLString& path,
        const NSMap& nsMap
        ) const;

    virtual
    Event*
    createEvent(const XMLString& eventType) const = 0;

protected:
    // fields

    static const XMLString EMPTY_STRING;

    // methods

    virtual
    Node*
    copyNode(
        bool deep,
        Document* pOwnerDocument
        ) const = 0;

    virtual
    bool
    events() const;

    virtual
    bool
    eventsSuspended() const;

    void
    captureEvent(Event* evt);

    void
    bubbleEvent(Event* evt);

    void
    dispatchSubtreeModified();

    void
    dispatchNodeInserted();

    void
    dispatchNodeRemoved();

    virtual
    void
    dispatchNodeRemovedFromDocument();

    virtual
    void
    dispatchNodeInsertedIntoDocument();

    void
    dispatchAttrModified(
        Attr* pAttr,
        MutationEvent::AttrChangeType changeType,
        const XMLString& prevValue,
        const XMLString& newValue
        );

    void
    dispatchCharacterDataModified(
        const XMLString& prevValue,
        const XMLString& newValue
        );

    void
    setOwnerDocument(Document* pOwnerDocument);

    virtual
    void
    dispatchNodeRemovedFromDocument();

    virtual
    void
    dispatchNodeInsertedIntoDocument();

    bool
    hasAttributeValue(
        const XMLString& name,
        const XMLString& value,
        const NSMap* pNSMap
        ) const;

    static
    const Node*
    findNode(
        XMLString::const_iterator& it,
        const XMLString::const_iterator& end,
        const Node* pNode,
        const NSMap* pNSMap
        );

    static
    const Node*
    findElement(
        const XMLString& name,
        const Node* pNode,
        const NSMap* pNSMap
        );

    static
    const Node*
    findElement(
        int index,
        const Node* pNode,
        const NSMap* pNSMap
        );

    static
    const Node*
    findElement(
        const XMLString& attr,
        const XMLString& value,
        const Node* pNode,
        const NSMap* pNSMap
        );

    static
    const Attr*
    findAttribute(
        const XMLString& name,
        const Node* pNode,
        const NSMap* pNSMap
        );

    static
    bool
    namesAreEqual(
        const Node* pNode1,
        const Node* pNode2,
        const NSMap* pNSMap
        );

    static
    bool
    namesAreEqual(
        const Node* pNode,
        const XMLString& name,
        const NSMap* pNSMap
        );

Detailed Documentation

The Document interface represents the entire HTML or XML document.

Conceptually, it is the root of the document tree, and provides the primary access to the document’s data.

Since elements, text nodes, comments, processing instructions, etc. cannot exist outside the context of a Document, the Document interface also contains the factory methods needed to create these objects. The Node objects created have a ownerDocument attribute which associates them with the Document within whose context they were created.

Construction

Document(NamePool* pNamePool = 0)

Creates a new document.

If pNamePool == 0, the document creates its own name pool, otherwise it uses the given name pool. Sharing a name pool makes sense for documents containing instances of the same schema, thus reducing memory usage.

Document(unsigned long namePoolSize)

Creates a new document using a name pool with the given size, which should be a prime number (e.g., 251, 509, 1021, 4093).

Document(
    DocumentType* pDocumentType,
    NamePool* pNamePool = 0
    )

Creates a new document.

If pNamePool == 0, the document creates its own name pool, otherwise it uses the given name pool. Sharing a name pool makes sense for documents containing instances of the same schema, thus reducing memory usage.

Document(
    DocumentType* pDocumentType,
    unsigned long namePoolSize
    )

Creates a new document using a name pool with the given size, which should be a prime number (e.g., 251, 509, 1021, 4093).

Methods

NamePool&
namePool()

Returns a pointer to the documents Name Pool.

AutoReleasePool&
autoReleasePool()

Returns a pointer to the documents Auto Release Pool.

void
collectGarbage()

Releases all objects in the Auto Release Pool.

void
suspendEvents()

Suspends all events until resumeEvents() is called.

void
resumeEvents()

Resumes all events suspended with suspendEvent();.

virtual
bool
eventsSuspended() const

Returns true if events are suspeded.

virtual
bool
events() const

Returns true if events are not suspeded.

const DocumentType*
doctype() const

The Document Type Declaration (see DocumentType) associated with this document.

For HTML documents as well as XML documents without a document type declaration this returns null. The DOM Level 1 does not support editing the Document Type Declaration. docType cannot be altered in any way, including through the use of methods inherited from the Node interface, such as insertNode or removeNode.

const DOMImplementation&
implementation() const

The DOMImplementation object that handles this document.

A DOM application may use objects from multiple implementations.

Element*
documentElement() const

This is a convenience attribute that allows direct access to the child node that is the root element of the document.

For HTML documents, this is the element with the tagName “HTML”.

Element*
createElement(const XMLString& tagName) const

Creates an element of the type specified.

Note that the instance returned implements the Element interface, so attributes can be specified directly on the returned object.

In addition, if there are known attributes with default values, Attr nodes representing them are automatically created and attached to the element.

DocumentFragment*
createDocumentFragment() const

Creates an empty DocumentFragment object.

Text*
createTextNode(const XMLString& data) const

Creates a text node given the specified string.

Comment*
createComment(const XMLString& data) const

Creates a comment node given the specified string.

CDATASection*
createCDATASection(const XMLString& data) const

Creates a CDATASection node whose value is the specified string.

ProcessingInstruction*
createProcessingInstruction(
    const XMLString& target,
    const XMLString& data
    ) const

Creates a ProcessingInstruction node given the specified target and data strings.

Attr*
createAttribute(const XMLString& name) const

Creates an Attr of the given name.

Note that the Attr instance can then be set on an Element using the setAttributeNode method.

EntityReference*
createEntityReference(const XMLString& name) const

Creates an EntityReference object.

In addition, if the referenced entity is known, the child list of the EntityReference node is made the same as that of the corresponding Entity node.

NodeList*
getElementsByTagName(const XMLString& name) const

Returns a NodeList of all Elements with a given tag name in the order in which they would be encountered in a preorder traversal of the document tree.

The returned NodeList must be released with a call to release() when no longer needed.

Node*
importNode(
    Node* importedNode,
    bool deep
    )

Imports a node from another document to this document.

The returned node has no parent; (parentNode is null). The source node is not altered or removed from the original document; this method creates a new copy of the source node. For all nodes, importing a node creates a node object owned by the importing document, with attribute values identical to the source node’s nodeName and nodeType, plus the attributes related to namespaces (prefix, localName, and namespaceURI). As in the cloneNode operation on a Node, the source node is not altered. Additional information is copied as appropriate to the nodeType, attempting to mirror the behavior expected if a fragment of XML or HTML source was copied from one document to another, recognizing that the two documents may have different DTDs in the XML case.

Element*
createElementNS(
    const XMLString& namespaceURI,
    const XMLString& qualifiedName
    ) const

Creates an element of the given qualified name and namespace URI.

Attr*
createAttributeNS(
    const XMLString& namespaceURI,
    const XMLString& qualifiedName
    ) const

Creates an attribute of the given qualified name and namespace URI.

NodeList*
getElementsByTagNameNS(
    const XMLString& namespaceURI,
    const XMLString& localName
    ) const

Returns a NodeList of all the Elements with a given local name and namespace URI in the order in which they are encountered in a preorder traversal of the Document tree.

Element*
getElementById(const XMLString& elementId) const

Returns the Element whose ID is given by elementId.

If no such element exists, returns null. Behavior is not defined if more than one element has this ID.

Note: The DOM implementation must have information that says which attributes are of type ID. Attributes with the name “ID” are not of type ID unless so defined. Implementations that do not know whether attributes are of type ID or not are expected to return null. This implementation therefore returns null.

See also the non-standard two argument variant of getElementById() and getElementByIdNS().

virtual
Event*
createEvent(const XMLString& eventType) const

Creates an event of the specified type.

The eventType parameter specifies the type of Event interface to be created. If the Event interface specified is supported by the implementation this method will return a new Event of the interface type requested. If the Event is to be dispatched via the dispatchEvent method the appropriate event init method must be called after creation in order to initialize the Event ‘s values. As an example, a user wishing to synthesize some kind of UIEvent would call createEvent with the parameter “UIEvents”. The initUIEvent method could then be called on the newly created UIEvent to set the specific type of UIEvent to be dispatched and set its context information. The createEvent method is used in creating Events when it is either inconvenient or unnecessary for the user to create an Event themselves. In cases where the implementation provided Event is insufficient, users may supply their own Event implementations for use with the dispatchEvent method.

virtual
const XMLString&
nodeName() const

Returns the name of this node, depending on its type.

virtual
unsigned short
nodeType() const

Returns a code representing the type of the underlying object.

virtual
bool
dispatchEvent(Event* evt)

This method allows the dispatch of events into the implementations event model.

Events dispatched in this manner will have the same capturing and bubbling behavior as events dispatched directly by the implementation. The target of the event is the EventTarget on which dispatchEvent is called.

Entity*
createEntity(
    const XMLString& name,
    const XMLString& publicId,
    const XMLString& systemId,
    const XMLString& notationName
    ) const

Creates an Entity with the given name, publicId, systemId and notationName.

This method is not part of the W3C Document Object Model.

Notation*
createNotation(
    const XMLString& name,
    const XMLString& publicId,
    const XMLString& systemId
    ) const

Creates a Notation with the given name, publicId and systemId.

This method is not part of the W3C Document Object Model.

Element*
getElementById(
    const XMLString& elementId,
    const XMLString& idAttribute
    ) const

Returns the first Element whose ID attribute (given in idAttribute) has the given elementId.

If no such element exists, returns null.

This method is an extension to the W3C Document Object Model.

Element*
getElementByIdNS(
    const XMLString& elementId,
    const XMLString& idAttributeURI,
    const XMLString& idAttributeLocalName
    ) const

Returns the first Element whose ID attribute (given in idAttributeURI and idAttributeLocalName) has the given elementId.

If no such element exists, returns null.

This method is an extension to the W3C Document Object Model.