# perldoc > XML::LibXML

---
type: CommandReference
command: XML::LibXML
mode: perldoc
section: 3
source: perldoc
---

## Quick Reference

- `XML::LibXML->load_xml(string => $xml_string)` — parse XML string
- `XML::LibXML->load_xml(location => $filename)` — parse XML file
- `XML::LibXML->load_xml(IO => $fh)` — parse from filehandle (use `binmode` or `:raw`)
- `$doc->toString()` — serialize document (returns byte string with encoding declaration)
- `$element->toString()` — serialize element (returns character string)
- `XML::LibXML::LIBXML_DOTTED_VERSION` — get libxml2 version string
- `XML::LibXML::LIBXML_VERSION` — get libxml2 version id
- `XML::LibXML::LIBXML_RUNTIME_VERSION` — get linked libxml2 version

## Name

`XML::LibXML` — Perl Binding for libxml2

## Synopsis

perl
use XML::LibXML;
my $dom = XML::LibXML->load_xml(string => <<'EOT');
<some-xml/>
EOT

$Version_String = XML::LibXML::LIBXML_DOTTED_VERSION;
$Version_ID = XML::LibXML::LIBXML_VERSION;
$DLL_Version = XML::LibXML::LIBXML_RUNTIME_VERSION;
$libxmlnode = XML::LibXML->import_GDOME( $node, $deep );
$gdomenode = XML::LibXML->export_GDOME( $node, $deep );
## Description

Interface to libxml2 providing XML and HTML parsers with DOM, SAX, and XMLReader interfaces, DOM Layer 3, and XPath support.

**Encoding rules:**
- Do **not** apply PerlIO layers (`:utf8`, `:encoding(...)`) to file handles used for parsing or saving — libxml2 handles encoding internally.
- All DOM functions accept and return character strings (UTF-8 with UTF8 flag on).
- `toString()` on a Document node returns a byte string with encoding declaration; on other nodes returns a character string.

**Key submodules:**
- `XML::LibXML::Parser` — parsing XML files
- `XML::LibXML::DOM` — DOM implementation
- `XML::LibXML::SAX` — direct SAX parser
- `XML::LibXML::Reader` — pull-parser
- `XML::LibXML::Dtd` — DTD validation
- `XML::LibXML::RelaxNG` — RelaxNG schema validation
- `XML::LibXML::Schema` — W3C Schema validation
- `XML::LibXML::XPathContext` — XPath evaluation
- `XML::LibXML::InputCallback` — custom URI resolver
- `XML::LibXML::Common` — common functions

**DOM node classes:**
- `XML::LibXML::Document` — document node
- `XML::LibXML::Node` — abstract base class
- `XML::LibXML::Element` — element node
- `XML::LibXML::Text` — text node
- `XML::LibXML::Comment` — comment node
- `XML::LibXML::CDATASection` — CDATA section
- `XML::LibXML::Attr` — attribute
- `XML::LibXML::DocumentFragment` — document fragment
- `XML::LibXML::Namespace` — namespace
- `XML::LibXML::PI` — processing instruction

## Options (Functions)

- `XML::LibXML->load_xml(...)` — parse XML from string, location, IO handle, or URI
- `XML::LibXML::LIBXML_DOTTED_VERSION` — returns libxml2 version string (e.g., `2.6.2`)
- `XML::LibXML::LIBXML_VERSION` — returns libxml2 version id (e.g., `20602`)
- `XML::LibXML::LIBXML_RUNTIME_VERSION` — returns version of linked libxml2
- `XML::LibXML->import_GDOME($node, $deep)` — clone a GDOME node into LibXML
- `XML::LibXML->export_GDOME($node, $deep)` — clone a LibXML node into GDOME
- `encodeToUTF8()` — (from `:encoding` tag) convert to UTF-8
- `decodeFromUTF8()` — (from `:encoding` tag) convert from UTF-8
- `XML::LibXML::Document->new($version, $encoding)` — create new document
- `$doc->createElement($name)` — create element
- `$element->appendText($text)` — append text
- `$node->toString()` — serialize node to string

## Examples

Basic parsing:

perl
use XML::LibXML;
my $dom = XML::LibXML->load_xml(location => 'file.xml');
my $root = $dom->documentElement;
print $root->name;
Parsing with encoding (avoid PerlIO layers):

perl
use XML::LibXML;
open my $fh, '<:raw', 'file.xml';  # or binmode $fh
my $doc = XML::LibXML->load_xml(IO => $fh);
## See Also

- `XML::LibXML::Parser`
- `XML::LibXML::DOM`
- `XML::LibXML::SAX`
- `XML::LibXML::Reader`
- `XML::LibXML::Dtd`
- `XML::LibXML::RelaxNG`
- `XML::LibXML::Schema`
- `XML::LibXML::XPathContext`
- `XML::LibXML::InputCallback`
- `XML::LibXML::Common`
- `XML::LibXSLT`
- `XML::LibXML::Iterator`
- `XML::CompactTree::XS`
- `XML::GDOME`

## Export Tags

`:all` — includes `:libxml`, `:encoding`, and `:ns`.

`:libxml` — exports integer constants for DOM node types (e.g., `XML_ELEMENT_NODE` => 1).

`:encoding` — exports `encodeToUTF8()` and `decodeFromUTF8()`.

`:ns` — exports `XML_XML_NS` and `XML_XMLNS_NS`.