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 List<Node> getValidChildNodes(Node node) { List<Node> nl = new ArrayList<>(); NodeList nodeList = node.getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { if (isValidNode(nodeList.item(i))) nl.add(nodeList.item(i));/*from w w w . j a va 2 s . c o m*/ } return nl; }
From source file:Main.java
/** * get the value of an Element in the Xml Document. * finds the first occurance of the element and retrieves its value. * @param root the root Element.//from w ww . j ava 2 s .c o m * @param elemName the name of the element to search for. * @return String the element value or null if not found. */ public static String getElementValue(Element root, String elemName) { NodeList nl = root.getElementsByTagName(elemName); if (null == nl) { return (null); } Node n = nl.item(0); return (n.getFirstChild().getNodeValue().trim()); }
From source file:Main.java
/** * get single element by tag name//from w w w . j a va 2 s.c o m * @param element * @param tag * @return */ public static List<Element> getElementsByTagName(Element element, String tag) { List<Element> elems = new ArrayList<Element>(); NodeList list = element.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { Node node = list.item(i); if (node instanceof Element) { Element e = (Element) node; if (e.getTagName().equals(tag)) { elems.add(e); } } } return elems; }
From source file:Main.java
public static Element getChildWithAttribute(Element parent, String attrName) { NodeList childNodes = parent.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node node = childNodes.item(i); if (node instanceof Element) { Element element = (Element) node; if (!"".equals(element.getAttribute(attrName))) { return element; }//from w ww .j ava 2 s . com } } return null; }
From source file:Main.java
/** Given a person element, must get the element specified by the tagName, then must traverse that Node to get the value.//from w ww .j a va2s. c o m Step1) get Element of name tagName from e Step2) cast element to Node and then traverse it for its non-whitespace, cr/lf value. Step3) return it! NOTE: Element is a subclass of Node @param e an Element @param tagName a tag name @return s the value of a Node */ public static String getValue(Element e, String tagName) { try { //get node lists of a tag name from a 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) { System.out.println(ex); ex.printStackTrace(); } return null; }
From source file:Main.java
public static List<Element> getChildElements(String tagName, Element element) { LinkedList<Element> result = new LinkedList<>(); NodeList nl = element.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node n = nl.item(i); if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(tagName)) { result.add((Element) n); }/*from w ww . j av a 2 s . c om*/ } return result; }
From source file:Main.java
public static Element getChildWithAttributeValue(Element parent, String attrName, String attrValue) { NodeList childNodes = parent.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node node = childNodes.item(i); if (node instanceof Element) { Element element = (Element) node; if (attrValue.equals(element.getAttribute(attrName))) { return element; }/*from www .j a v a 2s . c o m*/ } } return null; }
From source file:Main.java
public static Node getChildNode(Node node, String nodeName) { if (node.getNodeType() == Node.ELEMENT_NODE) { NodeList nodeList = node.getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { Node n = nodeList.item(i); if (n.getNodeName().equals(nodeName)) { return n; }// w w w . j a v a2s.c o m } } return null; }
From source file:Main.java
public static Map<String, String> SimpleDocumentToMap(Document doc) { Map<String, String> simpleMap = new HashMap<String, String>(); //trim off outter layer Node node = doc.getFirstChild(); NodeList nList = node.getChildNodes(); for (int i = 0; i < nList.getLength(); i++) { Node nNode = nList.item(i); if (nNode.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) nNode; if (eElement != null) { simpleMap.put(eElement.getTagName(), eElement.getTextContent()); } // end if eelement } // end if nnode.getnodetype() } //end for int temp return simpleMap; }
From source file:Main.java
public static final String getCDATA(Element elem, boolean trim) { StringBuilder sb = new StringBuilder(); NodeList nl = elem.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node nc = nl.item(i); if (nc.getNodeType() == Node.CDATA_SECTION_NODE) { sb.append(((Text) nc).getData()); } else if (nc.getNodeType() == Node.TEXT_NODE) { String txt = ((Text) nc).getData(); if (trim) { txt = txt.trim();// ww w. j a v a2 s. c o m } sb.append(txt); } } return sb.toString(); }