List of utility methods to do XML Element Get
String | getElementName(Object obj) Returns the element name of the spedified object. XmlType xmlType = obj.getClass().getAnnotation(XmlType.class); if (xmlType != null && xmlType.name() != null) { return xmlType.name(); return null; |
String | getElementNameSansNamespace(Element element) get Element Name Sans Namespace String name = element.getNodeName(); int colonIndex = name.indexOf(':'); if (colonIndex != -1) { return name.substring(colonIndex + 1); return name; |
void | getElementNamespaces(Element element, Set Gets the namespaces declared on the element and adds them to the specified set. NamedNodeMap atts = element.getAttributes(); for (int i = 0; i < atts.getLength(); i++) { Attr att = (Attr) (atts.item(i)); String prefix = att.getPrefix(); if (prefix != null && prefix.equals("xmlns")) { String nsURI = att.getValue(); namespaces.add(nsURI); } else { ... |
String | getElementNamespaceURI(Element element) get Element Namespace URI String namespace = null; if (element == null) { throw new IllegalArgumentException("The element cannot be null"); } else { try { namespace = element.getNamespaceURI(); } catch (NoSuchMethodError e) { String tagName = element.getTagName(); ... |
Element | getElementNS(Element el, String nsuri, String name) get Element NS Node node = el.getFirstChild(); while (node != null) { if (matchNS(node, nsuri, name)) { return (Element) node; node = node.getNextSibling(); return null; ... |
Element | getElementNS(Element elem, String namespaceUri, String localName) Get the first child with the specified name in the specified namespace. NodeList nl = elem.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node node = nl.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { if ((node.getNamespaceURI() != null && node.getNamespaceURI().equals(namespaceUri)) && node.getLocalName().equals(localName)) { return (Element) node; return null; |
Element | getElementNS(Element root, Set Searches for the first sub-element of the given XML element, which has the given local name and whose namespace belongs to the given set. if (root == null) { return null; Node node = root.getFirstChild(); while (node != null) { if ((node instanceof Element) && nsUris.contains(node.getNamespaceURI()) && node.getLocalName().equals(wantedLocalName)) { return (Element) node; ... |
String | getElementPath(Element elem) Get the path to a given element. if (elem == null) return null; StringBuffer sb = new StringBuffer(); Node parent = null; for (Node node = elem; node != null; node = parent) { parent = node.getParentNode(); if ((node instanceof Element) && (parent instanceof Element)) { if (sb.length() > 0) ... |
String | getElementPath(Element element) Returns absolute xpath for specified element, ignores namespaces Node elm = element; String result = elm.getNodeName() + "[" + getElementIndex(elm) + "]"; while (elm.getParentNode() != null && elm.getParentNode().getNodeType() != Node.DOCUMENT_NODE) { elm = elm.getParentNode(); result = elm.getNodeName() + "[" + getElementIndex(elm) + "]/" + result; return "/" + result; |
String | getElementPath(Element element) Returns absolute xpath for specified element, ignores namespaces Node elm = element; String result = elm.getNodeName() + "[" + getElementIndex(elm) + "]"; while (elm.getParentNode() != null && elm.getParentNode().getNodeType() != Node.DOCUMENT_NODE) { elm = elm.getParentNode(); result = elm.getNodeName() + "[" + getElementIndex(elm) + "]/" + result; return "/" + result; |