public final class XmlElement extends Object implements org.joda.beans.ImmutableBean
This class is a minimal, lightweight representation of an element in the XML tree. The element has a name, attributes, and either content or children.
Note that this representation does not express all XML features. No support is provided for processing instructions, comments or mixed content. In addition, it is not possible to determine the difference between empty content and no children.
There is no explicit support for namespaces.
When creating instances, the caller may choose to use a convention to represent namespaces.
For example, element and attribute names may use prefixes or the standard QName format.
| Modifier and Type | Method and Description |
|---|---|
boolean |
equals(Object obj)
Checks if this element equals another.
|
Optional<String> |
findAttribute(String attrName)
Finds an attribute by name, or empty if not found.
|
Optional<XmlElement> |
findChild(String childName)
Finds the child element with the specified name, or empty if not found,
throwing an exception if more than one.
|
String |
getAttribute(String attrName)
Gets an attribute by name, throwing an exception if not found.
|
ImmutableMap<String,String> |
getAttributes()
Gets the attributes.
|
XmlElement |
getChild(int index)
Gets a child element by index.
|
XmlElement |
getChild(String childName)
Gets the child element with the specified name, throwing an exception if not found or more than one.
|
ImmutableList<XmlElement> |
getChildren()
Gets the child elements.
|
ImmutableList<XmlElement> |
getChildren(String childName)
Gets the child elements matching the specified name.
|
String |
getContent()
Gets the element content.
|
String |
getName()
Gets the element name.
|
boolean |
hasContent()
Checks if the element has content.
|
int |
hashCode()
Returns a suitable hash code.
|
org.joda.beans.MetaBean |
metaBean() |
static XmlElement |
ofChildren(String name,
List<XmlElement> children)
Obtains an instance with children and no attributes.
|
static XmlElement |
ofChildren(String name,
Map<String,String> attributes,
List<XmlElement> children)
Obtains an instance with children and attributes.
|
static XmlElement |
ofContent(String name,
Map<String,String> attributes,
String content)
Obtains an instance with content and attributes.
|
static XmlElement |
ofContent(String name,
String content)
Obtains an instance with content and no attributes.
|
Stream<XmlElement> |
streamChildren(String childName)
Gets the child elements matching the specified name.
|
String |
toString()
Returns a string summary of the element.
|
public static XmlElement ofContent(String name, String content)
Returns an element representing XML with content, but no children.
name - the element name, not emptycontent - the content, empty if the element has no contentpublic static XmlElement ofContent(String name, Map<String,String> attributes, String content)
Returns an element representing XML with content and attributes but no children.
name - the element name, not emptyattributes - the attributes, empty if the element has no attributescontent - the content, empty if the element has no contentpublic static XmlElement ofChildren(String name, List<XmlElement> children)
Returns an element representing XML with children, but no content.
name - the element name, not emptychildren - the children, empty if the element has no childrenpublic static XmlElement ofChildren(String name, Map<String,String> attributes, List<XmlElement> children)
Returns an element representing XML with children and attributes, but no content.
name - the element name, not emptyattributes - the attributes, empty if the element has no attributeschildren - the children, empty if the element has no childrenpublic String getName()
public String getAttribute(String attrName)
This returns the value of the attribute with the specified name. An exception is thrown if the attribute does not exist.
attrName - the attribute name to findIllegalArgumentException - if the attribute name does not existpublic Optional<String> findAttribute(String attrName)
This returns the value of the attribute with the specified name. If the attribute is not found, optional empty is returned.
attrName - the attribute name to findpublic ImmutableMap<String,String> getAttributes()
This returns all the attributes of this element.
public boolean hasContent()
Content exists if it is non-empty.
public String getContent()
If this element has no content, the empty string is returned.
public XmlElement getChild(int index)
index - the index to findIndexOutOfBoundsException - if the index is invalidpublic ImmutableList<XmlElement> getChildren()
This returns all the children of this element.
public XmlElement getChild(String childName)
This returns the child element with the specified name. An exception is thrown if there is more than one matching child or the child does not exist.
childName - the name to matchIllegalArgumentException - if there is more than one match or no matchespublic Optional<XmlElement> findChild(String childName)
This returns the child element with the specified name. If the element is not found, optional empty is returned.
childName - the name to matchIllegalArgumentException - if there is more than one matchpublic ImmutableList<XmlElement> getChildren(String childName)
This returns all the child elements with the specified name.
childName - the name to matchpublic Stream<XmlElement> streamChildren(String childName)
This returns all the child elements with the specified name.
childName - the name to matchpublic org.joda.beans.MetaBean metaBean()
metaBean in interface org.joda.beans.Beanpublic boolean equals(Object obj)
This compares the entire state of the element, including all children.
public int hashCode()
This includes the entire state of the element, including all children.
Copyright 2009-Present by OpenGamma Inc. and individual contributors
Apache v2 licensed
Additional documentation can be found at strata.opengamma.io.