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

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

本文整理了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

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());
}

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

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);
 }
}

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

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();
}

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

/**
 * 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;
  }
}

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

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);
  }
}

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

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

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

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());

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

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;
}

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

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));
}

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

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());
}

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

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

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

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;
}

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

/**
 * 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;
}

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

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;
 }
}

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

/**
 * 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;
  }
}

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

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);
  }
}

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

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());
}

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

@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"));
}

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

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;
}

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

/**
 * 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);
}

相关文章