XML technologies / XML / Perl & XML / Tree-oriented processing / XML trees

(Excerpt from “Perl & XML” by Erik T. Ray & Jason McIntosh)

Any XML document can be represented as data objects connected in a noncyclic structure called a tree. Each object, also called a node, represents part of the document, such as an element, text or a processing instruction. The node called the root connects to others, which connect to still others, continuing to nodes without further connections. Draw it and it resembles a large bushy tree, hence the name.

A tree structure is a practical way to represent XML. With no cycles, no node connects directly or indirectly to itself, so simple loops and recursion can be combined without risking endless loops. Like filesystem trees, a simple expression identifies each node’s position. Like real trees, breaking off a piece produces a smaller tree: a tree is essentially subtrees connected to their root. Best of all, all information is in one place, searchable like a database.

Trees greatly simplify programming. Event-based processing requires capturing passing details in your own structures or retaining them for later output. That is tedious and can become extremely expensive for complex documents; joining distant information can drive you mad. With a document tree, every detail is available. Just navigate nodes and extract what matters.

Nothing is free in programming. Random access to the document costs tree-construction time and CPU cycles, especially with an object-oriented tree model. Every node also consumes RAM. Large documents with millions of nodes are realistic and can overwhelm a machine. Normally, however, tree processing performs well, especially with optimizations such as those later in this chapter. Do not dismiss it yet.

Genealogical terms describe relationships between nodes. A container is the parent of its contained nodes, which are its children. The terms descendant, ancestor and siblings are then self-explanatory. Two siblings share a parent, and all other nodes descend from their ancestor, the root.

Tree types vary with implementation. Documents can be modeled in slightly different ways; for instance, should entity references and text share an object class? Each module therefore has a slightly different scheme. The following table lists common node types.

Table: Typical node types

Typ

Properties

Element

Name, Attribute und Referenzen auf Kinder

Namensraum

Präfix und URI

Character data (Zeichendaten)

Textstring

Verarbeitungsanweisung (PI)

Target, Daten

Kommentar

Textstring

CDATA-Abschnitt

Textstring

Entityreferenz

Name und Ersetzungstext (oder System-ID und/oder Public-ID)

Some implementations also define node types for the DTD, providing easy access to element declarations, entities, notations and attribute lists. Separate types may also exist for the XML declaration and document type declaration.

  

  

 

 

 

back to top