Package uk.ac.starlink.util
Class DOMUtils
java.lang.Object
uk.ac.starlink.util.DOMUtils
Provides convenience methods for handling DOMs.
-
Method Summary
Modifier and TypeMethodDescriptionstatic Element
getChildElementByName
(Node parent, String name) Returns the first child element of a node which has a given name.static Element[]
getChildElementsByName
(Node parent, String name) Returns all child elements of a node with a given name.static Element
getFirstElementSibling
(Node node) Returns the first subsequent sibling of a given node which is an Element.static String
Returns a string representing the plain text content of an element.static String
mapNodeType
(short nodeType) Maps a node type, as returned by to a name.static Document
Returns a newDocument
instance.static Node
relativizeDOM
(Node n, URI baseURI, String attname) Traverses the given DOM, relativising all the URIs in theuri
attributes of eachElement
.
-
Method Details
-
getChildElementByName
Returns the first child element of a node which has a given name.- Parameters:
parent
- the node whose children are to be searchedname
- the name of the element being searched for- Returns:
- the first child of
parent
which is anElement
and has the tagnamename
, ornull
if none match
-
getChildElementsByName
Returns all child elements of a node with a given name.- Parameters:
parent
- the node whose children are to be searchedname
- the name of the element being searched for- Returns:
- array of child elements of
parent
with tagnamename
; ifname
is null, all child elements are returned
-
getTextContent
Returns a string representing the plain text content of an element. Any comments, attributes, elements or other non-text children are ignored, and all CDATA and Text nodes are merged to give a single string.- Parameters:
el
- the element whose text content is wanted- Returns:
- the pure text content. If there is none, an empty string is returned.
-
getFirstElementSibling
Returns the first subsequent sibling of a given node which is an Element. This is useful for naviating a DOM as a tree of elements when the presence of text or attribute children is a distraction.- Parameters:
node
- the node whose siblings (including itself) you are interested in. May benull
- Returns:
- the first sibling of
node
which is an Element. Ifnode
itself is an element, that is returned. Ifnode
has no subsequent siblings which are elements, or if it isnull
, thennull
is returned.
-
relativizeDOM
Traverses the given DOM, relativising all the URIs in theuri
attributes of eachElement
.The (uri-attribute) nodes in the input DOM are modified by this method; if this is a problem, use
Node.cloneNode(boolean)
first.- Parameters:
n
- a node containing the DOM whose URIs are to be relativized. If this is null, the method immediately returns nullbaseURI
- the URI relative to which the DOM is to be relativised. If this is null, then the input node is immediately returned unchanged.attname
- the attribute name to be used. If null, this defaults touri
- Returns:
- the input node
- See Also:
-
mapNodeType
Maps a node type, as returned by to a name. The node types returned byNode.getNodeType()
are numeric and are therefore inconveniently opaque.- Parameters:
nodeType
- a numeric Node type, one of the node type constants defined inNode
- Returns:
- a string name for the type
-
newDocument
Returns a newDocument
instance. This method just does all the tedious business of mucking about with factories for you.- Returns:
- an empty Document
-