scala.xml

package scala.xml

This library provides support for the XML literal syntax in Scala programs.

val planets: scala.xml.Elem = <planets>
 <planet id="earth">
   <title>Earth</title>
   <mass unit="kg">5.9742e24</mass>
   <radius unit="m">6378.14e3</radius>
 </planet>
 <planet id="mars">
   <title>Mars</title>
   <mass unit="kg">0.64191e24</mass>
   <radius unit="m">3397.0e3</radius>
 </planet>
</planets>

Additionally, you can mix Scala expressions in your XML elements by using the curly brace notation:

val sunMass = 1.99e30
val sunRadius = 6.96e8
val star = <star>
 <title>Sun</title>
 <mass unit="kg">{ sunMass }</mass>
 <radius unit="m">{ sunRadius }</radius>
 <surface unit="m²">{ 4 * Math.PI * Math.pow(sunRadius, 2) }</surface>
 <volume unit="m³">{ 4/3 * Math.PI * Math.pow(sunRadius, 3) }</volume>
</star>

An XML element, for example <star/> and <planet/>, is represented in this library as a case class, scala.xml.Elem.

The sub-elements of XML values share a common base class, scala.xml.Node.

However, the non-element declarations found in XML files share a different common base class, scala.xml.dtd.Decl. Additionally, document type declarations are represented by a different trait, scala.xml.dtd.DTD.

For reading and writing XML data to and from files, see scala.xml.XML. The default parser of XML data is the Xerces parser and is provided in Java by javax.xml.parsers.SAXParser.

For more control of the input, use the parser written in Scala that is provided, scala.xml.parsing.ConstructingParser.

For working with XHTML input, use scala.xml.parsing.XhtmlParser.

For more control of the output, use the scala.xml.PrettyPrinter.

Utility methods for working with XML data are provided in scala.xml.Utility.

XML values in Scala are immutable, but you can traverse and transform XML data with a scala.xml.transform.RuleTransformer.

Attributes

Members list

Type members

Classlikes

class Atom[+A](val data: A) extends SpecialNode, Serializable

The class Atom provides an XML node for text (PCDATA). It is used in both non-bound and bound XML representations.

The class Atom provides an XML node for text (PCDATA). It is used in both non-bound and bound XML representations.

Value parameters

data

the text contained in this node, may not be '''null'''.

Attributes

Supertypes
class SpecialNode
class Node
class NodeSeq
trait Serializable
trait Equality
trait Seq[Node]
trait SeqOps[Node, Seq, NodeSeq]
trait Iterable[Node]
class AbstractSeq[Node]
trait Seq[Node]
trait Equals
trait SeqOps[Node, Seq, NodeSeq]
trait Int => Node
trait Iterable[Node]
class Object
trait Matchable
class Any
Show all
Known subtypes
class PCData
class Text
class Unparsed
object Attribute

This singleton object contains the apply and unapply methods for convenient construction and deconstruction.

This singleton object contains the apply and unapply methods for convenient construction and deconstruction.

Attributes

Companion
trait
Supertypes
class Object
trait Matchable
class Any
Self type
Attribute.type
trait Attribute extends MetaData

The Attribute trait defines the interface shared by both scala.xml.PrefixedAttribute and scala.xml.UnprefixedAttribute.

The Attribute trait defines the interface shared by both scala.xml.PrefixedAttribute and scala.xml.UnprefixedAttribute.

Attributes

Companion
object
Supertypes
Known subtypes
case class Comment(commentText: String) extends SpecialNode

The class Comment implements an XML node for comments.

The class Comment implements an XML node for comments.

Value parameters

commentText

the text contained in this node, may not contain "--" and the final character may not be - to prevent a closing span of --> which is invalid. https://www.w3.org/TR/xml11//#IDA5CES

Attributes

Supertypes
trait Product
class SpecialNode
class Node
class NodeSeq
trait Serializable
trait Equality
trait Seq[Node]
trait SeqOps[Node, Seq, NodeSeq]
trait Iterable[Node]
class AbstractSeq[Node]
trait Seq[Node]
trait Equals
trait SeqOps[Node, Seq, NodeSeq]
trait Int => Node
trait Iterable[Node]
class Object
trait Matchable
class Any
Show all
class Document extends NodeSeq, Serializable

A document information item (according to InfoSet spec). The comments are copied from the Infoset spec, only augmented with some information on the Scala types for definitions that might have no value. Also plays the role of an XMLEvent for pull parsing.

A document information item (according to InfoSet spec). The comments are copied from the Infoset spec, only augmented with some information on the Scala types for definitions that might have no value. Also plays the role of an XMLEvent for pull parsing.

Attributes

Supertypes
object Elem

This singleton object contains the apply and unapplySeq methods for convenient construction and deconstruction. It is possible to deconstruct any Node instance (that is not a SpecialNode or a Group) using the syntax case Elem(prefix, label, attribs, scope, child @ _*) => ...

This singleton object contains the apply and unapplySeq methods for convenient construction and deconstruction. It is possible to deconstruct any Node instance (that is not a SpecialNode or a Group) using the syntax case Elem(prefix, label, attribs, scope, child @ _*) => ...

Attributes

Companion
class
Supertypes
class Object
trait Matchable
class Any
Self type
Elem.type
class Elem(val prefix: String, val label: String, attributes1: MetaData, val scope: NamespaceBinding, val minimizeEmpty: Boolean, val child: Node*) extends Node, Serializable

An immutable data object representing an XML element.

An immutable data object representing an XML element.

Child elements can be other Elems or any one of the other Node types.

XML attributes are implemented with the scala.xml.MetaData base class.

Optional XML namespace scope is represented by scala.xml.NamespaceBinding.

Value parameters

attributes1

the attribute map

child

the children of this node

label

the element name

minimizeEmpty

true if this element should be serialized as minimized (i.e. "<el/>") when empty; false if it should be written out in long form.

prefix

namespace prefix (may be null, but not the empty string)

scope

the scope containing the namespace bindings

Attributes

Companion
object
Supertypes
class Node
class NodeSeq
trait Serializable
trait Equality
trait Seq[Node]
trait SeqOps[Node, Seq, NodeSeq]
trait Iterable[Node]
class AbstractSeq[Node]
trait Seq[Node]
trait Equals
trait SeqOps[Node, Seq, NodeSeq]
trait Int => Node
trait Iterable[Node]
class Object
trait Matchable
class Any
Show all
case class EntityRef(entityName: String) extends SpecialNode

The class EntityRef implements an XML node for entity references.

The class EntityRef implements an XML node for entity references.

Value parameters

entityName

the name of the entity reference, for example amp.

Attributes

Supertypes
trait Product
class SpecialNode
class Node
class NodeSeq
trait Serializable
trait Equality
trait Seq[Node]
trait SeqOps[Node, Seq, NodeSeq]
trait Iterable[Node]
class AbstractSeq[Node]
trait Seq[Node]
trait Equals
trait SeqOps[Node, Seq, NodeSeq]
trait Int => Node
trait Iterable[Node]
class Object
trait Matchable
class Any
Show all
object Equality

In an attempt to contain the damage being inflicted on consistency by the ad hoc equals methods spread around xml, the logic is centralized and all the xml classes go through the xml.Equality trait. There are two forms of xml comparison.

In an attempt to contain the damage being inflicted on consistency by the ad hoc equals methods spread around xml, the logic is centralized and all the xml classes go through the xml.Equality trait. There are two forms of xml comparison.

  1. '''def''' strict_==(other: scala.xml.Equality)

This one tries to honor the little things like symmetry and hashCode contracts. The equals method routes all comparisons through this.

  1. xml_==(other: Any)

This one picks up where strict_== leaves off. It might declare any two things equal.

As things stood, the logic not only made a mockery of the collections equals contract, but also laid waste to that of case classes.

Among the obstacles to sanity are/were:

Node extends NodeSeq extends Seq[Node] MetaData extends Iterable[MetaData] The hacky "Group" xml node which throws exceptions with wild abandon, so don't get too close Rampant asymmetry and impossible hashCodes Most classes claiming to be equal to "String" if some specific stringification of it was the same. String was never going to return the favor.

Attributes

Companion
trait
Supertypes
class Object
trait Matchable
class Any
Self type
Equality.type
trait Equality extends Equals

Attributes

Companion
object
Supertypes
trait Equals
class Object
trait Matchable
class Any
Known subtypes
class MetaData
trait Attribute
object Null
object TopScope
class NodeSeq
class Document
class Node
class Elem
class Group
class SpecialNode
class Atom[A]
class PCData
class Text
class Unparsed
class Comment
class EntityRef
class ProcInstr
Show all
final case class Group(nodes: Seq[Node]) extends Node

A hack to group XML nodes in one node for output.

A hack to group XML nodes in one node for output.

Attributes

Supertypes
trait Product
class Node
class NodeSeq
trait Serializable
trait Equality
trait Seq[Node]
trait SeqOps[Node, Seq, NodeSeq]
trait Iterable[Node]
class AbstractSeq[Node]
trait Seq[Node]
trait Equals
trait SeqOps[Node, Seq, NodeSeq]
trait Int => Node
trait Iterable[Node]
class Object
trait Matchable
class Any
Show all

Attributes

Supertypes
trait Product
trait Equals
class Exception
class Throwable
trait Serializable
class Object
trait Matchable
class Any
Show all
object MetaData

Attributes

Companion
class
Supertypes
class Object
trait Matchable
class Any
Self type
MetaData.type

This class represents an attribute and at the same time a linked list of attributes. Every instance of this class is either

This class represents an attribute and at the same time a linked list of attributes. Every instance of this class is either

  • an instance of UnprefixedAttribute key,value or
  • an instance of PrefixedAttribute namespace_prefix,key,value or
  • Null, the empty attribute list.

Namespace URIs are obtained by using the namespace scope of the element owning this attribute (see getNamespace).

Attributes

Companion
object
Supertypes
Known subtypes
object MinimizeMode extends Enumeration

Governs how empty elements (i.e. those without child elements) should be serialized.

Governs how empty elements (i.e. those without child elements) should be serialized.

Attributes

Supertypes
class Enumeration
trait Serializable
class Object
trait Matchable
class Any
Self type
case class NamespaceBinding(prefix: String, uri: String, parent: NamespaceBinding) extends Equality

The class NamespaceBinding represents namespace bindings and scopes. The binding for the default namespace is treated as a null prefix. the absent namespace is represented with the null uri. Neither prefix nor uri may be empty, which is not checked.

The class NamespaceBinding represents namespace bindings and scopes. The binding for the default namespace is treated as a null prefix. the absent namespace is represented with the null uri. Neither prefix nor uri may be empty, which is not checked.

Attributes

Supertypes
trait Serializable
trait Product
trait Equality
trait Equals
class Object
trait Matchable
class Any
Show all
Known subtypes
object TopScope
object Node

This singleton object contains the unapplySeq method for convenient deconstruction.

This singleton object contains the unapplySeq method for convenient deconstruction.

Attributes

Companion
class
Supertypes
class Object
trait Matchable
class Any
Self type
Node.type
abstract class Node extends NodeSeq

An abstract class representing XML with nodes of a labeled tree. This class contains an implementation of a subset of XPath for navigation.

An abstract class representing XML with nodes of a labeled tree. This class contains an implementation of a subset of XPath for navigation.

Attributes

Companion
object
Supertypes
Known subtypes
class Elem
class Group
class SpecialNode
class Atom[A]
class PCData
class Text
class Unparsed
class Comment
class EntityRef
class ProcInstr
Show all
class NodeBuffer extends ArrayBuffer[Node]

This class acts as a Buffer for nodes. If it is used as a sequence of nodes Seq[Node], it must be ensured that no updates occur after that point, because scala.xml.Node is assumed to be immutable.

This class acts as a Buffer for nodes. If it is used as a sequence of nodes Seq[Node], it must be ensured that no updates occur after that point, because scala.xml.Node is assumed to be immutable.

Despite this being a sequence, don't use it as key in a hashtable. Calling the hashcode function will result in a runtime error.

Attributes

Supertypes
object NodeSeq

This object ...

This object ...

Attributes

Companion
class
Supertypes
class Object
trait Matchable
class Any
Self type
NodeSeq.type
abstract class NodeSeq extends AbstractSeq[Node], Seq[Node], Equality, Serializable

This class implements a wrapper around Seq[Node] that adds XPath and comprehension methods.

This class implements a wrapper around Seq[Node] that adds XPath and comprehension methods.

Attributes

Companion
object
Supertypes
Known subtypes
class Document
class Node
class Elem
class Group
class SpecialNode
class Atom[A]
class PCData
class Text
class Unparsed
class Comment
class EntityRef
class ProcInstr
Show all
case object Null extends MetaData

Essentially, every method in here is a dummy, returning Zero[T]. It provides a backstop for the unusual collection defined by MetaData, sort of a linked list of tails.

Essentially, every method in here is a dummy, returning Zero[T]. It provides a backstop for the unusual collection defined by MetaData, sort of a linked list of tails.

Attributes

Supertypes
trait Singleton
trait Product
trait Mirror
trait Product
class MetaData
trait Serializable
trait Equality
trait Equals
class Object
trait Matchable
class Any
Show all
Self type
Null.type
class PCData(data: String) extends Atom[String]

This class (which is not used by all XML parsers, but always used by the XHTML one) represents parseable character data, which appeared as CDATA sections in the input and is to be preserved as CDATA section in the output.

This class (which is not used by all XML parsers, but always used by the XHTML one) represents parseable character data, which appeared as CDATA sections in the input and is to be preserved as CDATA section in the output.

Attributes

Companion
object
Supertypes
class Atom[String]
class SpecialNode
class Node
class NodeSeq
trait Serializable
trait Equality
trait Seq[Node]
trait SeqOps[Node, Seq, NodeSeq]
trait Iterable[Node]
class AbstractSeq[Node]
trait Seq[Node]
trait Equals
trait SeqOps[Node, Seq, NodeSeq]
trait Int => Node
trait Iterable[Node]
class Object
trait Matchable
class Any
Show all
object PCData

This singleton object contains the applyand unapply methods for convenient construction and deconstruction.

This singleton object contains the applyand unapply methods for convenient construction and deconstruction.

Attributes

Companion
class
Supertypes
class Object
trait Matchable
class Any
Self type
PCData.type
class PrefixedAttribute(val pre: String, val key: String, val value: Seq[Node], val next1: MetaData) extends Attribute

prefixed attributes always have a non-null namespace.

prefixed attributes always have a non-null namespace.

Value parameters

value

the attribute value

Attributes

Companion
object
Supertypes

Attributes

Companion
class
Supertypes
class Object
trait Matchable
class Any
Self type
class PrettyPrinter(width: Int, step: Int, minimizeEmpty: Boolean)

Class for pretty printing. After instantiating, you can use the format() and formatNode() methods to convert XML to a formatted string. The class can be reused to pretty print any number of XML nodes.

Class for pretty printing. After instantiating, you can use the format() and formatNode() methods to convert XML to a formatted string. The class can be reused to pretty print any number of XML nodes.

Value parameters

minimizeEmpty

self-close empty tags

step

indentation

width

the width to fit the output into

Attributes

Note

This class is not threadsafe and should not be accessed by multiple threads at the same time.

Supertypes
class Object
trait Matchable
class Any
case class ProcInstr(target: String, proctext: String) extends SpecialNode

an XML node for processing instructions (PI)

an XML node for processing instructions (PI)

Value parameters

proctext

text contained in this node, may not contain "?>"

target

target name of this PI

Attributes

Supertypes
trait Product
class SpecialNode
class Node
class NodeSeq
trait Serializable
trait Equality
trait Seq[Node]
trait SeqOps[Node, Seq, NodeSeq]
trait Iterable[Node]
class AbstractSeq[Node]
trait Seq[Node]
trait Equals
trait SeqOps[Node, Seq, NodeSeq]
trait Int => Node
trait Iterable[Node]
class Object
trait Matchable
class Any
Show all
object QNode

This object provides an extractor method to match a qualified node with its namespace URI

This object provides an extractor method to match a qualified node with its namespace URI

Attributes

Supertypes
class Object
trait Matchable
class Any
Self type
QNode.type
object Source

Attributes

Supertypes
class Object
trait Matchable
class Any
Self type
Source.type
abstract class SpecialNode extends Node

SpecialNode is a special XML node which represents either text (PCDATA), a comment, a PI, or an entity ref.

SpecialNode is a special XML node which represents either text (PCDATA), a comment, a PI, or an entity ref.

Attributes

Supertypes
class Node
class NodeSeq
trait Serializable
trait Equality
trait Seq[Node]
trait SeqOps[Node, Seq, NodeSeq]
trait Iterable[Node]
class AbstractSeq[Node]
trait Seq[Node]
trait Equals
trait SeqOps[Node, Seq, NodeSeq]
trait Int => Node
trait Iterable[Node]
class Object
trait Matchable
class Any
Show all
Known subtypes
class Atom[A]
class PCData
class Text
class Unparsed
class Comment
class EntityRef
class ProcInstr
Show all
class Text(data: String) extends Atom[String]

The class Text implements an XML node for text (PCDATA). It is used in both non-bound and bound XML representations.

The class Text implements an XML node for text (PCDATA). It is used in both non-bound and bound XML representations.

Value parameters

data

the text contained in this node, may not be null.

Attributes

Companion
object
Supertypes
class Atom[String]
class SpecialNode
class Node
class NodeSeq
trait Serializable
trait Equality
trait Seq[Node]
trait SeqOps[Node, Seq, NodeSeq]
trait Iterable[Node]
class AbstractSeq[Node]
trait Seq[Node]
trait Equals
trait SeqOps[Node, Seq, NodeSeq]
trait Int => Node
trait Iterable[Node]
class Object
trait Matchable
class Any
Show all
object Text

This singleton object contains the applyand unapply methods for convenient construction and deconstruction.

This singleton object contains the applyand unapply methods for convenient construction and deconstruction.

Attributes

Companion
class
Supertypes
class Object
trait Matchable
class Any
Self type
Text.type
object TextBuffer

Attributes

Companion
class
Supertypes
class Object
trait Matchable
class Any
Self type
TextBuffer.type
class TextBuffer

The class TextBuffer is for creating text nodes without surplus whitespace. All occurrences of one or more whitespace in strings appended with the append method will be replaced by a single space character, and leading and trailing space will be removed completely.

The class TextBuffer is for creating text nodes without surplus whitespace. All occurrences of one or more whitespace in strings appended with the append method will be replaced by a single space character, and leading and trailing space will be removed completely.

Attributes

Companion
object
Supertypes
class Object
trait Matchable
class Any
object TopScope extends NamespaceBinding

top level namespace scope. only contains the predefined binding for the "xml" prefix which is bound to "http://www.w3.org/XML/1998/namespace"

top level namespace scope. only contains the predefined binding for the "xml" prefix which is bound to "http://www.w3.org/XML/1998/namespace"

Attributes

Supertypes
trait Serializable
trait Product
trait Equality
trait Equals
class Object
trait Matchable
class Any
Show all
Self type
TopScope.type
abstract class TypeSymbol

Attributes

Supertypes
class Object
trait Matchable
class Any
class Unparsed(data: String) extends Atom[String]

An XML node for unparsed content. It will be output verbatim, all bets are off regarding wellformedness etc.

An XML node for unparsed content. It will be output verbatim, all bets are off regarding wellformedness etc.

Value parameters

data

content in this node, may not be null.

Attributes

Companion
object
Supertypes
class Atom[String]
class SpecialNode
class Node
class NodeSeq
trait Serializable
trait Equality
trait Seq[Node]
trait SeqOps[Node, Seq, NodeSeq]
trait Iterable[Node]
class AbstractSeq[Node]
trait Seq[Node]
trait Equals
trait SeqOps[Node, Seq, NodeSeq]
trait Int => Node
trait Iterable[Node]
class Object
trait Matchable
class Any
Show all
object Unparsed

This singleton object contains the applyand unapply methods for convenient construction and deconstruction.

This singleton object contains the applyand unapply methods for convenient construction and deconstruction.

Attributes

Companion
class
Supertypes
class Object
trait Matchable
class Any
Self type
Unparsed.type
class UnprefixedAttribute(val key: String, val value: Seq[Node], next1: MetaData) extends Attribute

Unprefixed attributes have the null namespace, and no prefix field

Unprefixed attributes have the null namespace, and no prefix field

Attributes

Companion
object
Supertypes

Attributes

Companion
class
Supertypes
class Object
trait Matchable
class Any
Self type
object Utility extends TokenTests

The Utility object provides utility functions for processing instances of bound and not bound XML classes, as well as escaping text nodes.

The Utility object provides utility functions for processing instances of bound and not bound XML classes, as well as escaping text nodes.

Attributes

Supertypes
trait TokenTests
class Object
trait Matchable
class Any
Self type
Utility.type
object XML extends XMLLoader[Elem]

The object XML provides constants, and functions to load and save XML elements. Use this when data binding is not desired, i.e. when XML is handled using Symbol nodes.

The object XML provides constants, and functions to load and save XML elements. Use this when data binding is not desired, i.e. when XML is handled using Symbol nodes.

Attributes

Supertypes
trait XMLLoader[Elem]
class Object
trait Matchable
class Any
Self type
XML.type
object Xhtml

Attributes

Supertypes
class Object
trait Matchable
class Any
Self type
Xhtml.type

Value members

Concrete fields