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

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

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

Node.appendChild介绍

[英]Adds the node newChild to the end of the list of children of this node. If the newChild is already in the tree, it is first removed.
[中]将节点newChild添加到此节点的子节点列表的末尾。如果newChild已在树中,则首先将其删除。

代码示例

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

@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) {
  Node parent = getParent();
  Element element = this.document.createElementNS(uri, qName);
  for (int i = 0; i < attributes.getLength(); i++) {
    String attrUri = attributes.getURI(i);
    String attrQname = attributes.getQName(i);
    String value = attributes.getValue(i);
    if (!attrQname.startsWith("xmlns")) {
      element.setAttributeNS(attrUri, attrQname, value);
    }
  }
  element = (Element) parent.appendChild(element);
  this.elements.add(element);
}

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

/** If the child node doesn't exist, it is created. */
private static Node getFirstChildNodeByName (Node parent, String child) {
  NodeList childNodes = parent.getChildNodes();
  for (int i = 0; i < childNodes.getLength(); i++) {
    if (childNodes.item(i).getNodeName().equals(child)) {
      return childNodes.item(i);
    }
  }
  Node newNode = parent.getOwnerDocument().createElement(child);
  if (childNodes.item(0) != null)
    return parent.insertBefore(newNode, childNodes.item(0));
  else
    return parent.appendChild(newNode);
}

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

@Override
public void characters(char[] ch, int start, int length) {
  String data = new String(ch, start, length);
  Node parent = getParent();
  Node lastChild = parent.getLastChild();
  if (lastChild != null && lastChild.getNodeType() == Node.TEXT_NODE) {
    ((Text) lastChild).appendData(data);
  }
  else {
    Text text = this.document.createTextNode(data);
    parent.appendChild(text);
  }
}

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

public static void main(String[] args) throws Exception {
  DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
  DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
  Document document = documentBuilder.parse("server.xml");
  Element root = document.getDocumentElement();
    Element newServer = document.createElement("server");
    Element name = document.createElement("name");
    name.appendChild(document.createTextNode(server.getName()));
    newServer.appendChild(name);
    Element port = document.createElement("port");
    port.appendChild(document.createTextNode(Integer.toString(server.getPort())));
    newServer.appendChild(port);
    root.appendChild(newServer);

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

File... files) throws Exception {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
  .newInstance();
docBuilderFactory
  .setIgnoringElementContentWhitespace(true);
DocumentBuilder docBuilder = docBuilderFactory
  .newDocumentBuilder();
Document base = docBuilder.parse(files[0]);
  nextResults.removeChild(kid);
  kid = base.importNode(kid, true);
  results.appendChild(kid);

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

nl = (NodeList) exprFrom.evaluate(source, XPathConstants.NODESET);
} else if (source instanceof String) {
  DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
  Document doc = builder.newDocument();
  Element temp = doc.createElementNS(null, "temp");
  temp.appendChild(doc.createTextNode((String) source));
  nl = temp.getChildNodes();
} else if (source == null) {
      targetElem = ((Text) nl.item(i)).getWholeText();
    } else {
      DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
      Document doc = builder.newDocument();
      targetElem  = doc.importNode(nl.item(i), true);
      ((Element) targetElem).setAttributeNode((Attr) n);
    } else {
      ((org.w3c.dom.Node) targetElem).appendChild(n);

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

Element node = factory.createElement("foundJar");
node.setAttribute("name", keyStr.substring(0, keyStr.indexOf("-")));
node.setAttribute("desc", keyStr.substring(keyStr.indexOf("-") + 1));
node.appendChild(factory.createTextNode((String)subhash.get(keyStr)));
container.appendChild(node);
Element node = factory.createElement("foundJar");
node.appendChild(factory.createTextNode(ERROR + " Reading " + key + " threw: " + e.toString()));
container.appendChild(node);

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

.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
DOMImplementation domImpl = db.getDOMImplementation();
Document document = buildExampleDocumentWithNamespaces(domImpl);
Document document = domImpl.createDocument("urn:example.namespace",
    "my:example", null);
Element element = document.createElementNS("http://another.namespace",
    "element");
document.getDocumentElement().appendChild(element);
return document;

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

Element root = xmlDoc.createElement("booking");
item = xmlDoc.createElement("bookingID");
item.appendChild(xmlDoc.createTextNode("115"));
root.appendChild(item);
xmlDoc.appendChild(root);

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

protected void appendElement(org.w3c.dom.Node parentNode) {
  DocumentNavigator docNav = new DocumentNavigator();
  Document ownerDocument = parentNode.getOwnerDocument();
  if (ownerDocument == null) {
    // If the parentNode is a Document itself, it's ownerDocument is
    // null
    ownerDocument = (Document) parentNode;
  }
  String elementName = docNav.getElementName(this);
  Element element = ownerDocument.createElement(elementName);
  parentNode.appendChild(element);
  for (Iterator<Attribute> iter = docNav.getAttributeAxisIterator(this); iter.hasNext();) {
    Attribute attr = iter.next();
    element.setAttribute(attr.getName(), attr.getStringValue());
  }
  for (Iterator<Node> iter = docNav.getChildAxisIterator(this); iter.hasNext();) {
    AbstractNode child = (AbstractNode) iter.next();
    child.appendElement(element);
  }
}

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

static class LoggingErrorListener implements ErrorListener {
    // See http://www.cafeconleche.org/slides/sd2003west/xmlandjava/346.html
  
  boolean strict;
    public LoggingErrorListener(boolean strict) {
  }
  
  public void warning(TransformerException exception) {
      log.warn(exception.getMessage(), exception);
      // Don't throw an exception and stop the processor
   // just for a warning; but do log the problem
  }
  
  public void error(TransformerException exception)
   throws TransformerException {
   
    log.error(exception.getMessage(), exception);
   
    // XSLT is not as draconian as XML. There are numerous errors
   // which the processor may but does not have to recover from; 
   // e.g. multiple templates that match a node with the same
   // priority. If I do not want to allow that,  I'd throw this 
   // exception here.
    if (strict) {
      throw exception;
    }

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

private static Element configVServiceNodeDetails(Document doc, String vlanId, String ipAddr) {
  Element configure = doc.createElementNS(s_ciscons, "nxos:configure");
  Element modeConfigure = doc.createElement("nxos:" + s_configuremode);
  configure.appendChild(modeConfigure);
  Element vservice = doc.createElement("vservice");
  vservice.appendChild(doc.createElement("node"))
    .appendChild(doc.createElement("ASA_" + vlanId))
    .appendChild(doc.createElement("type"))
    .appendChild(doc.createElement("asa"));
  modeConfigure.appendChild(vservice);
  Element address = doc.createElement(s_paramvalue);
  address.setAttribute("isKey", "true");
  address.setTextContent(ipAddr);
  modeConfigure.appendChild(doc.createElement("ip")).appendChild(doc.createElement("address")).appendChild(doc.createElement("value")).appendChild(address);
  modeConfigure.appendChild(doc.createElement("adjacency"))
    .appendChild(doc.createElement("l2"))
    .appendChild(doc.createElement("vlan"))
    .appendChild(doc.createElement("value"))
    .appendChild(vlan);
  modeConfigure.appendChild(doc.createElement("fail-mode")).appendChild(doc.createElement("close"));

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

DocumentBuilder builder = XMLParserFactoryProducer.createSecureDocBuilderFactory().newDocumentBuilder();
 data.targetDOM = builder.parse( inputSource );
 if ( !meta.isComplexJoin() ) {
 DocumentBuilder builder = XMLParserFactoryProducer.createSecureDocBuilderFactory().newDocumentBuilder();
 Document joinDocument = builder.parse( new InputSource( new StringReader( strJoinXML ) ) );
 data.targetNode.appendChild( node );
} catch ( Exception e ) {
 throw new KettleException( e );

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

public void startElement(String namespace, String localName, String qName,
Attributes attrs) 
final Element tmp = (Element)_document.createElementNS(namespace, qName);
for (int i = 0; i < nattrs; i++) {
  if (attrs.getLocalName(i) == null) {
  tmp.setAttribute(attrs.getQName(i), attrs.getValue(i));
  last.insertBefore(tmp, _nextSibling);
else
  last.appendChild(tmp);

代码示例来源:origin: commons-digester/commons-digester

Node previousTop = top;
  if ((localName == null) || (localName.length() == 0)) { 
    top = doc.createElement(qName);
  } else {
    top = doc.createElementNS(namespaceURI, localName);
  previousTop.appendChild(top);
  depth++;
} catch (DOMException e) {

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

private Node createElement(Document doc, Node currentNode, String axisName) {
  Node selectedNode;
  if (isNamespaceDeclared()) {
    selectedNode =doc.createElementNS(getNamespace(),axisName);
  } else {
    selectedNode = doc.createElementNS("", axisName);
  }
  currentNode.appendChild(selectedNode);
  return selectedNode;
}

代码示例来源:origin: org.apache.servicemix/servicemix-eip

protected Element createChildElement(QName name, Node parent) {
  Document doc = parent instanceof Document ? (Document) parent : parent.getOwnerDocument();
  Element elem;
  if ("".equals(name.getNamespaceURI())) {
    elem = doc.createElement(name.getLocalPart());   
  } else {
    elem = doc.createElementNS(name.getNamespaceURI(),
                  name.getPrefix() + ":" + name.getLocalPart());
  }
  parent.appendChild(elem);
  return elem;
}

代码示例来源:origin: org.semver/api

/**
 * Add a contained class.
 *
 * @param info information about a class
 * @throws DiffException when there is an underlying exception, e.g.
 *                       writing to a file caused an IOException
 */
public void contains(ClassInfo info) throws DiffException {
  Element tmp = doc.createElementNS(XML_URI, "class");
  tmp.setAttribute("name", info.getName());
  currentNode.appendChild(tmp);
}

代码示例来源:origin: org.apache.servicemix/servicemix-core

private static Element createChild(Node parent, String name, String text) {
  Document doc = parent instanceof Document ? (Document) parent : parent.getOwnerDocument();
  Element child = doc.createElementNS("http://java.sun.com/xml/ns/jbi/management-message", name);
  if (text != null) {
    child.appendChild(doc.createTextNode(text));
  }
  parent.appendChild(child);
  return child;
}

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

Element e = doc.createElementNS(uri, qName);
node.appendChild(e);
node = e;

相关文章