private void parse(Document doc) { NodeList nodeList = doc.getChildNodes(); for (int count = 0; count < nodeList.getLength(); count++) { Node node = nodeList.item(count); if (node.getNodeType() == Node.ELEMENT_NODE && node.hasChildNodes()) { parseAttrList(node.getChildNodes()); } } }
public static void removeChildren(Node e) { NodeList list = e.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { Node n = list.item(i); e.removeChild(n); } } private static void getMatchingNodes(Node node, Pattern[] nodePath, int cur, List<Node> res) {
private static String getChildElementValue( final String tagName, final Element element) { Node tagNode = element.getElementsByTagName(tagName).item(0); if ( tagNode == null ) return null; NodeList nodes= tagNode.getChildNodes(); Node node = (Node)nodes.item(0); return node.getNodeValue(); }
public static String text(Node node) { if (node.getNodeType() == Node.TEXT_NODE || node.getNodeType() == Node.CDATA_SECTION_NODE) { return node.getNodeValue(); } if (node.hasChildNodes()) { return text(node.getChildNodes()); } return ""; }
public Stream<XmlNode> children() { NodeList children = this.node.getChildNodes(); return IntStream.range(0, children.getLength()) .mapToObj(children::item) .map(XmlNode::new); }
Mockito.when( node.getNodeName() ).thenReturn( "directory" ); Node child = Mockito.mock( Node.class ); Mockito.when( node.getFirstChild() ).thenReturn( child ); Mockito.when( child.getNodeValue() ).thenReturn( directory ); Mockito.when( jobNode.getChildNodes() ).thenReturn( nodeList );
/** If the child node doesn't exist, it is created. */ private static Node getFirstChildNodeByName (Node parent, String child) { NodeList childNodes = parent.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { if (childNodes.item(i).getNodeName().equals(child)) { return childNodes.item(i); } } Node newNode = parent.getOwnerDocument().createElement(child); if (childNodes.item(0) != null) return parent.insertBefore(newNode, childNodes.item(0)); else return parent.appendChild(newNode); }
private void populateContent(Node item, Object object) { for (int i = 0; i < item.getChildNodes().getLength(); i++) { Node child = item.getChildNodes().item(i); if (child instanceof Text) { setText(object, (Text) child); } } }
/** If the child node doesn't exist, it is created. */ private static Node getFirstChildNodeByName (Node parent, String child) { NodeList childNodes = parent.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { if (childNodes.item(i).getNodeName().equals(child)) { return childNodes.item(i); } } Node newNode = parent.getOwnerDocument().createElement(child); if (childNodes.item(0) != null) return parent.insertBefore(newNode, childNodes.item(0)); else return parent.appendChild(newNode); }
@Override public int jjtGetChildIndex() { org.w3c.dom.Node parent = node.getParentNode(); NodeList childNodes = parent.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { if (node == childNodes.item(i)) { return i; } } throw new IllegalStateException("This node is not a child of its parent: " + node); }
private List<Node> getChildrenTags(final Node node, final String tagName) { List<Node> children = new ArrayList<>(); for (int i = 0; i < node.getChildNodes().getLength(); i++) { Node childNode = node.getChildNodes().item(i); if (childNode.getNodeName().equalsIgnoreCase(tagName)) { children.add(childNode); } } return children; }
public static String getJustText(Node text) { StringBuilder sb = new StringBuilder(); NodeList textElems = text.getChildNodes(); for(int i = 0; i < textElems.getLength(); i++) { Node child = textElems.item(i); String str = child.getTextContent(); //replace single occurrence of \n with " ", double occurrences with a single one. str = str.replaceAll("\n(?!\n)", " "); str = str.replaceAll("_", ""); //bug fix for sentence splitting sb.append(str + " "); } return sb.toString(); }
private void readPackageConfigFromXml(Node node) throws IOException { NodeList childNodes = node.getChildNodes(); if (childNodes.getLength() > 0) { for (int j = 0, n = childNodes.getLength(); j < n; j++) { Node child = childNodes.item(j); if (child.getNodeType() == Node.ELEMENT_NODE) { Element check = (Element) child; String tagName = check.getTagName(); String value = check.getAttribute(ATTR_VALUE); String name = check.getAttribute(ATTR_NAME); if (tagName.equals(ATTR_CONFIG_FIELD)) { mPackageFields.put(name, value); } else { System.err.println("unknown package config tag " + tagName); } } } } }
private void trimNode(final Node node) { NodeList children = node.getChildNodes(); for (int i = children.getLength() - 1; i >= 0; i--) { trimChild(node, children.item(i)); } }
private static String parseTextNode(Node exampleNode) { StringBuffer buffer = new StringBuffer(); for (int i = 0; i < exampleNode.getChildNodes().getLength(); i++) { Node node = exampleNode.getChildNodes().item(i); if (node.getNodeType() == Node.CDATA_SECTION_NODE || node.getNodeType() == Node.TEXT_NODE) { buffer.append(node.getNodeValue()); } } return buffer.toString().trim(); } }
/** * Searches for all immediate children with the given name */ protected static List<Node> getChildrenByName(Node node, String name) { List<Node> matches = new ArrayList<>(); NodeList children = node.getChildNodes(); // search children for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); if (child.getNodeName().equals(name)) { matches.add(child); } } return matches; }
/** Get the string contained in the first text child of the node. */ private static String getNodeValue(final Node node) { NodeList childNodes = node.getChildNodes(); for (int index = 0; index < childNodes.getLength(); index++) { Node childNode = childNodes.item(index); if (childNode.getNodeType() == Node.TEXT_NODE) { return childNode.getNodeValue(); } } return EMPTY_VALUE; }
public static Iterator<Node> findChildren(Node node, String name) { List<Node> result = Lists.newArrayList(); NodeList children = node.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node n = children.item(i); if (name.equals(n.getNodeName())) { result.add(n); } } return result.iterator(); }
private void parseAttrList(NodeList nodeList) { for (int count = 0; count < nodeList.getLength(); count++) { Node tempNode = nodeList.item(count); if (tempNode.getNodeType() == Node.ELEMENT_NODE && tempNode.hasAttributes() && tempNode.hasChildNodes()) { String name = null; NamedNodeMap nodeMap = tempNode.getAttributes(); for (int i = 0; i < nodeMap.getLength(); i++) { Node node = nodeMap.item(i); if (node.getNodeName().equals("name")) { name = node.getNodeValue(); break; } } if (name != null && tempNode.getNodeName().equals("attr")) { parseValues(name, tempNode.getChildNodes()); } else { parseAttrList(tempNode.getChildNodes()); } } } }
private void readLibPatternsFromXml(Node node) throws IOException { NodeList childNodes = node.getChildNodes(); if (childNodes.getLength() > 0) { for (int j = 0, n = childNodes.getLength(); j < n; j++) { Node child = childNodes.item(j); if (child.getNodeType() == Node.ELEMENT_NODE) { Element check = (Element) child; String tagName = check.getTagName(); String value = check.getAttribute(ATTR_VALUE); if (tagName.equals(ATTR_PATTERN)) { addToPatterns(value, mSoFilePattern); } else { System.err.println("unknown dex tag " + tagName); } } } } }