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

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

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

Node.getTextContent介绍

[英]This attribute returns the text content of this node and its descendants. When it is defined to be null, setting it has no effect. On setting, any possible children this node may have are removed and, if it the new string is not empty or null, replaced by a single Text node containing the string this attribute is set to.
On getting, no serialization is performed, the returned string does not contain any markup. No whitespace normalization is performed and the returned string does not contain the white spaces in element content (see the attribute Text.isElementContentWhitespace). Similarly, on setting, no parsing is performed either, the input string is taken as pure textual content.
The string returned is made of the text content of this node depending on its type, as defined below: Node typeContentELEMENT_NODE, ATTRIBUTE_NODE, ENTITY_NODE, ENTITY_REFERENCE_NODE, DOCUMENT_FRAGMENT_NODEconcatenation of the textContent attribute value of every child node, excluding COMMENT_NODE and PROCESSING_INSTRUCTION_NODE nodes. This is the empty string if the node has no children.TEXT_NODE, CDATA_SECTION_NODE, COMMENT_NODE, PROCESSING_INSTRUCTION_NODEnodeValueDOCUMENT_NODE, DOCUMENT_TYPE_NODE, NOTATION_NODEnull
[中]此属性返回此节点及其子节点的文本内容。将其定义为null时,将其设置为无效。在设置时,此节点可能具有的所有可能的子节点都将被删除,如果新字符串不为空或null,则替换为包含此属性设置为的字符串的单个Text节点。
获取时,不执行序列化,返回的字符串不包含任何标记。不执行空白规范化,并且返回的字符串不包含元素内容中的空白(请参见属性[$3$])。类似地,在设置时,也不执行解析,输入字符串被视为纯文本内容。
返回的字符串由该节点的文本内容组成,具体取决于其类型,定义如下:节点类型ContentElement\u节点、属性节点、实体节点、实体引用节点、文档片段节点每个子节点的textContent属性值的链接,不包括注释节点和处理指令节点。如果节点没有子节点,则为空字符串。文本节点、CDATA节点、节节点、注释节点、处理节点、指令节点nodeValue文档节点、文档类型节点、符号节点null

代码示例

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

private static String getTagAttributeText(final Document doc, final String tag, final String attribute) {
 NodeList elementsByTagName = doc.getElementsByTagName(tag);
 for (int i = 0; i < elementsByTagName.getLength(); ++i) {
  Node item = elementsByTagName.item(i);
  Node namedItem = item.getAttributes().getNamedItem(attribute);
  if (namedItem != null) {
   return namedItem.getTextContent();
  }
 }
 return null;
}

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

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

代码示例来源: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: chanjarster/weixin-java-tools

static String extractEncryptPart(String xml) {
 try {
  DocumentBuilder db = builderLocal.get();
  Document document = db.parse(new InputSource(new StringReader(xml)));
  Element root = document.getDocumentElement();
  return root.getElementsByTagName("Encrypt").item(0).getTextContent();
 } catch (Exception e) {
  throw new RuntimeException(e);
 }
}

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

private Map<String, String> parsePropertiesNode(Node propertiesNode) {
  final Map<String, String> properties = new HashMap<String, String>();
  final NodeList propertiesNodes = propertiesNode.getChildNodes();
  for (int j = 0; j < propertiesNodes.getLength(); j++) {
    final Node propertyNode = propertiesNodes.item(j);
    final String nodeName = propertyNode.getNodeName();
    if (nodeName != null) {
      properties.put(nodeName, propertyNode.getTextContent());
    }
  }
  return properties;
}

代码示例来源:origin: org.apache.hadoop/hadoop-common

static String readFirstTagValue(Element e, String tag) {
 NodeList nodes = e.getElementsByTagName(tag);
 return (nodes.getLength() == 0)? null : nodes.item(0).getTextContent();
}

代码示例来源: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: libgdx/libgdx

private void initData () throws ParserConfigurationException, IOException, SAXException {
  DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
  DocumentBuilder builder = dbFactory.newDocumentBuilder();
  Document doc = builder.parse(ExternalExtensionsDialog.class
    .getResourceAsStream("/com/badlogic/gdx/setup/data/extensions.xml"));
  for (int i = 0; i < nList.getLength(); i++) {
    Node nNode = nList.item(i);
    if (nNode.getNodeType() == Node.ELEMENT_NODE) {
      String name = eElement.getElementsByTagName("name").item(0).getTextContent();
      String description = eElement.getElementsByTagName("description").item(0).getTextContent();
      String version = eElement.getElementsByTagName("version").item(0).getTextContent();
      String compatibility = eElement.getElementsByTagName("compatibility").item(0).getTextContent();
      String url = eElement.getElementsByTagName("website").item(0).getTextContent();
      gwtInherits = new String[inheritsNode.getLength()];
      for (int j = 0; j < inheritsNode.getLength(); j++)
        gwtInherits[j] = inheritsNode.item(j).getTextContent();

代码示例来源:origin: apache/geode

/**
 * Finds the node in the given document with the given name and attribute
 *
 * @param doc XML document to search for the node
 * @param nodeName The name of the node to search for
 * @param name The name of the attribute that the node should contain
 * @param value The value of the node's given attribute
 * @return Node with the given name, attribute, and attribute value
 */
private static Node findNodeWithAttribute(Document doc, String nodeName, String name,
  String value) {
 // Get all nodes with given name
 NodeList nodes = doc.getElementsByTagName(nodeName);
 if (nodes == null) {
  return null;
 }
 // Find and return the first node that has the given attribute
 for (int i = 0; i < nodes.getLength(); i++) {
  Node node = nodes.item(i);
  Node nodeAttr = node.getAttributes().getNamedItem(name);
  if (nodeAttr != null && nodeAttr.getTextContent().equals(value)) {
   return node;
  }
 }
 return null;
}

代码示例来源:origin: Tencent/tinker

private static String nodeToString(Node node, boolean isNoChild) {
  StringBuilder stringBuilder = new StringBuilder();
  if (node != null) {
    stringBuilder.append(node.getNodeName());
    NamedNodeMap namedNodeMap = node.getAttributes();
    stringBuilder.append(Constant.Symbol.MIDDLE_BRACKET_LEFT);
    int namedNodeMapLength = namedNodeMap.getLength();
    for (int j = 0; j < namedNodeMapLength; j++) {
      Node attributeNode = namedNodeMap.item(j);
      stringBuilder.append(Constant.Symbol.AT + attributeNode.getNodeName() + Constant.Symbol.EQUAL + attributeNode.getNodeValue());
      if (j < namedNodeMapLength - 1) {
        stringBuilder.append(Constant.Symbol.COMMA);
      }
    }
    stringBuilder.append(Constant.Symbol.MIDDLE_BRACKET_RIGHT);
    String value = StringUtil.nullToBlank(isNoChild ? node.getTextContent() : node.getNodeValue()).trim();
    if (StringUtil.isNotBlank(value)) {
      stringBuilder.append(Constant.Symbol.EQUAL + value);
    }
  }
  return stringBuilder.toString();
}

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

private void parseParentNode(Node node) {
  final NodeList parentNodes = node.getChildNodes();
  String parentGroupId = null;
  String parentArtifactId = null;
  String parentVersion = null;
  for (int k = 0; k < parentNodes.getLength(); k++) {
    final Node childParentNode = parentNodes.item(k);
    final String nodeName = childParentNode.getNodeName();
    if ("groupId".equals(nodeName)) {
      parentGroupId = childParentNode.getTextContent();
    } else if ("artifactId".equals(nodeName)) {
      parentArtifactId = childParentNode.getTextContent();
    } else if ("version".equals(nodeName)) {
      parentVersion = childParentNode.getTextContent();
    }
  }
  if (this.groupId == null) {
    this.groupId = parentGroupId;
  }
  if (this.version == null) {
    this.version = parentVersion;
  }
  this.parent = new MavenArtifact();
  this.parent.groupId = parentGroupId;
  this.parent.artifactId = parentArtifactId;
  this.parent.version = parentVersion;
}

代码示例来源:origin: code4craft/webmagic

if (nodeList.getLength() == 0) {
  return null;
Node item = nodeList.item(0);
if (item.getNodeType() == Node.ATTRIBUTE_NODE || item.getNodeType() == Node.TEXT_NODE) {
  return item.getTextContent();
} else {
  StreamResult xmlOutput = new StreamResult(new StringWriter());

代码示例来源:origin: apache/geode

Node attr = nodeAttrs.getNamedItem(key);
if (attr == null
  || (checkSimilarValues && !attr.getTextContent().equals(attributes.get(key)))) {
 return false;
String attr = nodeAttrs.item(i).getNodeName();
if (attributes.get(attr) == null || (checkSimilarValues
  && !attributes.get(attr).equals(nodeAttrs.item(i).getTextContent()))) {
 return false;

代码示例来源:origin: sannies/mp4parser

public static void trimWhitespace(Node node) {
  NodeList children = node.getChildNodes();
  for (int i = 0; i < children.getLength(); ++i) {
    Node child = children.item(i);
    if (child.getNodeType() == Node.TEXT_NODE) {
      child.setTextContent(child.getTextContent().trim());
    }
    trimWhitespace(child);
  }
}

代码示例来源:origin: code4craft/webmagic

StreamResult xmlOutput = new StreamResult();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
for (int i = 0; i < nodeList.getLength(); i++) {
  Node item = nodeList.item(i);
  if (item.getNodeType() == Node.ATTRIBUTE_NODE || item.getNodeType() == Node.TEXT_NODE) {
    results.add(item.getTextContent());
  } else {
    xmlOutput.setWriter(new StringWriter());

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

protected void readDataOutputAssociation(org.w3c.dom.Node xmlNode, SubProcessNode subProcessNode, Map<String, String> dataOutputs) {
  // sourceRef
  org.w3c.dom.Node subNode = xmlNode.getFirstChild();
  String from = subNode.getTextContent();
  // targetRef
  subNode = subNode.getNextSibling();
  String to = subNode.getTextContent();
  // transformation
  Transformation transformation = null;
  subNode = subNode.getNextSibling();
  if (subNode != null && "transformation".equals(subNode.getNodeName())) {
    String lang = subNode.getAttributes().getNamedItem("language").getNodeValue();
    String expression = subNode.getTextContent();
    DataTransformer transformer = transformerRegistry.find(lang);
    if (transformer == null) {
      throw new IllegalArgumentException("No transformer registered for language " + lang);
    }                
    transformation = new Transformation(lang, expression, from);
    subNode = subNode.getNextSibling();
  }
  subProcessNode.addOutMapping(dataOutputs.get(from), to, transformation);
}

代码示例来源:origin: Javen205/IJPay

/**
 * 针对没有嵌套节点的简单处理
 * 
 * @return map集合
 */
public Map<String, String> toMap() {
  Element root = doc.getDocumentElement();
  Map<String, String> params = new HashMap<String, String>();
  // 将节点封装成map形式
  NodeList list = root.getChildNodes();
  for (int i = 0; i < list.getLength(); i++) {
    Node node = list.item(i);
    params.put(node.getNodeName(), node.getTextContent());
  }
  // 含有空白符会生成一个#text参数
  params.remove("#text");
  return params;
}

代码示例来源:origin: internetarchive/heritrix3

/**
 * Return all config files included via 'import' statements in the
 * primary config (or other included configs). 
 * 
 * @param xml File to examine
 * @return List&lt;File&gt; of all transitively-imported Files
 */
@SuppressWarnings("unchecked")
public List<File> getImportedConfigs(File xml) {
  List<File> imports = new LinkedList<File>(); 
  Document doc = getDomDocument(xml);
  if(doc==null) {
    return ListUtils.EMPTY_LIST;
  }
  NodeList importElements = doc.getElementsByTagName("import");
  for(int i = 0; i < importElements.getLength(); i++) {
    File imported = new File(
        getJobDir(),
        importElements.item(i).getAttributes().getNamedItem("resource").getTextContent());
    imports.add(imported);
    imports.addAll(getImportedConfigs(imported));
  }
  return imports; 
}

代码示例来源:origin: typ0520/fastdex

private static String nodeToString(Node node, boolean isNoChild) {
  StringBuilder stringBuilder = new StringBuilder();
  if (node != null) {
    stringBuilder.append(node.getNodeName());
    NamedNodeMap namedNodeMap = node.getAttributes();
    stringBuilder.append(Constant.Symbol.MIDDLE_BRACKET_LEFT);
    int namedNodeMapLength = namedNodeMap.getLength();
    for (int j = 0; j < namedNodeMapLength; j++) {
      Node attributeNode = namedNodeMap.item(j);
      stringBuilder.append(Constant.Symbol.AT + attributeNode.getNodeName() + Constant.Symbol.EQUAL + attributeNode.getNodeValue());
      if (j < namedNodeMapLength - 1) {
        stringBuilder.append(Constant.Symbol.COMMA);
      }
    }
    stringBuilder.append(Constant.Symbol.MIDDLE_BRACKET_RIGHT);
    String value = StringUtil.nullToBlank(isNoChild ? node.getTextContent() : node.getNodeValue()).trim();
    if (StringUtil.isNotBlank(value)) {
      stringBuilder.append(Constant.Symbol.EQUAL + value);
    }
  }
  return stringBuilder.toString();
}

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

String xml = "<add job=\"351\">\n" +
       "    <tag>foobar</tag>\n" +
       "    <tag>foobar2</tag>\n" +
       "</add>";
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
ByteArrayInputStream bis = new ByteArrayInputStream(xml.getBytes());
Document doc = db.parse(bis);
Node n = doc.getFirstChild();
NodeList nl = n.getChildNodes();
Node an,an2;

for (int i=0; i < nl.getLength(); i++) {
  an = nl.item(i);
  if(an.getNodeType()==Node.ELEMENT_NODE) {
    NodeList nl2 = an.getChildNodes();

    for(int i2=0; i2<nl2.getLength(); i2++) {
      an2 = nl2.item(i2);
      // DEBUG PRINTS
      System.out.println(an2.getNodeName() + ": type (" + an2.getNodeType() + "):");
      if(an2.hasChildNodes()) System.out.println(an2.getFirstChild().getTextContent());
      if(an2.hasChildNodes()) System.out.println(an2.getFirstChild().getNodeValue());
      System.out.println(an2.getTextContent());
      System.out.println(an2.getNodeValue());
    }
  }
}

相关文章