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

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

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

Node.setTextContent介绍

[英]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: pmd/pmd

@Override
public void setTextContent(String textContent) throws DOMException {
  node.setTextContent(textContent);
}

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

protected Node wrap(AbstractWmlConversionContext context, String result, Document doc) {
  
  RPr rPr = null;
  Node node = null;
  if (result != null) {
    node = createNode(doc);
    if (result.length() > 0) {
      node.setTextContent(result);
    }
  }
  return node;
}

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

public InputStream sanitize(InputStream in) throws Exception {
    byte [] content = IOUtils.toByteArray(in);
    try {
      // storing the entire file in memory in case we need to bail.
      Document doc = builder.parse(new ByteArrayInputStream(content));
      doc.setXmlStandalone(true);
      Object result = expression.evaluate(doc, XPathConstants.NODESET);
      NodeList nodes = (NodeList) result;
      for (int i = 0; i < nodes.getLength(); i++) {
        nodes.item(i).setTextContent("");
      }
      DOMSource source = new DOMSource(doc);
      ByteArrayOutputStream output = new ByteArrayOutputStream();
      StreamResult outStream = new StreamResult(output);
      transformer.transform(source, outStream);
      return new ByteArrayInputStream(output.toByteArray());
    } catch (Exception e) {
      ROOT_LOGGER.debug("Error while sanitizing an xml document", e);
      return new ByteArrayInputStream(content);
    }
  }
}

代码示例来源:origin: spring-projects/spring-batch

n.setTextContent(resolvedValue);

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

public static void testXmlClusteredCache(OsgiTestUtils.Cluster cluster) throws Exception {
 File config = cluster.getWorkingArea().resolve("ehcache.xml").toFile();
 Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(TestMethods.class.getResourceAsStream("ehcache-clustered-osgi.xml"));
 XPath xpath = XPathFactory.newInstance().newXPath();
 Node clusterUriAttribute = (Node) xpath.evaluate("//config/service/cluster/connection/@url", doc, XPathConstants.NODE);
 clusterUriAttribute.setTextContent(cluster.getConnectionUri().toString() + "/cache-manager");
 Transformer xformer = TransformerFactory.newInstance().newTransformer();
 xformer.transform(new DOMSource(doc), new StreamResult(config));
 try (PersistentCacheManager cacheManager = (PersistentCacheManager) CacheManagerBuilder.newCacheManager(
  new XmlConfiguration(config.toURI().toURL(), TestMethods.class.getClassLoader())
 )) {
  cacheManager.init();
  final Cache<Long, Person> cache = cacheManager.getCache("clustered-cache", Long.class, Person.class);
  cache.put(1L, new Person("Brian"));
  assertThat(cache.get(1L).name, is("Brian"));
 }
}

代码示例来源:origin: syncthing/syncthing-android

private boolean setConfigElement(Element parent, String tagName, String textContent) {
  Node element = parent.getElementsByTagName(tagName).item(0);
  if (element == null) {
    element = mConfig.createElement(tagName);
    parent.appendChild(element);
  }
  if (!textContent.equals(element.getTextContent())) {
    element.setTextContent(textContent);
    return true;
  }
  return false;
}

代码示例来源:origin: ron190/jsql-injection

public static void deleteInjectionPoint(Document doc, Node node) {
  NodeList nodeList = node.getChildNodes();
  for (int i = 0; i < nodeList.getLength(); i++) {
    Node currentNode = nodeList.item(i);
    if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
      //calls this method for all the children which is Element
      SoapUtil.deleteInjectionPoint(doc, currentNode);
    } else if (currentNode.getNodeType() == Node.TEXT_NODE) {
      currentNode.setTextContent(currentNode.getTextContent().replaceAll(Pattern.quote(InjectionModel.STAR) +"$", ""));
    }
  }
}

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

try {
  String filePath = Environment.getExternalStorageDirectory()+"/Trip/"+tripID+".trip";  
  File file = new File(filePath);
  DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
  DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
  Document doc = docBuilder.parse(file);

  // Change the content of node
  Node nodes = doc.getElementsByTagName("RouteName").item(0);
  // I changed the below line form nodes.setNodeValue to nodes.setTextContent
  nodes.setTextContent(newname);

  Transformer transformer = TransformerFactory.newInstance().newTransformer();
  transformer.setOutputProperty(OutputKeys.INDENT, "yes");

  // initialize StreamResult with File object to save to file
  StreamResult result = new StreamResult(file);
  DOMSource source = new DOMSource(doc);
  transformer.transform(source, result);

} catch (Exception e) {
  e.printStackTrace();
}

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

mapElement.setTextContent(entry.getValue());
rootElement.appendChild(mapElement);

代码示例来源:origin: spring-projects/spring-roo

/**
 * Gets database dependency from config file and adds it with test scope
 *
 * @param moduleName
 *            the module which dependency should be added
 * @param databaseConfigPrefix
 *            the prefix name for choosing the dependency to add
 * @param configuration
 *            the configuration file with the dependencies to copy from
 */
private void addTestScopedDependency(String moduleName, String databaseConfigPrefix,
  final Element configuration) {
 final List<Element> databaseDependencies =
   XmlUtils.findElements(databaseConfigPrefix + "/dependencies/dependency", configuration);
 for (final Element dependencyElement : databaseDependencies) {
  // Change scope from provided to test
  NodeList childNodes = dependencyElement.getChildNodes();
  for (int i = 0; i < childNodes.getLength(); i++) {
   final Node node = childNodes.item(i);
   if (node != null && node.getNodeType() == Node.ELEMENT_NODE
     && node.getNodeName().equals("scope")) {
    node.setTextContent("test");
   }
  }
  // Add dependency
  getProjectOperations().addDependency(moduleName, new Dependency(dependencyElement));
 }
}

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

Node diskNodeAttribute = diskNodeAttributes.getNamedItem("type");
diskNodeAttribute.setTextContent(migrateDiskInfo.getDiskType().toString());
    Node driverNodeAttribute = driverNodeAttributes.getNamedItem("type");
    driverNodeAttribute.setTextContent(migrateDiskInfo.getDriverType().toString());
  } else if ("source".equals(diskChildNode.getNodeName())) {
    diskNode.removeChild(diskChildNode);

代码示例来源:origin: ron190/jsql-injection

SoapUtil.deleteInjectionPoint(doc, doc.getDocumentElement());
currentNode.setTextContent(currentNode.getTextContent() + InjectionModel.STAR);

代码示例来源:origin: syncthing/syncthing-android

password.setTextContent(BCrypt.hashpw(apikey, BCrypt.gensalt(4)));
changed = true;

代码示例来源:origin: org.apache.nifi.minifi/minifi-runtime

private void setBundleInformation(String group, String artifact, String version) {
  this.bundleElement.getElementsByTagName(GROUP_ELEMENT_NAME).item(0).setTextContent(group);
  this.bundleElement.getElementsByTagName(ARTIFACT_ELEMENT_NAME).item(0).setTextContent(artifact);
  this.bundleElement.getElementsByTagName(VERSION_ELEMENT_NAME).item(0).setTextContent(version);
}

代码示例来源:origin: org.springframework.roo/org.springframework.roo.addon.web.selenium

private Node clickAndWaitCommand(final Document document,
    final String linkTarget) {
  final Node tr = document.createElement("tr");
  final Node td1 = tr.appendChild(document.createElement("td"));
  td1.setTextContent("clickAndWait");
  final Node td2 = tr.appendChild(document.createElement("td"));
  td2.setTextContent(linkTarget);
  final Node td3 = tr.appendChild(document.createElement("td"));
  td3.setTextContent(" ");
  return tr;
}

代码示例来源:origin: org.wso2.bpel/ode-bpel-epr

public void setUrl(String url) {
  NodeList addrList = _eprElmt.getElementsByTagNameNS(Namespaces.WS_ADDRESSING_NS, "Address");
  if (addrList.getLength() > 0)
    addrList.item(0).setTextContent(url);
  else {
    Element addrElmt = _eprElmt.getOwnerDocument().createElementNS(Namespaces.WS_ADDRESSING_NS, "Address");
    addrElmt.setTextContent(url);
    _eprElmt.appendChild(addrElmt);
  }
}

代码示例来源:origin: espertechinc/esper

private 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: net.sourceforge.htmlunit/htmlunit

private static void setText(final Node node, final String text) {
  if (node instanceof SelectableTextInput) {
    ((SelectableTextInput) node).setText(text);
  }
  else {
    node.setTextContent(text);
  }
}

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

@Override
protected Map<XPathExpression, Node> getReplacements(final XPath xPath, final ElementFactory elemFactory)
    throws ParserConfigurationException, XPathExpressionException {
 final XPathExpression key = xPath.compile(autoDiscoverParamExpression);
 final Element value = elemFactory.createElement("init-param");
 value.appendChild(elemFactory.createElement("param-name")).setTextContent("auto-discover-services");
 value.appendChild(elemFactory.createElement("param-value")).setTextContent("false");
 final Map<XPathExpression, Node> replacements = new HashMap<XPathExpression, Node>(1);
 replacements.put(key, value);
 return replacements;
}

相关文章