List of usage examples for org.w3c.dom NodeList item
public Node item(int index);
index
th item in the collection. From source file:Main.java
public static String[] getElementsByTagNameArray(String sTag, Element eElement) { String[] array = new String[0]; NodeList nlList = eElement.getElementsByTagName(sTag); if (nlList != null && nlList.item(0) != null) { NodeList nodes = nlList.item(0).getChildNodes(); array = new String[nodes.getLength()]; for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); array[i] = node.getTextContent(); }// w ww . j a v a 2 s .com } return array; }
From source file:Main.java
private static void print(Node e, String tab) { if (e.getNodeType() == Node.TEXT_NODE) { System.out.println(tab + e.getNodeValue()); return;//from ww w .ja va2 s. c o m } System.out.print(tab + e.getNodeName()); NamedNodeMap as = e.getAttributes(); if (as != null && as.getLength() > 0) { System.out.print(" attributes=["); for (int i = 0; i < as.getLength(); i++) System.out.print((i == 0 ? "" : ", ") + as.item(i)); System.out.print("]"); } System.out.println(); if (e.getNodeValue() != null) System.out.println(tab + " " + e.getNodeValue()); NodeList childs = e.getChildNodes(); for (int i = 0; i < childs.getLength(); i++) print(childs.item(i), tab + " "); }
From source file:Main.java
/** * Given a todo element, we must get the element specified * by the tagName, then must traverse that Node to get the * value.// w ww . ja v a2 s.c o m * @param e * @param tagName * @return the value of the corresponding element */ public static String getValue(Element e, String tagName) { try { // Get node lists of a tag name from an Element NodeList elements = e.getElementsByTagName(tagName); Node node = elements.item(0); NodeList nodes = node.getChildNodes(); // Find a value whose value is non-whitespace String s; for (int i = 0; i < nodes.getLength(); i++) { s = ((Node) nodes.item(i)).getNodeValue().trim(); if (s.equals("") || s.equals("\r")) { continue; } else { return s; } } } catch (Exception ex) { return null; } return null; }
From source file:Main.java
public static String getNodeText(Node parentNode, String nodeName) { Element fstElmnt = (Element) parentNode; NodeList fstNmElmntLst = fstElmnt.getElementsByTagName(nodeName); Element fstNmElmnt = (Element) fstNmElmntLst.item(0); NodeList fstNm = fstNmElmnt.getChildNodes(); String ret = fstNm.item(0).getNodeValue(); return ret;// w w w . j a v a 2 s. c o m }
From source file:Main.java
private static String getTextValue(Element ele, String tagName) { String textVal = null;/* ww w .j a va2 s. c om*/ NodeList nl = ele.getElementsByTagName(tagName); if (nl != null && nl.getLength() > 0) { Element el = (Element) nl.item(0); textVal = el.getFirstChild().getNodeValue(); } return textVal; }
From source file:Main.java
public static void removeElement(Element parent, String tagName) { NodeList nl = parent.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node nd = nl.item(i); if (nd.getNodeName().equals(tagName)) { parent.removeChild(nd);/*from ww w. j a v a 2 s. com*/ } } }
From source file:Main.java
public static String getAttributeInXML(org.w3c.dom.Document doc, String tagName, String attributeName) { String output = ""; org.w3c.dom.NodeList ontologyTypes = doc.getElementsByTagName(tagName); output = ((Element) ontologyTypes.item(0)).getAttribute(attributeName); return output; }
From source file:Main.java
public static HashMap<String, String> getElementsByTagNameHashMap(String sTag, Element eElement) { String value = null;/*from w ww.ja v a 2 s. co m*/ HashMap<String, String> map = new HashMap<String, String>(); NodeList nlList = eElement.getElementsByTagName(sTag); if (nlList != null && nlList.item(0) != null) { NodeList nodes = nlList.item(0).getChildNodes(); for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); map.put(node.getNodeName(), node.getTextContent()); } } return map; }
From source file:Main.java
public static Vector getChildrenElementsByTag(Node node, String name) { Vector result = new Vector(); NodeList nl = node.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) if (nl.item(i) instanceof Element && (((Element) nl.item(i)).getTagName().equals(name) || name == null)) { result.add(nl.item(i));/*w w w . j av a2s.c om*/ //System.out.println(nl.item(i).toString()); //System.out.println(((Element)nl.item(i)).getAttribute("className")); //System.out.println(((Element)nl.item(i)).getTagName()); } return result; }
From source file:Main.java
public static org.w3c.dom.Element getFirstElement(String name, org.w3c.dom.Element root) { NodeList list = root.getElementsByTagName(name); if (list != null) { return (org.w3c.dom.Element) list.item(0); } else/*w w w . ja v a2s . c o m*/ return null; }