List of usage examples for org.w3c.dom Element getTagName
public String getTagName();
From source file:com.bluexml.side.form.utils.DOMUtil.java
/** * Gets all elements of the list that match the tag name. * /*from ww w .j ava2 s. c om*/ * @param elements * the elements * @param tagName * the tag name * * @return the children by tag name */ public static List<Element> getElementsByTagName(List<Element> elements, String tagName) { List<Element> result = new ArrayList<Element>(); for (Element element : elements) { if (StringUtils.equalsIgnoreCase(element.getTagName(), tagName)) { result.add(element); } } return result; }
From source file:com.bluexml.side.form.utils.DOMUtil.java
/** * Gets the immediate children whose tag name matches the given name. * /* w w w. j av a2 s. co m*/ * @param rootElement * the root element * @param name * the required tag name, possibly including namespace prefix and local name * * @return the list of the relevant children. Never <code>null</code>. */ public static List<Element> getChildren(Element rootElement, String name) { List<Element> result = new ArrayList<Element>(); NodeList childNodes = rootElement.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node child = childNodes.item(i); if (child instanceof Element) { Element element = (Element) child; if (StringUtils.equals(element.getTagName(), name)) { result.add(element); } } } return result; }
From source file:DOMTreeTest.java
public static JPanel elementPanel(Element e) { JPanel panel = new JPanel(); panel.add(new JLabel("Element: " + e.getTagName())); final NamedNodeMap map = e.getAttributes(); panel.add(new JTable(new AbstractTableModel() { public int getRowCount() { return map.getLength(); }/*from ww w. j a va 2 s.c o m*/ public int getColumnCount() { return 2; } public Object getValueAt(int r, int c) { return c == 0 ? map.item(r).getNodeName() : map.item(r).getNodeValue(); } })); return panel; }
From source file:XMLUtils.java
/** * Get the next sibling element of a given element. * @param el//from w w w .j ava2s . c o m * @return */ public static Element getNextSibling(Element el) { String tagName = el.getTagName(); if (tagName == null) { return null; } Node n = el.getNextSibling(); while (n != null && (!(n instanceof Element) || !tagName.equals(((Element) n).getTagName()))) { // get the next one n = n.getNextSibling(); } if (n instanceof Element) { return (Element) n; } else { // else, nothing to return return null; } }
From source file:edu.lternet.pasta.portal.search.BrowseGroup.java
public static BrowseGroup generateKeywordCache() { BrowseGroup controlledVocabulary = new BrowseGroup("Controlled Vocabulary"); BrowseGroup lterSiteCache = generateLterSiteCache(); controlledVocabulary.addBrowseGroup(lterSiteCache); try {/*from w ww . jav a2 s. c om*/ String topTermsXML = ControlledVocabularyClient.webServiceFetchTopTerms(); DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); InputStream inputStream = IOUtils.toInputStream(topTermsXML, "UTF-8"); Document document = documentBuilder.parse(inputStream); Element documentElement = document.getDocumentElement(); NodeList documentNodeList = documentElement.getElementsByTagName("term"); for (int i = 0; i < documentNodeList.getLength(); i++) { Node documentNode = documentNodeList.item(i); NodeList childNodes = documentNode.getChildNodes(); String termId = null; String value = null; for (int j = 0; j < childNodes.getLength(); j++) { Node childNode = childNodes.item(j); if (childNode instanceof Element) { Element childElement = (Element) childNode; if (childElement.getTagName().equals("term_id")) { Text text = (Text) childElement.getFirstChild(); termId = text.getData().trim(); } else if (childElement.getTagName().equals("string")) { Text text = (Text) childElement.getFirstChild(); value = text.getData().trim(); } } } BrowseGroup topTerm = new BrowseGroup(value); controlledVocabulary.addBrowseGroup(topTerm); topTerm.setTermId(termId); topTerm.setHasMoreDown("1"); topTerm.addFetchDownElements(); } } catch (Exception e) { logger.error("Exception:\n" + e.getMessage()); e.printStackTrace(); /* * By returning null, we let callers know that there was a problem * refreshing the browse cache, so callers will know not to * overwrite the previous results. */ controlledVocabulary = null; } return controlledVocabulary; }
From source file:Main.java
public static String toString(Element element) { if (element == null) return "null"; Source source = new DOMSource(element); StringWriter stringWriter = new StringWriter(); PrintWriter printWriter = new PrintWriter(stringWriter); Result result = new StreamResult(printWriter); try {/*from ww w . j a v a 2 s. co m*/ Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.transform(source, result); } catch (Exception e) { throw new RuntimeException("couldn't write element '" + element.getTagName() + "' to string", e); } printWriter.close(); return stringWriter.toString(); }
From source file:com.bluexml.side.form.utils.DOMUtil.java
/** * Gets the node value by tag name.//from w ww . j a va2s . c om * * @param node * the node * @param name * the name * @param recursive * the recursive * * @return the node value by tag name */ public static String getNodeValueByTagName(Node node, String name, boolean recursive) { if (node instanceof Document) { return getNodeValueByTagName(((Document) node).getDocumentElement(), name, recursive); } if (node instanceof Element) { Element element = (Element) node; if (StringUtils.equals(element.getTagName(), name)) { return StringUtils.trimToNull(element.getTextContent()); } } NodeList childNodes = node.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node item = childNodes.item(i); if (item instanceof Element) { Element element = (Element) item; if (StringUtils.equals(element.getTagName(), name)) { return StringUtils.trimToNull(element.getTextContent()); } if (recursive) { String nodeValueByTagName = getNodeValueByTagName(item, name, true); if (nodeValueByTagName != null) { return nodeValueByTagName; } } } } return null; }
From source file:Main.java
/** * Get the text content of an element identified by a path, * where the path elements can include an index. The first * path element must not have an index, and its name must * match the name of the starting node. If the starting * node is a Document, the root Element of the document is * used as the starting point. Path elements must be separated * by the slash character. If the path starts with a slash, * the slash is ignored. If the element or attribute identified * by the path is not present as a child of the starting node, * the empty string is returned. If a path element identifies * an attribute, any subsequent path elements are ignored. * A path is in the form: /e1/e2/e3/... or /e1/e2/@attr * Note the slash preceding the attribute's @-sign. * @param node the starting node for the path. The first path * element must match the name of this node. * @param path the path to the target node. * @return the full text value of the target node (including all * descendent text nodes), or the empty string if the target is * not a descendent of the starting node. *///from ww w. ja v a2 s . c o m public static String getTextContent(Node node, String path) { if (node instanceof Document) node = ((Document) node).getDocumentElement(); if (!(node instanceof Element)) return ""; Element el = (Element) node; path = path.replaceAll("\\s", ""); if (path.startsWith("/")) path = path.substring(1); String[] pathElements = path.split("/"); if (!pathElements[0].equals(el.getTagName())) return ""; for (int i = 1; i < pathElements.length; i++) { String pe = pathElements[i]; if (pe.startsWith("@")) { //If this path element identifies an attribute, return it //and ignore any further path elements. return el.getAttribute(pe.substring(1)); } else { //This path element identifies an Element. It may have an index. //Get the index, if present, and get the element name. int n = 0; int k = pe.indexOf("["); int kk = pe.lastIndexOf("]"); if ((k != -1) && (k < kk)) { try { n = Integer.parseInt(pe.substring(k + 1, kk)); } catch (Exception ex) { return ""; } pe = pe.substring(0, k); } else if (k != kk) return ""; //We now have the element name and the index. //Find the identified Element. We have to count //matching elements to find the one identified //by the index. int nn = 0; Node child = el.getFirstChild(); while (child != null) { if ((child.getNodeType() == Node.ELEMENT_NODE) && child.getNodeName().equals(pe)) { if (n == nn) break; nn++; } child = child.getNextSibling(); } //If the child is null, we didn't find the identified Element. if (child == null) return ""; //If we get here, we found it, now look for the next one. el = (Element) child; } } //Okay, we must be at the end of the path, and it must be an Element. //Return the text content of the element. return el.getTextContent(); }
From source file:it.cnr.icar.eric.server.security.authorization.RegistryPolicyFinderModule.java
/** * Loads a policy from the DataHandler, using the specified * <code>PolicyFinder</code> to help with instantiating PolicySets. * * @param DataHandler the DataHandler to load the policy from * @param finder a PolicyFinder used to help in instantiating PolicySets * @param handler an error handler used to print warnings and errors * during parsing/*from w w w . ja v a 2 s. c o m*/ * * @return a policy associated with the specified DataHandler * * @throws RegistryException exception thrown if there is a problem reading the DataHandler's input stream */ private static AbstractPolicy loadPolicy(DataHandler dh, PolicyFinder finder) throws RegistryException { AbstractPolicy policy = null; try { // create the factory DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setIgnoringComments(true); DocumentBuilder db = null; // set the factory to work the way the system requires // we're not doing any validation factory.setNamespaceAware(false); factory.setValidating(false); db = factory.newDocumentBuilder(); // try to load the policy file Document doc = db.parse(dh.getInputStream()); // handle the policy, if it's a known type Element root = doc.getDocumentElement(); String name = root.getTagName(); if (name.equals("Policy")) { policy = Policy.getInstance(root); } else if (name.equals("PolicySet")) { policy = PolicySet.getInstance(root, finder); } else { // this isn't a root type that we know how to handle throw new RegistryException(ServerResourceBundle.getInstance() .getString("message.unknownRootDocumentType", new Object[] { name })); } } catch (Exception e) { log.error(ServerResourceBundle.getInstance().getString("message.FailedToLoadPolicy"), e); throw new RegistryException(e); } return policy; }
From source file:com.impetus.kundera.loader.PersistenceXMLLoader.java
/** * Find persistence units.//w w w. ja v a 2 s .c o m * * @param url * the url * @param defaultTransactionType * the default transaction type * @return the list * @throws Exception * the exception */ public static List<PersistenceUnitMetadata> findPersistenceUnits(final URL url, final String[] persistenceUnits, PersistenceUnitTransactionType defaultTransactionType) throws InvalidConfigurationException { Document doc; try { doc = getDocument(url); } catch (InvalidConfigurationException e) { throw e; } doc.getXmlVersion(); Element top = doc.getDocumentElement(); String versionName = top.getAttribute("version"); NodeList children = top.getChildNodes(); ArrayList<PersistenceUnitMetadata> units = new ArrayList<PersistenceUnitMetadata>(); // parse for persistenceUnitRootInfoURL. for (int i = 0; i < children.getLength(); i++) { if (children.item(i).getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) children.item(i); String tag = element.getTagName(); // look for "persistence-unit" element if (tag.equals("persistence-unit")) { PersistenceUnitMetadata metadata = parsePersistenceUnit(url, persistenceUnits, element, versionName); if (metadata != null) { units.add(metadata); } } } } return units; }