- Common ways to obtain Node
private void myMethod () {Node n =
NodeList children;children.item(index)
Element element;element.getFirstChild()
NamedNodeMap attributes;String name;attributes.getNamedItem(name)
- Smart code suggestions by Codota
}
String url = "http://stackoverflow.com/questions/3152138"; Document document = new Tidy().parseDOM(new URL(url).openStream(), null); XPath xpath = XPathFactory.newInstance().newXPath(); Node question = (Node) xpath.compile("//*[@id='question']//*[contains(@class,'post-text')]//p[1]").evaluate(document, XPathConstants.NODE); System.out.println("Question: " + question.getFirstChild().getNodeValue()); NodeList answerers = (NodeList) xpath.compile("//*[@id='answers']//*[contains(@class,'user-details')]//a[1]").evaluate(document, XPathConstants.NODESET); for (int i = 0; i < answerers.getLength(); i++) { System.out.println("Answerer: " + answerers.item(i).getFirstChild().getNodeValue()); }
private void parse(Document document) { Element element = document.getDocumentElement(); NodeList statusNode = element.getElementsByTagName("status"); for (int i = 0; i < statusNode.getLength(); i++) { Node node = statusNode.item(i); String value = node.getFirstChild().getNodeValue(); int statusCode = Integer.valueOf(value.substring(value.indexOf(" "), value.lastIndexOf(" ")).trim()); String statusText = value.substring(value.lastIndexOf(" ")); status = new HttpStatusWrapper(status, statusText, statusCode); } }
private String getValue(Node node) { if (node == null) { return null; } StringBuilder sb = new StringBuilder(); Node n = node.getFirstChild(); while (n != null) { if (n.getNodeType() == Node.TEXT_NODE) { sb.append(n.getNodeValue()); } n = n.getNextSibling(); } return sb.toString(); }
/** * Recursively removes all comment nodes from the subtree. * * @see #simplify */ static public void removeComments(Node parent) { Node child = parent.getFirstChild(); while (child != null) { Node nextSibling = child.getNextSibling(); if (child.getNodeType() == Node.COMMENT_NODE) { parent.removeChild(child); } else if (child.hasChildNodes()) { removeComments(child); } child = nextSibling; } }
protected void readDataInputAssociation(org.w3c.dom.Node xmlNode, Map<String, String> forEachNodeInputAssociation) { // sourceRef org.w3c.dom.Node subNode = xmlNode.getFirstChild(); if ("sourceRef".equals(subNode.getNodeName())) { String source = subNode.getTextContent(); // targetRef subNode = subNode.getNextSibling(); String target = subNode.getTextContent(); forEachNodeInputAssociation.put(target, source); } }
String entityName = item.getNodeName(); Node firstChild = item.getFirstChild(); if (firstChild != null) { result = result.replaceAll(Matcher.quoteReplacement(firstChild.getNodeValue()), "&" + entityName + ";"); } else {
if (doc != null) { core.getLoginInfo().put(StorageLoginInfoEnum.skey.getKey(), doc.getElementsByTagName(StorageLoginInfoEnum.skey.getKey()).item(0).getFirstChild() .getNodeValue()); core.getLoginInfo().put(StorageLoginInfoEnum.wxsid.getKey(), doc.getElementsByTagName(StorageLoginInfoEnum.wxsid.getKey()).item(0).getFirstChild() .getNodeValue()); core.getLoginInfo().put(StorageLoginInfoEnum.wxuin.getKey(), doc.getElementsByTagName(StorageLoginInfoEnum.wxuin.getKey()).item(0).getFirstChild() .getNodeValue()); core.getLoginInfo().put(StorageLoginInfoEnum.pass_ticket.getKey(), doc.getElementsByTagName(StorageLoginInfoEnum.pass_ticket.getKey()).item(0).getFirstChild() .getNodeValue());
private String displayMetadata(Node node, int level) { final NamedNodeMap map = node.getAttributes(); if (map != null) { final Node keyword = map.getNamedItem("keyword"); if (keyword != null && tag.equals(keyword.getNodeValue())) { final Node text = map.getNamedItem("value"); if (text != null) { return text.getNodeValue(); } } } Node child = node.getFirstChild(); // children, so close current tag while (child != null) { // print children recursively final String result = displayMetadata(child, level + 1); if (result != null) { return result; } child = child.getNextSibling(); } return null; }
private static AceCharSeq parseCharSeq(Node node) { Node child = getChildByName(node, "charseq"); String start = getAttributeValue(child, "START"); String end = getAttributeValue(child, "END"); String text = child.getFirstChild().getNodeValue(); return new AceCharSeq(text, Integer.parseInt(start), Integer.parseInt(end)); }
public void executeWorkItem(WorkItem workItem, WorkItemManager mgr) { assertEquals("id", ((org.w3c.dom.Node) workItem .getParameter("coId")).getNodeName()); assertEquals("some text", ((org.w3c.dom.Node) workItem .getParameter("coId")).getFirstChild() .getTextContent()); }
protected void printChildren(Node parent, Map namespaces) { for (Node node = parent.getFirstChild(); node != null; node = node.getNextSibling()) { print(node, namespaces, false); } }
public String getSample( String strFunctionName, String strFunctionNameWithArgs ) { String sRC = "// Sorry, no Script available for " + strFunctionNameWithArgs; NodeList nl = dom.getElementsByTagName( "jsFunction" ); for ( int i = 0; i < nl.getLength(); i++ ) { if ( nl.item( i ).getAttributes().getNamedItem( "name" ).getNodeValue().equals( strFunctionName ) ) { Node elSample = ( (Element) nl.item( i ) ).getElementsByTagName( "sample" ).item( 0 ); if ( elSample.hasChildNodes() ) { return ( elSample.getFirstChild().getNodeValue() ); } } } return sRC; }
/** * Method getStrFromNode * * @param xpathnode * @return the string for the node. */ public static String getStrFromNode(Node xpathnode) { if (xpathnode.getNodeType() == Node.TEXT_NODE) { // we iterate over all siblings of the context node because eventually, // the text is "polluted" with pi's or comments StringBuilder sb = new StringBuilder(); for (Node currentSibling = xpathnode.getParentNode().getFirstChild(); currentSibling != null; currentSibling = currentSibling.getNextSibling()) { if (currentSibling.getNodeType() == Node.TEXT_NODE) { sb.append(((Text) currentSibling).getData()); } } return sb.toString(); } else if (xpathnode.getNodeType() == Node.ATTRIBUTE_NODE) { return xpathnode.getNodeValue(); } else if (xpathnode.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) { return xpathnode.getNodeValue(); } return null; }
private Object get(Node de) { if (!de.hasChildNodes()) { return ""; } else if (de.getChildNodes().getLength() == 1 && de.getFirstChild() instanceof Text) { return de.getTextContent(); } else { NodeList childNodes = de.getChildNodes(); Map<String, Object> map = new HashMap<>(); for (int i = 0; i < childNodes.getLength(); i++) { Node item = childNodes.item(i); if (!(item instanceof Text)) { put(item, map); } } return map; } }
/** * Recursively removes all processing instruction nodes from the subtree. * * @see #simplify */ static public void removePIs(Node parent) { Node child = parent.getFirstChild(); while (child != null) { Node nextSibling = child.getNextSibling(); if (child.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) { parent.removeChild(child); } else if (child.hasChildNodes()) { removePIs(child); } child = nextSibling; } }
protected void readDataOutputAssociation(org.w3c.dom.Node xmlNode, Map<String, String> forEachNodeOutputAssociation) { // sourceRef org.w3c.dom.Node subNode = xmlNode.getFirstChild(); if ("sourceRef".equals(subNode.getNodeName())) { String source = subNode.getTextContent(); // targetRef subNode = subNode.getNextSibling(); String target = subNode.getTextContent(); forEachNodeOutputAssociation.put(source, target); } }
public void executeWorkItem(WorkItem workItem, WorkItemManager mgr) { Object coIdParamObj = workItem.getParameter("coId"); assertEquals("mydoc", ((Element) coIdParamObj).getNodeName()); assertEquals("mynode", ((Element) workItem.getParameter("coId")).getFirstChild().getNodeName()); assertEquals("user", ((Element) workItem.getParameter("coId")) .getFirstChild().getFirstChild() .getNodeName()); assertEquals("hello world", ((Element) workItem.getParameter("coId")) .getFirstChild().getFirstChild() .getAttributes().getNamedItem("hello") .getNodeValue()); }
@Test public void testDefaulSerializerXmlsSerializersValueHasWhitespaces() throws Exception { final URL resource = XmlConfigurationTest.class.getResource("/configs/default-serializer.xml"); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(new File(resource.toURI())); NodeList nList = doc.getElementsByTagName("ehcache:serializer"); assertThat(nList.item(2).getFirstChild().getNodeValue(), containsString(" ")); assertThat(nList.item(2).getFirstChild().getNodeValue(), containsString("\n")); assertThat(nList.item(3).getFirstChild().getNodeValue(), containsString(" ")); assertThat(nList.item(3).getFirstChild().getNodeValue(), containsString("\n")); nList = doc.getElementsByTagName("ehcache:key-type"); assertThat(nList.item(0).getFirstChild().getNodeValue(), containsString(" ")); assertThat(nList.item(0).getFirstChild().getNodeValue(), containsString("\n")); assertThat(nList.item(1).getFirstChild().getNodeValue(), containsString(" ")); assertThat(nList.item(1).getFirstChild().getNodeValue(), containsString("\n")); nList = doc.getElementsByTagName("ehcache:value-type"); assertThat(nList.item(0).getFirstChild().getNodeValue(), containsString(" ")); assertThat(nList.item(0).getFirstChild().getNodeValue(), containsString("\n")); assertThat(nList.item(1).getFirstChild().getNodeValue(), containsString(" ")); assertThat(nList.item(1).getFirstChild().getNodeValue(), containsString("\n")); }
private String getComplexTypeNameFromChildren(Node localComplexTypeRootNode, String elementNameWithoutNamespace) { if(localComplexTypeRootNode == null) { return ""; } Node node = localComplexTypeRootNode.getFirstChild(); String complexTypeName = ""; while (node != null) { if ( node instanceof Element && "element".equals(node.getLocalName())) { Node nameAttribute = getNameOrRefElement(node); if (nameAttribute.getNodeValue().equals(elementNameWithoutNamespace)) { Node complexTypeAttribute = node.getAttributes().getNamedItem("type"); if (complexTypeAttribute!=null) { complexTypeName = complexTypeAttribute.getNodeValue(); break; } } } node = node.getNextSibling(); } return complexTypeName; }
/** * Construct a ValidValue given the XML node and the encodingType. * * @param node that contains the validValue * @param encodingType for the enum */ public ValidValue(final Node node, final PrimitiveType encodingType) { name = getAttributeValue(node, "name"); description = getAttributeValueOrNull(node, "description"); value = PrimitiveValue.parse(node.getFirstChild().getNodeValue(), encodingType); sinceVersion = Integer.parseInt(getAttributeValue(node, "sinceVersion", "0")); deprecated = Integer.parseInt(getAttributeValue(node, "deprecated", "0")); checkForValidName(node, name); }