Class DOMUtils
java.lang.Object
uk.ac.starlink.util.DOMUtils
Provides convenience methods for handling DOMs.
-
Method Summary
Modifier and TypeMethodDescriptionstatic ElementgetChildElementByName(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 ElementgetFirstElementSibling(Node node) Returns the first subsequent sibling of a given node which is an Element.static StringReturns a string representing the plain text content of an element.static StringmapNodeType(short nodeType) Maps a node type, as returned by to a name.static DocumentReturns a newDocumentinstance.static NoderelativizeDOM(Node n, URI baseURI, String attname) Traverses the given DOM, relativising all the URIs in theuriattributes 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
parentwhich is anElementand has the tagnamename, ornullif 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
parentwith tagnamename; ifnameis 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
nodewhich is an Element. Ifnodeitself is an element, that is returned. Ifnodehas no subsequent siblings which are elements, or if it isnull, thennullis returned.
-
relativizeDOM
Traverses the given DOM, relativising all the URIs in theuriattributes 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 newDocumentinstance. This method just does all the tedious business of mucking about with factories for you.- Returns:
- an empty Document
-