Dart DocumentationxmlXmlElement

XmlElement class

XML element node.

class XmlElement extends XmlParent {

 final XmlName _name;
 final List<XmlAttribute> _attributes;

 XmlElement(XmlName name, Iterable<XmlAttribute> attributes, Iterable<XmlNode> children)
     : super(children), _name = name, _attributes = attributes.toList() {
   for (var attribute in attributes) {
     attribute._parent = this;
   }
 }

 XmlName get name => _name;
 List<XmlAttribute> get attributes => _attributes;

 String getAttribute(String key) {
   var attribute = getAttributeNode(key);
   return attribute != null ? attribute.value : null;
 }

 XmlAttribute getAttributeNode(String key) {
   for (var attribute in attributes) {
     if (attribute.name.local == key) {
       return attribute;
     }
   }
   return null;
 }

 void writeTo(StringBuffer buffer) {
   buffer.write('<');
   name.writeTo(buffer);
   for (var attribute in attributes) {
     buffer.write(' ');
     attribute.writeTo(buffer);
   }
   if (children.isEmpty) {
     buffer.write(' />');
   } else {
     buffer.write('>');
     super.writeTo(buffer);
     buffer.write('</');
     name.writeTo(buffer);
     buffer.write('>');
   }
 }

}

Extends

IterableBase<XmlNode> > IterableBase__XmlWritable > XmlNode > XmlParent > XmlElement

Constructors

new XmlElement(XmlName name, Iterable<XmlAttribute> attributes, Iterable<XmlNode> children) #

XmlElement(XmlName name, Iterable<XmlAttribute> attributes, Iterable<XmlNode> children)
   : super(children), _name = name, _attributes = attributes.toList() {
 for (var attribute in attributes) {
   attribute._parent = this;
 }
}

Properties

final List<XmlAttribute> attributes #

Answer the attribute nodes of the receiver.

docs inherited from XmlNode
List<XmlAttribute> get attributes => _attributes;

final List<XmlNode> children #

inherited from XmlParent

Answer the child nodes of the receiver.

docs inherited from XmlNode
List<XmlNode> get children => _children;

final XmlDocument document #

inherited from XmlNode

Answer the document that contains this node, or null if the node is not part of a document.

XmlDocument get document {
 return parent == null ? null : parent.document;
}

final E first #

inherited from IterableBase

Returns the first element.

If this is empty throws a StateError. Otherwise this method is equivalent to this.elementAt(0)

docs inherited from Iterable<E>
E get first {
 Iterator it = iterator;
 if (!it.moveNext()) {
   throw new StateError("No elements");
 }
 return it.current;
}

final XmlNode firstChild #

inherited from XmlNode

Answer the first child of the receiver or null.

XmlNode get firstChild {
 return children.length > 0 ? children[0] : null;
}

final bool isEmpty #

inherited from IterableBase

Returns true if there is no element in this collection.

docs inherited from Iterable<E>
bool get isEmpty => !iterator.moveNext();

final Iterator<XmlNode> iterator #

inherited from XmlNode

Answer an iterator over the receiver, all attributes and nested children.

Iterator<XmlNode> get iterator {
 var nodes = new List();
 _allAllNodesTo(nodes);
 return nodes.iterator;
}

final E last #

inherited from IterableBase

Returns the last element.

If this is empty throws a StateError.

docs inherited from Iterable<E>
E get last {
 Iterator it = iterator;
 if (!it.moveNext()) {
   throw new StateError("No elements");
 }
 E result;
 do {
   result = it.current;
 } while(it.moveNext());
 return result;
}

final XmlNode lastChild #

inherited from XmlNode

Answer the last child of the receiver or null.

XmlNode get lastChild {
 return children.length > 0 ? children[children.length - 1] : null;
}

final int length #

inherited from IterableBase

Returns the number of elements in this.

Counting all elements may be involve running through all elements and can therefore be slow.

docs inherited from Iterable<E>
int get length {
 int count = 0;
 Iterator it = iterator;
 while (it.moveNext()) {
   count++;
 }
 return count;
}

final XmlName name #

XmlName get name => _name;

final XmlNode nextSibling #

inherited from XmlNode

Answer the next sibling of the receiver or null.

XmlNode get nextSibling {
 if (parent != null) {
   var siblings = parent.children;
   for (var i = 0; i < siblings.length - 1; i++) {
     if (siblings[i] == this) {
       return siblings[i + 1];
     }
   }
 }
 return null;
}

final XmlNode parent #

inherited from XmlNode

Answer the parent node of the receiver, or null if there is none.

XmlNode get parent => _parent;

final XmlNode previousSibling #

inherited from XmlNode

Answer the previous sibling of the receiver or null.

XmlNode get previousSibling {
 if (parent != null) {
   var siblings = parent.children;
   for (var i = 1; i < siblings.length; i++) {
     if (siblings[i] == this) {
       return siblings[i - 1];
     }
   }
 }
 return null;
}

final XmlNode root #

inherited from XmlNode

Answer the root of the subtree in which this node is found, whether that's a document or another element.

XmlNode get root {
 return parent == null ? this : parent.root;
}

final E single #

inherited from IterableBase

Returns the single element in this.

If this is empty or has more than one element throws a StateError.

docs inherited from Iterable<E>
E get single {
 Iterator it = iterator;
 if (!it.moveNext()) throw new StateError("No elements");
 E result = it.current;
 if (it.moveNext()) throw new StateError("More than one element");
 return result;
}

Methods

bool any(bool f(E element)) #

inherited from IterableBase

Returns true if one element of this collection satisfies the predicate f. Returns false otherwise.

docs inherited from Iterable<E>
bool any(bool f(E element)) {
 for (E element in this) {
   if (f(element)) return true;
 }
 return false;
}

bool contains(E element) #

inherited from IterableBase

Check whether the collection contains an element equal to element.

docs inherited from Iterable<E>
bool contains(E element) {
 for (E e in this) {
   if (e == element) return true;
 }
 return false;
}

E elementAt(int index) #

inherited from IterableBase

Returns the indexth element.

If [this] [Iterable] has fewer than index elements throws a RangeError.

Note: if this does not have a deterministic iteration order then the function may simply return any element without any iteration if there are at least index elements in this.

docs inherited from Iterable<E>
E elementAt(int index) {
 if (index is! int || index < 0) throw new RangeError.value(index);
 int remaining = index;
 for (E element in this) {
   if (remaining == 0) return element;
   remaining--;
 }
 throw new RangeError.value(index);
}

bool every(bool f(E element)) #

inherited from IterableBase

Returns true if every elements of this collection satisify the predicate f. Returns false otherwise.

docs inherited from Iterable<E>
bool every(bool f(E element)) {
 for (E element in this) {
   if (!f(element)) return false;
 }
 return true;
}

Iterable expand(Iterable f(E element)) #

inherited from IterableBase

Expand each element of this Iterable into zero or more elements.

The resulting Iterable will run through the elements returned by f for each element of this, in order.

The returned Iterable is lazy, and will call f for each element of this every time it's iterated.

docs inherited from Iterable<E>
Iterable expand(Iterable f(E element)) =>
   new ExpandIterable<E, dynamic>(this, f);

E firstWhere(bool test(E value), {E orElse()}) #

inherited from IterableBase

Returns the first element that satisfies the given predicate f.

If none matches, the result of invoking the orElse function is returned. By default, when orElse is null, a StateError is thrown.

docs inherited from Iterable<E>
E firstWhere(bool test(E value), { E orElse() }) {
 // TODO(floitsch): check that arguments are of correct type?
 for (E element in this) {
   if (test(element)) return element;
 }
 if (orElse != null) return orElse();
 throw new StateError("No matching element");
}

dynamic fold(initialValue, combine(previousValue, E element)) #

inherited from IterableBase

Reduces a collection to a single value by iteratively combining each element of the collection with an existing value using the provided function.

Use initialValue as the initial value, and the function combine to create a new value from the previous one and an element.

Example of calculating the sum of an iterable:

iterable.fold(0, (prev, element) => prev + element);
docs inherited from Iterable<E>
dynamic fold(var initialValue,
            dynamic combine(var previousValue, E element)) {
 var value = initialValue;
 for (E element in this) value = combine(value, element);
 return value;
}

void forEach(void f(E element)) #

inherited from IterableBase

Applies the function f to each element of this collection.

docs inherited from Iterable<E>
void forEach(void f(E element)) {
 for (E element in this) f(element);
}

String getAttribute(String key) #

String getAttribute(String key) {
 var attribute = getAttributeNode(key);
 return attribute != null ? attribute.value : null;
}

XmlAttribute getAttributeNode(String key) #

XmlAttribute getAttributeNode(String key) {
 for (var attribute in attributes) {
   if (attribute.name.local == key) {
     return attribute;
   }
 }
 return null;
}

String join([String separator]) #

inherited from IterableBase

Converts each element to a String and concatenates the strings.

Converts each element to a String by calling Object.toString on it. Then concatenates the strings, optionally separated by the separator string.

docs inherited from Iterable<E>
String join([String separator]) {
 Iterator<E> iterator = this.iterator;
 if (!iterator.moveNext()) return "";
 StringBuffer buffer = new StringBuffer();
 if (separator == null || separator == "") {
   do {
     buffer.write("${iterator.current}");
   } while (iterator.moveNext());
 } else {
   buffer.write("${iterator.current}");
   while (iterator.moveNext()) {
     buffer.write(separator);
     buffer.write("${iterator.current}");
   }
 }
 return buffer.toString();
}

E lastWhere(bool test(E value), {E orElse()}) #

inherited from IterableBase

Returns the last element that satisfies the given predicate f.

If none matches, the result of invoking the orElse function is returned. By default, when orElse is null, a StateError is thrown.

docs inherited from Iterable<E>
E lastWhere(bool test(E value), {E orElse()}) {
 // TODO(floitsch): check that arguments are of correct type?
 E result = null;
 bool foundMatching = false;
 for (E element in this) {
   if (test(element)) {
     result = element;
     foundMatching = true;
   }
 }
 if (foundMatching) return result;
 if (orElse != null) return orElse();
 throw new StateError("No matching element");
}

Iterable map(f(E element)) #

inherited from IterableBase

Returns a lazy Iterable where each element e of this is replaced by the result of f(e).

This method returns a view of the mapped elements. As long as the returned Iterable is not iterated over, the supplied function f will not be invoked. The transformed elements will not be cached. Iterating multiple times over the the returned Iterable will invoke the supplied function f multiple times on the same element.

docs inherited from Iterable<E>
Iterable map(f(E element)) => new MappedIterable<E, dynamic>(this, f);

E reduce(E combine(E value, E element)) #

inherited from IterableBase

Reduces a collection to a single value by iteratively combining elements of the collection using the provided function.

Example of calculating the sum of an iterable:

iterable.reduce((value, element) => value + element);
docs inherited from Iterable<E>
E reduce(E combine(E value, E element)) {
 Iterator<E> iterator = this.iterator;
 if (!iterator.moveNext()) {
   throw new StateError("No elements");
 }
 E value = iterator.current;
 while (iterator.moveNext()) {
   value = combine(value, iterator.current);
 }
 return value;
}

E singleWhere(bool test(E value)) #

inherited from IterableBase

Returns the single element that satisfies f. If no or more than one element match then a StateError is thrown.

docs inherited from Iterable<E>
E singleWhere(bool test(E value)) {
 // TODO(floitsch): check that argument is of correct type?
 E result = null;
 bool foundMatching = false;
 for (E element in this) {
   if (test(element)) {
     if (foundMatching) {
       throw new StateError("More than one matching element");
     }
     result = element;
     foundMatching = true;
   }
 }
 if (foundMatching) return result;
 throw new StateError("No matching element");
}

Iterable<E> skip(int n) #

inherited from IterableBase

Returns an Iterable that skips the first n elements.

If this has fewer than n elements, then the resulting Iterable will be empty.

docs inherited from Iterable<E>
Iterable<E> skip(int n) {
 return new SkipIterable<E>(this, n);
}

Iterable<E> skipWhile(bool test(E value)) #

inherited from IterableBase

Returns an Iterable that skips elements while test is satisfied.

The filtering happens lazily. Every new Iterator of the returned Iterable will iterate over all elements of this. As long as the iterator's elements do not satisfy test they are discarded. Once an element satisfies the test the iterator stops testing and uses every element unconditionally.

docs inherited from Iterable<E>
Iterable<E> skipWhile(bool test(E value)) {
 return new SkipWhileIterable<E>(this, test);
}

Iterable<E> take(int n) #

inherited from IterableBase

Returns an Iterable with at most n elements.

The returned Iterable may contain fewer than n elements, if this contains fewer than n elements.

docs inherited from Iterable<E>
Iterable<E> take(int n) {
 return new TakeIterable<E>(this, n);
}

Iterable<E> takeWhile(bool test(E value)) #

inherited from IterableBase

Returns an Iterable that stops once test is not satisfied anymore.

The filtering happens lazily. Every new Iterator of the returned Iterable will start iterating over the elements of this. When the iterator encounters an element e that does not satisfy test, it discards e and moves into the finished state. That is, it will not ask or provide any more elements.

docs inherited from Iterable<E>
Iterable<E> takeWhile(bool test(E value)) {
 return new TakeWhileIterable<E>(this, test);
}

List<E> toList({bool growable: true}) #

inherited from IterableBase

Creates a List containing the elements of this Iterable.

The elements will be in iteration order. The list is fixed-length if growable is false.

docs inherited from Iterable<E>
List<E> toList({ bool growable: true }) =>
   new List<E>.from(this, growable: growable);

Set<E> toSet() #

inherited from IterableBase

Creates a Set containing the elements of this Iterable.

docs inherited from Iterable<E>
Set<E> toSet() => new Set<E>.from(this);

String toString() #

Answer a print string of the receiver.

String toString() {
 var buffer = new StringBuffer();
 writeTo(buffer);
 return buffer.toString();
}

Iterable<E> where(bool f(E element)) #

inherited from IterableBase

Returns a lazy Iterable with all elements that satisfy the predicate f.

This method returns a view of the mapped elements. As long as the returned Iterable is not iterated over, the supplied function f will not be invoked. Iterating will not cache results, and thus iterating multiple times over the the returned Iterable will invoke the supplied function f multiple times on the same element.

docs inherited from Iterable<E>
Iterable<E> where(bool f(E element)) => new WhereIterable<E>(this, f);

void writeTo(StringBuffer buffer) #

Writes the XML string of the receiver to a {@code buffer}.

docs inherited from IterableBase__XmlWritable
void writeTo(StringBuffer buffer) {
 buffer.write('<');
 name.writeTo(buffer);
 for (var attribute in attributes) {
   buffer.write(' ');
   attribute.writeTo(buffer);
 }
 if (children.isEmpty) {
   buffer.write(' />');
 } else {
   buffer.write('>');
   super.writeTo(buffer);
   buffer.write('</');
   name.writeTo(buffer);
   buffer.write('>');
 }
}