List of usage examples for org.w3c.dom NodeList getLength
public int getLength();
From source file:Main.java
public static String getTextTag(Node node, String tagName) { Element elem = (Element) node; NodeList list = elem.getElementsByTagName(tagName); if (list.getLength() == 0) return null; Node child = list.item(0).getFirstChild(); if (child.getNodeType() == Node.TEXT_NODE) return child.getNodeValue(); return null;//from w w w .jav a2 s . c o m }
From source file:Main.java
public static Element getLocalElementByTagName(Element context, String tagName) { NodeList childNodes = context.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node node = childNodes.item(i); if (node instanceof Element && tagName.equals(node.getNodeName())) return (Element) node; }/*from ww w . ja v a 2 s . c om*/ return null; }
From source file:Main.java
public static Node GetSimpleTagNode(Document doc, String tag) { NodeList nList2 = doc.getElementsByTagName(tag); for (int temp = 0; temp < nList2.getLength(); temp++) { Node nNode2 = nList2.item(temp); if (nNode2.getNodeType() == Node.ELEMENT_NODE) { return nNode2; } // end if nnode.getnodetype() } //end for int temp return null;/*from ww w. jav a 2s. com*/ }
From source file:Main.java
/** * Search a child node by type/*from ww w. ja v a 2 s . co m*/ * * @param parent the parent node * @param nodeName the node name * @param nodeType the node type for searching * @return Node with the specified name * @see Node * @throws Exception */ public static Node findChildNodeByNameAndType(Node parent, String nodeName, String nodeType) throws Exception { NodeList nl = parent.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node node = nl.item(i); if (node.getNodeName().equals(nodeName)) { String strType = node.getAttributes().getNamedItem("type").getNodeValue(); if (strType.equals(nodeType)) return node; } } return null; }
From source file:Main.java
public static Element[] getElementsByName(Element parent, String name) { List<Node> resList = Lists.newArrayList(); NodeList nl = getNodeList(parent); for (int i = 0; i < nl.getLength(); i++) { Node nd = nl.item(i);/*from ww w . j a v a 2 s . c om*/ if (nd.getNodeName().equals(name)) { resList.add(nd); } } Element[] res = new Element[resList.size()]; for (int i = 0; i < resList.size(); i++) { res[0] = (Element) resList.get(i); } return res; }
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
/** * Returns the first child of a node with a specified name. * //from ww w. jav a 2s . c o m * @param node The parent node. * @param name The name of the child. * @return The first child of <code>node</code> named <code>name</code>. */ public static Node getChildByName(Node node, String name) { try { NodeList list = node.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { if (list.item(i).getNodeName().equals(name)) { return list.item(i); } } return null; } catch (Exception e) { return null; } }
From source file:Main.java
public static String getSimpleElementText(Element node) { if (node == null) return null; StringBuffer sb = new StringBuffer(); NodeList children = node.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); if (child instanceof Text) { sb.append(child.getNodeValue()); }//from w w w .j a va 2 s. co m } return sb.toString(); }
From source file:Main.java
public static final List<Element> getChildElements(Element parent) { final List<Element> childElements = new ArrayList<Element>(); final NodeList childNodes = parent.getChildNodes(); for (int i = 0; i < childNodes.getLength(); ++i) { final Node node = childNodes.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { childElements.add((Element) node); }//from w w w .j ava 2 s .c o m } return childElements; }
From source file:Main.java
/**gets string associated with a node * @param node node to get string for//from w w w . ja va 2s .c o m * @param string associated with the input node*/ static public String getAssociatedString(Node node) { // Has to be a real Node if (node == null) return null; // Must have one and only one string associted with it NodeList children = node.getChildNodes(); if (children.getLength() > 1) { System.out.println("XMLUtils: Node has more than one child"); return null; } Node firstChild = children.item(0); if (firstChild.getNodeType() != Node.TEXT_NODE) { System.out.println("XMLUtils: Node is not a text node"); System.out.println( "XMLUtils: Node = " + firstChild.getNodeName() + ", Parent Node = " + node.getNodeName()); return null; } String stringToReturn = firstChild.getNodeValue().trim(); if (stringToReturn.equals("")) { System.out.println("XMLUtils: Trimmed string is empty"); return null; } else return stringToReturn; }