List of usage examples for org.w3c.dom Element getNodeName
public String getNodeName();
From source file:Main.java
public static Element getElement(Element root, String name) throws Exception { NodeList elements = root.getElementsByTagName(name); if (elements.getLength() > 0) { return (Element) elements.item(0); }//from w ww.j a va 2s . c o m throw new Exception("Child element '" + name + "' to parent " + root.getNodeName() + " not found."); }
From source file:com.msopentech.odatajclient.engine.data.atom.AtomDeserializer.java
public static AtomFeed feed(final Element input) { if (!ODataConstants.ATOM_ELEM_FEED.equals(input.getNodeName())) { return null; }/*from w w w .ja va 2 s. co m*/ final AtomFeed feed = new AtomFeed(); common(input, feed); final List<Element> entries = XMLUtils.getChildElements(input, ODataConstants.ATOM_ELEM_ENTRY); for (Element entry : entries) { feed.addEntry(entry(entry)); } final List<Element> links = XMLUtils.getChildElements(input, ODataConstants.ATOM_ELEM_LINK); for (Element link : links) { if (ODataConstants.NEXT_LINK_REL.equals(link.getAttribute(ODataConstants.ATTR_REL))) { feed.setNext(URI.create(link.getAttribute(ODataConstants.ATTR_HREF))); } } final List<Element> counts = XMLUtils.getChildElements(input, ODataConstants.ATOM_ATTR_COUNT); if (!counts.isEmpty()) { try { feed.setCount(Integer.parseInt(counts.get(0).getTextContent())); } catch (Exception e) { LOG.error("Could not parse $inlinecount {}", counts.get(0).getTextContent(), e); } } return feed; }
From source file:Main.java
/** * Find the xpath element under the given root. As the xpath requirement is very * simple, so we avoid using XPath API//from www.j a va2 s. c om * @param root - the root element that search begins * @param xpath - the path from the root * @return - the xpath defined element under the given root. */ public static Element findXPathElement(Element root, String xpath) { if (root == null) return null; if (xpath == null || xpath.trim().equals("")) return root; xpath = toRelativePath(xpath, root.getNodeName()); StringTokenizer st = new StringTokenizer(xpath, "/", false); String sitem; Element item = root; NodeList list; boolean first = true; while (st.hasMoreTokens()) { sitem = st.nextToken(); if (first && sitem.equals(item.getNodeName())) { first = false; } else { list = item.getElementsByTagName(sitem); if (list.getLength() < 1) return null; item = (Element) (list.item(0)); } } return item; }
From source file:Main.java
private static void collectResults(Element element, String[] path, int index, Collection destination) { // If we matched all the way to the leaf of the path, add the element to the destination.... String elemName = element.getNodeName(); int lastColon = elemName.lastIndexOf(':'); if (lastColon > 0) { elemName = elemName.substring(0, lastColon); }//from ww w . ja va 2 s .co m if (!elemName.equals(path[index])) return; // No match in this subtree if (index >= path.length - 1) { destination.add(element); return; } // OK, we have a match on the path so far, now check rest of the path (possibly none) Node child = element.getFirstChild(); while (child != null) { if (child.getNodeType() == Node.ELEMENT_NODE) { // Recursive step, try t collectResults((Element) child, path, index + 1, destination); } child = child.getNextSibling(); } }
From source file:Main.java
/** * Checks if Element Node is same as a Element name String *//* w w w.j a va 2 s . c o m*/ public static boolean isStrElementNode(String elementName, Node elementNode, boolean ignoreCase) { if ((elementNode == null) || (elementName == null) || (elementName.trim().equals("")) || (elementNode.getNodeType() != Node.ELEMENT_NODE)) return false; StringTokenizer tokenizer = new StringTokenizer(":"); int numTokens = tokenizer.countTokens(); if (numTokens == 1) { String name = (String) tokenizer.nextElement(); Element element = (Element) elementNode; if (element.getNamespaceURI() != null) return false; if (ignoreCase) return element.getNodeName().trim().equalsIgnoreCase(elementName); return element.getNodeName().trim().equals(elementName); } else if (numTokens == 2) { String namespace = (String) tokenizer.nextElement(); String localName = (String) tokenizer.nextElement(); Element element = (Element) elementNode; if (element.getNamespaceURI() == null) return false; if (ignoreCase) return ((element.getLocalName().trim().equalsIgnoreCase(localName)) && (element.getNamespaceURI().equalsIgnoreCase(namespace.trim()))); return ((element.getLocalName().trim().equals(localName)) && (element.getNamespaceURI().equals(namespace.trim()))); } else return false; }
From source file:fll.xml.XMLUtils.java
/** * Check if an element describes an enumerated goal or not. * /*w w w . j a v a2s . com*/ * @param element the goal element * @return if the element represents an enumerated goal */ public static boolean isEnumeratedGoal(final Element element) { if (!"goal".equals(element.getNodeName())) { // not a goal element return false; } final Iterator<Element> values = new NodelistElementCollectionAdapter(element.getElementsByTagName("value")) .iterator(); return values.hasNext(); }
From source file:Main.java
public static String getRequiredAttribute(Element element, Enum<?> attribute) { final String value = getAttribute(element, attribute); if (value != null) { return value; }//from w w w . j a v a 2 s. c om throw new IllegalArgumentException("attribute " + getXmlName(attribute) + " is missing or empty in XML element " + element.getNodeName()); }
From source file:Main.java
public static Element getSingleChildElement(Element parent, Enum<?> child) { final Element el = getSingleOptionalChildElement(parent, child); if (el != null) { return el; }/*w ww . j a v a 2 s .c o m*/ throw new IllegalArgumentException("mandatory (single) child element " + getXmlName(child) + " missing for XML element " + parent.getNodeName()); }
From source file:Main.java
/** * @param xml//www.jav a 2s . co m * The Element whose QName will be returned. * @return The QName of the given Element definition. */ public static QName getElementQName(final Element xml) { final String uri = xml.getNamespaceURI(); final String prefix = xml.getPrefix(); final String name = xml.getLocalName(); // // support for DOM Level 1 - no NS concept // if (name == null) return new QName(xml.getNodeName()); // // prefix is not required, but it CANNOT be null // if (prefix != null && prefix.length() > 0) return new QName(uri, name, prefix); return new QName(uri, name); }
From source file:Main.java
static String getChildNodeValue(Element root, String childNodeName, String defaultValue) throws SAXException { NodeList nl = root.getElementsByTagName(childNodeName); if (nl.getLength() == 1) { return getTextValue(nl.item(0)).trim(); } else if (defaultValue == null) { throw new SAXException("Node <" + root.getNodeName() + "> has no child named <" + childNodeName + ">"); } else {/*from w ww .ja v a 2 s . c o m*/ return defaultValue; } }