org.w3c.dom.Node.getFirstChild()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(11.4k)|赞(0)|评价(0)|浏览(724)

本文整理了Java中org.w3c.dom.Node.getFirstChild()方法的一些代码示例,展示了Node.getFirstChild()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Node.getFirstChild()方法的具体详情如下:
包路径:org.w3c.dom.Node
类名称:Node
方法名:getFirstChild

Node.getFirstChild介绍

[英]The first child of this node. If there is no such node, this returns null.
[中]此节点的第一个子节点。如果没有这样的节点,则返回null

代码示例

代码示例来源:origin: stackoverflow.com

  1. String url = "http://stackoverflow.com/questions/3152138";
  2. Document document = new Tidy().parseDOM(new URL(url).openStream(), null);
  3. XPath xpath = XPathFactory.newInstance().newXPath();
  4. Node question = (Node) xpath.compile("//*[@id='question']//*[contains(@class,'post-text')]//p[1]").evaluate(document, XPathConstants.NODE);
  5. System.out.println("Question: " + question.getFirstChild().getNodeValue());
  6. NodeList answerers = (NodeList) xpath.compile("//*[@id='answers']//*[contains(@class,'user-details')]//a[1]").evaluate(document, XPathConstants.NODESET);
  7. for (int i = 0; i < answerers.getLength(); i++) {
  8. System.out.println("Answerer: " + answerers.item(i).getFirstChild().getNodeValue());
  9. }

代码示例来源:origin: AsyncHttpClient/async-http-client

  1. private void parse(Document document) {
  2. Element element = document.getDocumentElement();
  3. NodeList statusNode = element.getElementsByTagName("status");
  4. for (int i = 0; i < statusNode.getLength(); i++) {
  5. Node node = statusNode.item(i);
  6. String value = node.getFirstChild().getNodeValue();
  7. int statusCode = Integer.valueOf(value.substring(value.indexOf(" "), value.lastIndexOf(" ")).trim());
  8. String statusText = value.substring(value.lastIndexOf(" "));
  9. status = new HttpStatusWrapper(status, statusText, statusCode);
  10. }
  11. }

代码示例来源:origin: oracle/opengrok

  1. private String getValue(Node node) {
  2. if (node == null) {
  3. return null;
  4. }
  5. StringBuilder sb = new StringBuilder();
  6. Node n = node.getFirstChild();
  7. while (n != null) {
  8. if (n.getNodeType() == Node.TEXT_NODE) {
  9. sb.append(n.getNodeValue());
  10. }
  11. n = n.getNextSibling();
  12. }
  13. return sb.toString();
  14. }

代码示例来源:origin: org.freemarker/freemarker

  1. /**
  2. * Recursively removes all comment nodes from the subtree.
  3. *
  4. * @see #simplify
  5. */
  6. static public void removeComments(Node parent) {
  7. Node child = parent.getFirstChild();
  8. while (child != null) {
  9. Node nextSibling = child.getNextSibling();
  10. if (child.getNodeType() == Node.COMMENT_NODE) {
  11. parent.removeChild(child);
  12. } else if (child.hasChildNodes()) {
  13. removeComments(child);
  14. }
  15. child = nextSibling;
  16. }
  17. }

代码示例来源:origin: kiegroup/jbpm

  1. protected void readDataInputAssociation(org.w3c.dom.Node xmlNode, Map<String, String> forEachNodeInputAssociation) {
  2. // sourceRef
  3. org.w3c.dom.Node subNode = xmlNode.getFirstChild();
  4. if ("sourceRef".equals(subNode.getNodeName())) {
  5. String source = subNode.getTextContent();
  6. // targetRef
  7. subNode = subNode.getNextSibling();
  8. String target = subNode.getTextContent();
  9. forEachNodeInputAssociation.put(target, source);
  10. }
  11. }

代码示例来源:origin: pmd/pmd

  1. String entityName = item.getNodeName();
  2. Node firstChild = item.getFirstChild();
  3. if (firstChild != null) {
  4. result = result.replaceAll(Matcher.quoteReplacement(firstChild.getNodeValue()),
  5. "&" + entityName + ";");
  6. } else {

代码示例来源:origin: yaphone/itchat4j

  1. if (doc != null) {
  2. core.getLoginInfo().put(StorageLoginInfoEnum.skey.getKey(),
  3. doc.getElementsByTagName(StorageLoginInfoEnum.skey.getKey()).item(0).getFirstChild()
  4. .getNodeValue());
  5. core.getLoginInfo().put(StorageLoginInfoEnum.wxsid.getKey(),
  6. doc.getElementsByTagName(StorageLoginInfoEnum.wxsid.getKey()).item(0).getFirstChild()
  7. .getNodeValue());
  8. core.getLoginInfo().put(StorageLoginInfoEnum.wxuin.getKey(),
  9. doc.getElementsByTagName(StorageLoginInfoEnum.wxuin.getKey()).item(0).getFirstChild()
  10. .getNodeValue());
  11. core.getLoginInfo().put(StorageLoginInfoEnum.pass_ticket.getKey(),
  12. doc.getElementsByTagName(StorageLoginInfoEnum.pass_ticket.getKey()).item(0).getFirstChild()
  13. .getNodeValue());

代码示例来源:origin: plantuml/plantuml

  1. private String displayMetadata(Node node, int level) {
  2. final NamedNodeMap map = node.getAttributes();
  3. if (map != null) {
  4. final Node keyword = map.getNamedItem("keyword");
  5. if (keyword != null && tag.equals(keyword.getNodeValue())) {
  6. final Node text = map.getNamedItem("value");
  7. if (text != null) {
  8. return text.getNodeValue();
  9. }
  10. }
  11. }
  12. Node child = node.getFirstChild();
  13. // children, so close current tag
  14. while (child != null) {
  15. // print children recursively
  16. final String result = displayMetadata(child, level + 1);
  17. if (result != null) {
  18. return result;
  19. }
  20. child = child.getNextSibling();
  21. }
  22. return null;
  23. }

代码示例来源:origin: stanfordnlp/CoreNLP

  1. private static AceCharSeq parseCharSeq(Node node) {
  2. Node child = getChildByName(node, "charseq");
  3. String start = getAttributeValue(child, "START");
  4. String end = getAttributeValue(child, "END");
  5. String text = child.getFirstChild().getNodeValue();
  6. return new AceCharSeq(text,
  7. Integer.parseInt(start),
  8. Integer.parseInt(end));
  9. }

代码示例来源:origin: kiegroup/jbpm

  1. public void executeWorkItem(WorkItem workItem,
  2. WorkItemManager mgr) {
  3. assertEquals("id", ((org.w3c.dom.Node) workItem
  4. .getParameter("coId")).getNodeName());
  5. assertEquals("some text", ((org.w3c.dom.Node) workItem
  6. .getParameter("coId")).getFirstChild()
  7. .getTextContent());
  8. }

代码示例来源:origin: groovy/groovy-core

  1. protected void printChildren(Node parent, Map namespaces) {
  2. for (Node node = parent.getFirstChild(); node != null; node = node.getNextSibling()) {
  3. print(node, namespaces, false);
  4. }
  5. }

代码示例来源:origin: pentaho/pentaho-kettle

  1. public String getSample( String strFunctionName, String strFunctionNameWithArgs ) {
  2. String sRC = "// Sorry, no Script available for " + strFunctionNameWithArgs;
  3. NodeList nl = dom.getElementsByTagName( "jsFunction" );
  4. for ( int i = 0; i < nl.getLength(); i++ ) {
  5. if ( nl.item( i ).getAttributes().getNamedItem( "name" ).getNodeValue().equals( strFunctionName ) ) {
  6. Node elSample = ( (Element) nl.item( i ) ).getElementsByTagName( "sample" ).item( 0 );
  7. if ( elSample.hasChildNodes() ) {
  8. return ( elSample.getFirstChild().getNodeValue() );
  9. }
  10. }
  11. }
  12. return sRC;
  13. }

代码示例来源:origin: plutext/docx4j

  1. /**
  2. * Method getStrFromNode
  3. *
  4. * @param xpathnode
  5. * @return the string for the node.
  6. */
  7. public static String getStrFromNode(Node xpathnode) {
  8. if (xpathnode.getNodeType() == Node.TEXT_NODE) {
  9. // we iterate over all siblings of the context node because eventually,
  10. // the text is "polluted" with pi's or comments
  11. StringBuilder sb = new StringBuilder();
  12. for (Node currentSibling = xpathnode.getParentNode().getFirstChild();
  13. currentSibling != null;
  14. currentSibling = currentSibling.getNextSibling()) {
  15. if (currentSibling.getNodeType() == Node.TEXT_NODE) {
  16. sb.append(((Text) currentSibling).getData());
  17. }
  18. }
  19. return sb.toString();
  20. } else if (xpathnode.getNodeType() == Node.ATTRIBUTE_NODE) {
  21. return xpathnode.getNodeValue();
  22. } else if (xpathnode.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) {
  23. return xpathnode.getNodeValue();
  24. }
  25. return null;
  26. }

代码示例来源:origin: spullara/mustache.java

  1. private Object get(Node de) {
  2. if (!de.hasChildNodes()) {
  3. return "";
  4. } else if (de.getChildNodes().getLength() == 1 && de.getFirstChild() instanceof Text) {
  5. return de.getTextContent();
  6. } else {
  7. NodeList childNodes = de.getChildNodes();
  8. Map<String, Object> map = new HashMap<>();
  9. for (int i = 0; i < childNodes.getLength(); i++) {
  10. Node item = childNodes.item(i);
  11. if (!(item instanceof Text)) {
  12. put(item, map);
  13. }
  14. }
  15. return map;
  16. }
  17. }

代码示例来源:origin: org.freemarker/freemarker

  1. /**
  2. * Recursively removes all processing instruction nodes from the subtree.
  3. *
  4. * @see #simplify
  5. */
  6. static public void removePIs(Node parent) {
  7. Node child = parent.getFirstChild();
  8. while (child != null) {
  9. Node nextSibling = child.getNextSibling();
  10. if (child.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) {
  11. parent.removeChild(child);
  12. } else if (child.hasChildNodes()) {
  13. removePIs(child);
  14. }
  15. child = nextSibling;
  16. }
  17. }

代码示例来源:origin: kiegroup/jbpm

  1. protected void readDataOutputAssociation(org.w3c.dom.Node xmlNode, Map<String, String> forEachNodeOutputAssociation) {
  2. // sourceRef
  3. org.w3c.dom.Node subNode = xmlNode.getFirstChild();
  4. if ("sourceRef".equals(subNode.getNodeName())) {
  5. String source = subNode.getTextContent();
  6. // targetRef
  7. subNode = subNode.getNextSibling();
  8. String target = subNode.getTextContent();
  9. forEachNodeOutputAssociation.put(source, target);
  10. }
  11. }

代码示例来源:origin: kiegroup/jbpm

  1. public void executeWorkItem(WorkItem workItem,
  2. WorkItemManager mgr) {
  3. Object coIdParamObj = workItem.getParameter("coId");
  4. assertEquals("mydoc", ((Element) coIdParamObj).getNodeName());
  5. assertEquals("mynode", ((Element) workItem.getParameter("coId")).getFirstChild().getNodeName());
  6. assertEquals("user",
  7. ((Element) workItem.getParameter("coId"))
  8. .getFirstChild().getFirstChild()
  9. .getNodeName());
  10. assertEquals("hello world",
  11. ((Element) workItem.getParameter("coId"))
  12. .getFirstChild().getFirstChild()
  13. .getAttributes().getNamedItem("hello")
  14. .getNodeValue());
  15. }

代码示例来源:origin: ehcache/ehcache3

  1. @Test
  2. public void testDefaulSerializerXmlsSerializersValueHasWhitespaces() throws Exception {
  3. final URL resource = XmlConfigurationTest.class.getResource("/configs/default-serializer.xml");
  4. DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
  5. DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
  6. Document doc = dBuilder.parse(new File(resource.toURI()));
  7. NodeList nList = doc.getElementsByTagName("ehcache:serializer");
  8. assertThat(nList.item(2).getFirstChild().getNodeValue(), containsString(" "));
  9. assertThat(nList.item(2).getFirstChild().getNodeValue(), containsString("\n"));
  10. assertThat(nList.item(3).getFirstChild().getNodeValue(), containsString(" "));
  11. assertThat(nList.item(3).getFirstChild().getNodeValue(), containsString("\n"));
  12. nList = doc.getElementsByTagName("ehcache:key-type");
  13. assertThat(nList.item(0).getFirstChild().getNodeValue(), containsString(" "));
  14. assertThat(nList.item(0).getFirstChild().getNodeValue(), containsString("\n"));
  15. assertThat(nList.item(1).getFirstChild().getNodeValue(), containsString(" "));
  16. assertThat(nList.item(1).getFirstChild().getNodeValue(), containsString("\n"));
  17. nList = doc.getElementsByTagName("ehcache:value-type");
  18. assertThat(nList.item(0).getFirstChild().getNodeValue(), containsString(" "));
  19. assertThat(nList.item(0).getFirstChild().getNodeValue(), containsString("\n"));
  20. assertThat(nList.item(1).getFirstChild().getNodeValue(), containsString(" "));
  21. assertThat(nList.item(1).getFirstChild().getNodeValue(), containsString("\n"));
  22. }

代码示例来源:origin: org.apache.poi/poi-ooxml

  1. private String getComplexTypeNameFromChildren(Node localComplexTypeRootNode,
  2. String elementNameWithoutNamespace) {
  3. if(localComplexTypeRootNode == null) {
  4. return "";
  5. }
  6. Node node = localComplexTypeRootNode.getFirstChild();
  7. String complexTypeName = "";
  8. while (node != null) {
  9. if ( node instanceof Element && "element".equals(node.getLocalName())) {
  10. Node nameAttribute = getNameOrRefElement(node);
  11. if (nameAttribute.getNodeValue().equals(elementNameWithoutNamespace)) {
  12. Node complexTypeAttribute = node.getAttributes().getNamedItem("type");
  13. if (complexTypeAttribute!=null) {
  14. complexTypeName = complexTypeAttribute.getNodeValue();
  15. break;
  16. }
  17. }
  18. }
  19. node = node.getNextSibling();
  20. }
  21. return complexTypeName;
  22. }

代码示例来源:origin: real-logic/simple-binary-encoding

  1. /**
  2. * Construct a ValidValue given the XML node and the encodingType.
  3. *
  4. * @param node that contains the validValue
  5. * @param encodingType for the enum
  6. */
  7. public ValidValue(final Node node, final PrimitiveType encodingType)
  8. {
  9. name = getAttributeValue(node, "name");
  10. description = getAttributeValueOrNull(node, "description");
  11. value = PrimitiveValue.parse(node.getFirstChild().getNodeValue(), encodingType);
  12. sinceVersion = Integer.parseInt(getAttributeValue(node, "sinceVersion", "0"));
  13. deprecated = Integer.parseInt(getAttributeValue(node, "deprecated", "0"));
  14. checkForValidName(node, name);
  15. }

相关文章