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 getTextContent(Node node) { StringBuffer buffer = new StringBuffer(); NodeList childList = node.getChildNodes(); for (int i = 0; i < childList.getLength(); i++) { Node child = childList.item(i); if (child.getNodeType() != Node.TEXT_NODE) continue; // skip non-text nodes buffer.append(child.getNodeValue()); }/*from w w w . j a va 2 s .c o m*/ return buffer.toString(); }
From source file:Main.java
public static Element getChild(Element element, String tagName) { NodeList nodes = element.getChildNodes(); for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); if (node instanceof Element) { Element child = (Element) node; if (child.getTagName().equals(tagName)) return child; }//from w w w . j a v a 2 s. com } return null; }
From source file:Main.java
public static NodeList getChildsByTagName(Element root, String name) { final Vector<Node> v = new Vector<Node>(); NodeList nl = root.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node n = nl.item(i); if (n.getNodeType() == Element.ELEMENT_NODE) { Element e = (Element) n; if (name.equals("*") || e.getNodeName().equalsIgnoreCase(name)) v.add(n);/*w w w . j av a2 s .c om*/ } } return new NodeList() { public Node item(int index) { if (index >= v.size() || index < 0) return null; else return v.get(index); } public int getLength() { return v.size(); } }; }
From source file:Main.java
/** * Method to change update date in xml files - used for reloadable issue and auto change on start up. * /*from w ww .j a v a 2s . c om*/ * @param target document * @return updated document * @throws ParseException */ private static Document changeDate(Document target) throws ParseException { NodeList parents = target.getElementsByTagName("g:options"); Element parent = (Element) parents.item(0); //g:options - only 1 element DateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.ENGLISH); Date newT = new Date(); String time = format.format(newT); parent.setAttribute("time", time); return target; }
From source file:Main.java
/** * Returns an DOM node from node list with a specified name. * @param nodes Node list/*from ww w . java 2 s. c o m*/ * @param name Node name * @return Node with specified name, or null if not found * @example * <pre name="test"> * Document d = createDomDocument(); * Node root = d.appendChild( d.createElement("Root") ); * Node n1 = root.appendChild( d.createElement("Node1") ); * Node n2 = root.appendChild( d.createElement("Node2") ); * getNodeByName(root.getChildNodes(), "Node1") === n1; * getNodeByName(root.getChildNodes(), "Node2") === n2; * </pre> */ public static Node getNodeByName(NodeList nodes, String name) { for (int i = 0; i < nodes.getLength(); i++) { if (nodes.item(i).getNodeName().equalsIgnoreCase(name)) return nodes.item(i); } return null; }
From source file:Main.java
static List<Node> children(Node parent, String childNS, String... childLocalName) { List<String> childNames = Arrays.asList(childLocalName); ArrayList<Node> result = new ArrayList<Node>(); NodeList children = parent.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); if (childNS.equals(child.getNamespaceURI()) && childNames.contains(child.getLocalName())) { result.add(child);//from www .ja v a 2 s. c o m } } return result; }
From source file:Main.java
private static List<String> getEmployeeNameWithAge(Document doc, XPath xpath, int age) throws Exception { List<String> list = new ArrayList<>(); XPathExpression expr = xpath.compile("/Employees/Employee[age>" + age + "]/name/text()"); NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET); for (int i = 0; i < nodes.getLength(); i++) list.add(nodes.item(i).getNodeValue()); return list;// w w w . j av a 2 s . com }
From source file:Main.java
public static List<Node> getChildNodes(Node node, String childTag) { ArrayList<Node> nodes = new ArrayList<Node>(); if (node == null) return nodes; NodeList nodeList = node.getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { Node childNode = nodeList.item(i); if (childNode != null && childNode.getLocalName() != null && childNode.getLocalName().equals(childTag)) { nodes.add(childNode);// ww w. j a v a2 s . co m } } return nodes; }
From source file:Main.java
/** * Trys to find a child element in the given parent element. * * @param parent The Element to search in. * @param name The name of the element to search for. * * @return The Element if found, null otherwise. */// www . j ava 2s . c o m public static Element findElement(Element parent, String name) { NodeList l = parent.getChildNodes(); for (int i = 0; i < l.getLength(); i++) { Node n = l.item(i); if (n.getNodeType() == n.ELEMENT_NODE) { Element e = (Element) n; if (e.getNodeName().equals(name)) { return e; } } } return null; }
From source file:Main.java
/** * Get the node set from parent node by the specified name * * @param parent/*from www . j av a2 s . co m*/ * @param name * @return */ public static Element[] getElementsByName(Element parent, String name) { ArrayList resList = new ArrayList(); NodeList nl = getNodeList(parent); for (int i = 0; i < nl.getLength(); i++) { Node nd = nl.item(i); 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; }