# perldoc > XML::DOM

---
type: CommandReference
command: XML::DOM
mode: perldoc
section: 
source: perldoc
---

## Quick Reference

- `use XML::DOM` — Import the module
- `$parser = new XML::DOM::Parser` — Create a new parser
- `$doc = $parser->parsefile("file.xml")` — Parse an XML file
- `$nodes = $doc->getElementsByTagName("CODEBASE")` — Get elements by tag name
- `$doc->printToFile("out.xml")` — Print document to file
- `print $doc->toString` — Convert document to string
- `$doc->dispose` — Clean up memory
- `XML::DOM::setTagCompression(\&func)` — Set empty tag printing style

## Name

A perl module for building DOM Level 1 compliant document structures

## Synopsis

perl
use XML::DOM;

my $parser = new XML::DOM::Parser;
my $doc = $parser->parsefile ("file.xml");

my $nodes = $doc->getElementsByTagName ("CODEBASE");
my $n = $nodes->getLength;
for (my $i = 0; $i < $n; $i++) {
    my $node = $nodes->item ($i);
    my $href = $node->getAttributeNode ("HREF");
    print $href->getValue . "\n";
}

$doc->printToFile ("out.xml");
print $doc->toString;
$doc->dispose;
## Description

Extends [XML::Parser](http://localhost/phpMan.php/perldoc/XML%3A%3AParser/markdown) to build a DOM Level 1 compliant tree structure. The [XML::DOM::Parser](http://localhost/phpMan.php/perldoc/XML%3A%3ADOM%3A%3AParser/markdown) class parses XML strings or files and returns an [XML::DOM::Document](http://localhost/phpMan.php/perldoc/XML%3A%3ADOM%3A%3ADocument/markdown) object. The Document can be examined, modified, and written back to file or string.

**Node types** (DOM Level 1):

- [XML::DOM::Node](http://localhost/phpMan.php/perldoc/XML%3A%3ADOM%3A%3ANode/markdown) — Superclass
- [XML::DOM::Document](http://localhost/phpMan.php/perldoc/XML%3A%3ADOM%3A%3ADocument/markdown) — Root
- [XML::DOM::DocumentType](http://localhost/phpMan.php/perldoc/XML%3A%3ADOM%3A%3ADocumentType/markdown) — DOCTYPE declaration
- [XML::DOM::Element](http://localhost/phpMan.php/perldoc/XML%3A%3ADOM%3A%3AElement/markdown) — Element
- [XML::DOM::Attr](http://localhost/phpMan.php/perldoc/XML%3A%3ADOM%3A%3AAttr/markdown) — Attribute
- [XML::DOM::Text](http://localhost/phpMan.php/perldoc/XML%3A%3ADOM%3A%3AText/markdown) — Text
- [XML::DOM::CDATASection](http://localhost/phpMan.php/perldoc/XML%3A%3ADOM%3A%3ACDATASection/markdown) — CDATA section
- [XML::DOM::Comment](http://localhost/phpMan.php/perldoc/XML%3A%3ADOM%3A%3AComment/markdown) — Comment
- [XML::DOM::EntityReference](http://localhost/phpMan.php/perldoc/XML%3A%3ADOM%3A%3AEntityReference/markdown) — Entity reference
- [XML::DOM::Entity](http://localhost/phpMan.php/perldoc/XML%3A%3ADOM%3A%3AEntity/markdown) — Entity definition
- [XML::DOM::ProcessingInstruction](http://localhost/phpMan.php/perldoc/XML%3A%3ADOM%3A%3AProcessingInstruction/markdown) — PI
- [XML::DOM::DocumentFragment](http://localhost/phpMan.php/perldoc/XML%3A%3ADOM%3A%3ADocumentFragment/markdown) — Lightweight fragment
- [XML::DOM::Notation](http://localhost/phpMan.php/perldoc/XML%3A%3ADOM%3A%3ANotation/markdown) — Notation

**Additional node types** (not in DOM spec):

- [XML::DOM::ElementDecl](http://localhost/phpMan.php/perldoc/XML%3A%3ADOM%3A%3AElementDecl/markdown) — ELEMENT declaration
- [XML::DOM::AttlistDecl](http://localhost/phpMan.php/perldoc/XML%3A%3ADOM%3A%3AAttlistDecl/markdown) — ATTLIST declaration
- [XML::DOM::AttDef](http://localhost/phpMan.php/perldoc/XML%3A%3ADOM%3A%3AAttDef/markdown) — Attribute definition
- [XML::DOM::XMLDecl](http://localhost/phpMan.php/perldoc/XML%3A%3ADOM%3A%3AXMLDecl/markdown) — XML declaration

**Parser options**: `KeepCDATA` to store CDATASection nodes; `ParseParamEnt` and `ExpandParamEnt` to control parameter entity expansion.

## Options

### Global Variables

- `$XML::DOM::VERSION` — Version string (e.g., "1.43")

### Node Type Constants

- `UNKNOWN_NODE (0)` — Unknown
- `ELEMENT_NODE (1)` — Element
- `ATTRIBUTE_NODE (2)` — Attr
- `TEXT_NODE (3)` — Text
- `CDATA_SECTION_NODE (4)` — CDATASection
- `ENTITY_REFERENCE_NODE (5)` — EntityReference
- `ENTITY_NODE (6)` — Entity
- `PROCESSING_INSTRUCTION_NODE (7)` — ProcessingInstruction
- `COMMENT_NODE (8)` — Comment
- `DOCUMENT_NODE (9)` — Document
- `DOCUMENT_TYPE_NODE (10)` — DocumentType
- `DOCUMENT_FRAGMENT_NODE (11)` — DocumentFragment
- `NOTATION_NODE (12)` — Notation
- `ELEMENT_DECL_NODE (13)` — ElementDecl (not in DOM)
- `ATT_DEF_NODE (14)` — AttDef (not in DOM)
- `XML_DECL_NODE (15)` — XMLDecl (not in DOM)
- `ATTLIST_DECL_NODE (16)` — AttlistDecl (not in DOM)

### Package Methods

- `XML::DOM::ignoreReadOnly($bool)` — Set/toggle read-only checks; returns previous value
- `XML::DOM::getIgnoreReadOnly()` — Return current ignoreReadOnly flag
- `XML::DOM::isValidName($name)` — Check if name is a valid XML Name
- `XML::DOM::allowReservedNames($bool)` — Allow names starting with "xml" (default: 1)
- `XML::DOM::getAllowReservedNames()` — Return current allowReservedNames flag
- `XML::DOM::setTagCompression(\&func)` — Set empty tag printing style (0= `<empty/>`, 1= `<empty></empty>`, 2= `<empty />`)

## Examples

perl
use XML::DOM;
my $parser = new XML::DOM::Parser;
my $doc = $parser->parsefile("file.xml");
# ... manipulate ...
$doc->printToFile("out.xml");
perl
# Set tag compression for XHTML
XML::DOM::setTagCompression(\&my_tag_compression);
sub my_tag_compression {
    my ($tag, $elem) = @_;
    return 2 if $tag =~ /^(br|hr|img)$/;
    return 1;
}
perl
# Temporarily disable read-only checks
my $old = XML::DOM::ignoreReadOnly(1);
eval { ... };
XML::DOM::ignoreReadOnly($old);
## See Also

- [XML::DOM::XPath](http://localhost/phpMan.php/perldoc/XML%3A%3ADOM%3A%3AXPath/markdown)
- [XML::DOM::Parser](http://localhost/phpMan.php/perldoc/XML%3A%3ADOM%3A%3AParser/markdown)
- [XML::DOM::ValParser](http://localhost/phpMan.php/perldoc/XML%3A%3ADOM%3A%3AValParser/markdown)
- [XML::Parser](http://localhost/phpMan.php/perldoc/XML%3A%3AParser/markdown)
- [XML::LibXML](http://localhost/phpMan.php/perldoc/XML%3A%3ALibXML/markdown)
- [XML::GDOME](http://localhost/phpMan.php/perldoc/XML%3A%3AGDOME/markdown)
- [DOM Level 1 Specification](http://www.w3.org/TR/REC-DOM-Level-1)
- [XML 1.0 Specification](http://www.w3.org/TR/REC-xml)