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

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

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

Node.insertBefore介绍

[英]Inserts the node newChild before the existing child node refChild. If refChild is null, insert newChild at the end of the list of children.
If newChild is a DocumentFragment object, all of its children are inserted, in the same order, before refChild. If the newChild is already in the tree, it is first removed.

Note: Inserting a node before itself is implementation dependent.
[中]在现有子节点refChild之前插入节点newChild。如果refChildnull,请在子项列表的末尾插入newChild
如果newChildDocumentFragment对象,则其所有子对象都将按相同顺序插入refChild之前。如果newChild已在树中,则首先将其删除。
注意:在节点之前插入节点取决于实现。

代码示例

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

  1. @Override
  2. public org.w3c.dom.Node insertBefore(org.w3c.dom.Node newChild, org.w3c.dom.Node refChild) throws DOMException {
  3. return node.insertBefore(newChild, refChild);
  4. }

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

  1. /** If the child node doesn't exist, it is created. */
  2. private static Node getFirstChildNodeByName (Node parent, String child) {
  3. NodeList childNodes = parent.getChildNodes();
  4. for (int i = 0; i < childNodes.getLength(); i++) {
  5. if (childNodes.item(i).getNodeName().equals(child)) {
  6. return childNodes.item(i);
  7. }
  8. }
  9. Node newNode = parent.getOwnerDocument().createElement(child);
  10. if (childNodes.item(0) != null)
  11. return parent.insertBefore(newNode, childNodes.item(0));
  12. else
  13. return parent.appendChild(newNode);
  14. }

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

  1. /** If the child node doesn't exist, it is created. */
  2. private static Node getFirstChildNodeByName (Node parent, String child) {
  3. NodeList childNodes = parent.getChildNodes();
  4. for (int i = 0; i < childNodes.getLength(); i++) {
  5. if (childNodes.item(i).getNodeName().equals(child)) {
  6. return childNodes.item(i);
  7. }
  8. }
  9. Node newNode = parent.getOwnerDocument().createElement(child);
  10. if (childNodes.item(0) != null)
  11. return parent.insertBefore(newNode, childNodes.item(0));
  12. else
  13. return parent.appendChild(newNode);
  14. }

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

  1. /** If the child node or attribute doesn't exist, it is created. Usage example: Node property =
  2. * getFirstChildByAttrValue(properties, "property", "name"); */
  3. private static Node getFirstChildByNameAttrValue (Node node, String childName, String attr, String value) {
  4. NodeList childNodes = node.getChildNodes();
  5. for (int i = 0; i < childNodes.getLength(); i++) {
  6. if (childNodes.item(i).getNodeName().equals(childName)) {
  7. NamedNodeMap attributes = childNodes.item(i).getAttributes();
  8. Node attribute = attributes.getNamedItem(attr);
  9. if (attribute.getNodeValue().equals(value)) return childNodes.item(i);
  10. }
  11. }
  12. Node newNode = node.getOwnerDocument().createElement(childName);
  13. NamedNodeMap attributes = newNode.getAttributes();
  14. Attr nodeAttr = node.getOwnerDocument().createAttribute(attr);
  15. nodeAttr.setNodeValue(value);
  16. attributes.setNamedItem(nodeAttr);
  17. if (childNodes.item(0) != null) {
  18. return node.insertBefore(newNode, childNodes.item(0));
  19. } else {
  20. return node.appendChild(newNode);
  21. }
  22. }

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

  1. /** If the child node or attribute doesn't exist, it is created. Usage example: Node property =
  2. * getFirstChildByAttrValue(properties, "property", "name"); */
  3. private static Node getFirstChildByNameAttrValue (Node node, String childName, String attr, String value) {
  4. NodeList childNodes = node.getChildNodes();
  5. for (int i = 0; i < childNodes.getLength(); i++) {
  6. if (childNodes.item(i).getNodeName().equals(childName)) {
  7. NamedNodeMap attributes = childNodes.item(i).getAttributes();
  8. Node attribute = attributes.getNamedItem(attr);
  9. if (attribute.getNodeValue().equals(value)) return childNodes.item(i);
  10. }
  11. }
  12. Node newNode = node.getOwnerDocument().createElement(childName);
  13. NamedNodeMap attributes = newNode.getAttributes();
  14. Attr nodeAttr = node.getOwnerDocument().createAttribute(attr);
  15. nodeAttr.setNodeValue(value);
  16. attributes.setNamedItem(nodeAttr);
  17. if (childNodes.item(0) != null) {
  18. return node.insertBefore(newNode, childNodes.item(0));
  19. } else {
  20. return node.appendChild(newNode);
  21. }
  22. }

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

  1. /**
  2. * adds processing instruction node to DOM.
  3. */
  4. public void processingInstruction(String target, String data) {
  5. final Node last = (Node)_nodeStk.peek();
  6. ProcessingInstruction pi = _document.createProcessingInstruction(
  7. target, data);
  8. if (pi != null){
  9. if (last == _root && _nextSibling != null)
  10. last.insertBefore(pi, _nextSibling);
  11. else
  12. last.appendChild(pi);
  13. _lastSibling = pi;
  14. }
  15. }

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

  1. /**
  2. * Lexical Handler method to create comment node in DOM tree.
  3. */
  4. public void comment(char[] ch, int start, int length) {
  5. final Node last = (Node)_nodeStk.peek();
  6. Comment comment = _document.createComment(new String(ch,start,length));
  7. if (comment != null){
  8. if (last == _root && _nextSibling != null)
  9. last.insertBefore(comment, _nextSibling);
  10. else
  11. last.appendChild(comment);
  12. _lastSibling = comment;
  13. }
  14. }

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

  1. /**
  2. * Analogous to the Node.insertBefore() method, insert a newNode after a refNode.
  3. *
  4. * @param newNode
  5. * new node
  6. * @param refNode
  7. * ref node
  8. * @throws DOMException
  9. * DOMException
  10. */
  11. public static void insertAfter(Node newNode, Node refNode) throws DOMException {
  12. Node parent = refNode.getParentNode();
  13. Node next = refNode.getNextSibling();
  14. if (next == null) {
  15. parent.appendChild(newNode);
  16. } else {
  17. parent.insertBefore(newNode, next);
  18. }
  19. }

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

  1. /**
  2. * Analogous to the Node.insertBefore() method, insert a newNode after a refNode.
  3. *
  4. * @param newNode
  5. * new node
  6. * @param refNode
  7. * ref node
  8. * @throws DOMException
  9. * DOMException
  10. */
  11. public static void insertAfter(Node newNode, Node refNode) throws DOMException {
  12. Node parent = refNode.getParentNode();
  13. Node next = refNode.getNextSibling();
  14. if (next == null) {
  15. parent.appendChild(newNode);
  16. } else {
  17. parent.insertBefore(newNode, next);
  18. }
  19. }

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

  1. public static void replaceElement(Element oldElement, NodeList newNodes) {
  2. Document doc = oldElement.getOwnerDocument();
  3. Node parent = oldElement.getParentNode();
  4. int len = newNodes.getLength();
  5. for (int i = 0; i < len; i++) {
  6. Node n = newNodes.item(i);
  7. if (!doc.equals(n.getOwnerDocument())) {
  8. // first we need to import the node into the document
  9. n = doc.importNode(n, true);
  10. }
  11. parent.insertBefore(n, oldElement);
  12. }
  13. parent.removeChild(oldElement);
  14. }

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

  1. public static void replaceElement(Element oldElement, NodeList newNodes) {
  2. Document doc = oldElement.getOwnerDocument();
  3. Node parent = oldElement.getParentNode();
  4. int len = newNodes.getLength();
  5. for (int i = 0; i < len; i++) {
  6. Node n = newNodes.item(i);
  7. if (!doc.equals(n.getOwnerDocument())) {
  8. // first we need to import the node into the document
  9. n = doc.importNode(n, true);
  10. }
  11. parent.insertBefore(n, oldElement);
  12. }
  13. parent.removeChild(oldElement);
  14. }

代码示例来源:origin: languagetool-org/languagetool

  1. private static Source mergeIntoSource(InputStream baseXmlStream, InputStream xmlStream) throws Exception {
  2. DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
  3. domFactory.setIgnoringComments(true);
  4. domFactory.setValidating(false);
  5. domFactory.setNamespaceAware(true);
  6. DocumentBuilder builder = domFactory.newDocumentBuilder();
  7. Document baseDoc = builder.parse(baseXmlStream);
  8. Document ruleDoc = builder.parse(xmlStream);
  9. // Shall this be more generic, i.e. reuse not just unification ???
  10. NodeList unificationNodes = baseDoc.getElementsByTagName("unification");
  11. Node ruleNode = ruleDoc.getElementsByTagName("rules").item(0);
  12. Node firstChildRuleNode = ruleNode.getChildNodes().item(1);
  13. for (int i = 0; i < unificationNodes.getLength(); i++) {
  14. Node unificationNode = ruleDoc.importNode(unificationNodes.item(i), true);
  15. ruleNode.insertBefore(unificationNode, firstChildRuleNode);
  16. }
  17. return new DOMSource(ruleDoc);
  18. }

代码示例来源:origin: osmandapp/Osmand

  1. public static void combineAllApplyTags(Document document) {
  2. NodeList nl = document.getElementsByTagName("apply");
  3. while(nl.getLength() > 0) {
  4. Element app = (Element) nl.item(0);
  5. Element parent = (Element) app.getParentNode();
  6. NamedNodeMap attrs = app.getAttributes();
  7. for(int i = 0; i < attrs.getLength(); i++) {
  8. Node ns = attrs.item(i);
  9. parent.setAttribute(ns.getNodeName(), ns.getNodeValue());
  10. }
  11. while(app.getChildNodes().getLength() > 0) {
  12. Node ni = app.getChildNodes().item(0);
  13. app.getParentNode().insertBefore(ni, app);
  14. }
  15. app.getParentNode().removeChild(app);
  16. }
  17. }

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

  1. public void characters(char[] ch, int start, int length) {
  2. final Node last = (Node)_nodeStk.peek();
  3. // No text nodes can be children of root (DOM006 exception)
  4. if (last != _document) {
  5. final String text = new String(ch, start, length);
  6. if( _lastSibling != null && _lastSibling.getNodeType() == Node.TEXT_NODE ){
  7. ((Text)_lastSibling).appendData(text);
  8. }
  9. else if (last == _root && _nextSibling != null) {
  10. _lastSibling = last.insertBefore(_document.createTextNode(text), _nextSibling);
  11. }
  12. else {
  13. _lastSibling = last.appendChild(_document.createTextNode(text));
  14. }
  15. }
  16. }

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

  1. private static String styleXML(Node dxfsNode, Node tableStyleNode) throws IOException, TransformerException {
  2. // built-ins doc uses 1-based dxf indexing, Excel uses 0 based.
  3. // add a dummy node to adjust properly.
  4. dxfsNode.insertBefore(dxfsNode.getOwnerDocument().createElement("dxf"), dxfsNode.getFirstChild());
  5. StringBuilder sb = new StringBuilder();
  6. sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n")
  7. .append("<styleSheet xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\" ")
  8. .append("xmlns:mc=\"http://schemas.openxmlformats.org/markup-compatibility/2006\" ")
  9. .append("xmlns:x14ac=\"http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac\" ")
  10. .append("xmlns:x16r2=\"http://schemas.microsoft.com/office/spreadsheetml/2015/02/main\" ")
  11. .append("mc:Ignorable=\"x14ac x16r2\">\n");
  12. sb.append(writeToString(dxfsNode));
  13. sb.append(writeToString(tableStyleNode));
  14. sb.append("</styleSheet>");
  15. return sb.toString();
  16. }

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

  1. /**
  2. * Enclose the elements' closest common ancestor.
  3. *
  4. * @param first
  5. * first
  6. * @param last
  7. * last
  8. */
  9. protected void slowDown(Element first, Element last) {
  10. Element phonol = MaryDomUtils.encloseNodesWithNewElement(first, last, MaryXML.PHONOLOGY);
  11. phonol.setAttribute("precision", "precise");
  12. Document doc = phonol.getOwnerDocument();
  13. Element prosody = MaryXML.createElement(doc, MaryXML.PROSODY);
  14. prosody.setAttribute("rate", "-20%");
  15. phonol.getParentNode().insertBefore(prosody, phonol);
  16. prosody.appendChild(phonol);
  17. }
  18. }

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

  1. /**
  2. * Enclose the elements' closest common ancestor.
  3. *
  4. * @param first
  5. * first
  6. * @param last
  7. * last
  8. */
  9. protected void slowDown(Element first, Element last) {
  10. Element phonol = MaryDomUtils.encloseNodesWithNewElement(first, last, MaryXML.PHONOLOGY);
  11. phonol.setAttribute("precision", "precise");
  12. Document doc = phonol.getOwnerDocument();
  13. Element prosody = MaryXML.createElement(doc, MaryXML.PROSODY);
  14. prosody.setAttribute("rate", "-20%");
  15. phonol.getParentNode().insertBefore(prosody, phonol);
  16. prosody.appendChild(phonol);
  17. }
  18. }

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

  1. private static void appendNodes(Node self, Closure c) {
  2. Node parent = self.getParentNode();
  3. Node beforeNode = self.getNextSibling();
  4. DOMBuilder b = new DOMBuilder(self.getOwnerDocument());
  5. Element newNodes = (Element) b.invokeMethod("rootNode", c);
  6. Iterator<Node> iter = XmlGroovyMethods.iterator(children(newNodes));
  7. while (iter.hasNext()) {
  8. parent.insertBefore(iter.next(), beforeNode);
  9. }
  10. }

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

  1. /**
  2. * Splits this CDATA node into parts that do not contain a "]]>" sequence.
  3. * Any newly created nodes will be inserted before this node.
  4. */
  5. public void split() {
  6. if (!needsSplitting()) {
  7. return;
  8. }
  9. Node parent = getParentNode();
  10. String[] parts = getData().split("\\]\\]>");
  11. parent.insertBefore(new CDATASectionImpl(document, parts[0] + "]]"), this);
  12. for (int p = 1; p < parts.length - 1; p++) {
  13. parent.insertBefore(new CDATASectionImpl(document, ">" + parts[p] + "]]"), this);
  14. }
  15. setData(">" + parts[parts.length - 1]);
  16. }

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

  1. public final Text splitText(int offset) throws DOMException {
  2. Text newText = document.createTextNode(
  3. substringData(offset, getLength() - offset));
  4. deleteData(0, offset);
  5. Node refNode = getNextSibling();
  6. if (refNode == null) {
  7. getParentNode().appendChild(newText);
  8. } else {
  9. getParentNode().insertBefore(newText, refNode);
  10. }
  11. return this;
  12. }

相关文章