List of usage examples for org.w3c.dom Node insertBefore
public Node insertBefore(Node newChild, Node refChild) throws DOMException;
newChild
before the existing child node refChild
. From source file:Main.java
/** * Inserts a new child element to a parent. * /*from w w w. j av a 2 s . com*/ * @param parent the Element to which to append the child * @param name the (tag) name of the new child * @param value the text value of the new element. (can be null!) * @param pos the inserted element will be placed before this element * * @return the new child element */ static public Element insertElement(Node parent, String name, String value, Element pos) { if (parent == null) return null; // Fehler Element child = parent.getOwnerDocument().createElement(name); if (value != null) setElementText(child, value); // insert now parent.insertBefore(child, pos); return child; }
From source file:Main.java
public static Node prependXMLChildFromString(Node parent, String child) throws ParserConfigurationException, SAXException, IOException { if (parent instanceof Document) parent = ((Document) parent).getDocumentElement(); Document newDoc = getXMLFromString(child); Node newNode = parent.getOwnerDocument().importNode(newDoc.getDocumentElement(), true); return parent.insertBefore(newNode, parent.getFirstChild()); }
From source file:Main.java
/** Goes through and adds newlines and indent to the current node and all its children * @param current the current node// ww w .j av a 2 s . c o m * @param indent the current level of indent this is increased recursively*/ private static void addFormatting(Document doc, Node current, String indent) { // go through each of the children adding space as required Node child = current.getFirstChild(); String childIndent = indent + "\t"; while (child != null) { Node nextChild = child.getNextSibling(); if (child.getNodeType() != Node.TEXT_NODE) { // Only if we aren't a text node do we add the space current.insertBefore(doc.createTextNode("\n" + childIndent), child); if (child.hasChildNodes()) { addFormatting(doc, child, childIndent); } if (nextChild == null) { // Because this is the last child, we need to add some space after it current.appendChild(doc.createTextNode("\n" + indent)); } } child = nextChild; } }
From source file:Main.java
/** * The method inserts end-of-line+indentation Text nodes where indentation is necessary. * * @param node - node to be pretty formatted * @param identLevel - initial indentation level of the node * @param ident - additional indentation inside the node *//*from www. j a v a 2 s . c om*/ private static void prettyFormat(Node node, String identLevel, String ident) { NodeList nodelist = node.getChildNodes(); int iStart = 0; Node item = nodelist.item(0); if (item != null) { short type = item.getNodeType(); if (type == Node.ELEMENT_NODE || type == Node.COMMENT_NODE) { Node newChild = node.getOwnerDocument().createTextNode(EOL_XML + identLevel + ident); node.insertBefore(newChild, item); iStart = 1; } } for (int i = iStart; i < nodelist.getLength(); i++) { item = nodelist.item(i); if (item != null) { short type = item.getNodeType(); if (type == Node.TEXT_NODE && item.getNodeValue().trim().length() == 0) { if (i + 1 < nodelist.getLength()) { item.setNodeValue(EOL_XML + identLevel + ident); } else { item.setNodeValue(EOL_XML + identLevel); } } else if (type == Node.ELEMENT_NODE) { prettyFormat(item, identLevel + ident, ident); if (i + 1 < nodelist.getLength()) { Node nextItem = nodelist.item(i + 1); if (nextItem != null) { short nextType = nextItem.getNodeType(); if (nextType == Node.ELEMENT_NODE || nextType == Node.COMMENT_NODE) { Node newChild = node.getOwnerDocument() .createTextNode(EOL_XML + identLevel + ident); node.insertBefore(newChild, nextItem); i++; continue; } } } else { Node newChild = node.getOwnerDocument().createTextNode(EOL_XML + identLevel); node.appendChild(newChild); i++; continue; } } } } }
From source file:com.gargoylesoftware.htmlunit.html.impl.SimpleRange.java
private static void insertNodeOrDocFragment(final Node parent, final Node newNode, final Node refNode) { if (newNode instanceof DocumentFragment) { final DocumentFragment fragment = (DocumentFragment) newNode; final NodeList childNodes = fragment.getChildNodes(); while (childNodes.getLength() > 0) { final Node item = childNodes.item(0); parent.insertBefore(item, refNode); }/*from w w w .j a v a2 s .c om*/ } else { parent.insertBefore(newNode, refNode); } }
From source file:bridge.toolkit.commands.S1000DConverter.java
/** * modify scoEntryItem in scoEntryContent, move lom from scoEntryItem * //from www . java 2s.co m * @param nodes * @throws Exception */ private static void walkingthrough(Node nodes, org.w3c.dom.Document document) throws Exception { changeScoEntry(nodes, document); int length = nodes.getChildNodes().getLength(); for (int i = 0; i < length; i++) { Node node = nodes.getChildNodes().item(i); changeScoEntry(node, document); if (node.getNodeName().equals("scoEntryItem")) { // the lom children must linked to the parent child (scoEntry) int lengthlom = node.getChildNodes().getLength(); for (int lom = 0; lom < lengthlom; lom++) { if (node.getChildNodes().item(lom) != null && node.getChildNodes().item(lom).getNodeName().equals("lom:lom")) { // clone the node.. Node cloneLom = node.getChildNodes().item(lom).cloneNode(true); // first of all I remove it Node lomnode = node.getChildNodes().item(lom); Node father = lomnode.getParentNode(); Node grandf = father.getParentNode(); father.removeChild(lomnode); // then I add it to the new parent.. grandf.insertBefore(cloneLom, father); } // for each dmRef open the DM and read the real data module // content to be show in the lesson if (node.getChildNodes().item(lom) != null && node.getChildNodes().item(lom).getNodeName().equals("dmRef")) { String filename = resourcepack + "\\" + gettingDmfilename(node.getChildNodes().item(lom)) + ".xml"; if (new File(filename).exists()) { // if the data module is a scoContent will copy each // dmref in the scoEntry org.w3c.dom.Document dm41 = getDoc(new File(filename)); if (processXPathSingleNode("dmodule/content/scoContent", dm41) != null) ; { NodeList dmre = processXPath("/dmodule/content/scoContent/trainingStep/dmRef", dm41); Node father = node.getChildNodes().item(lom).getParentNode(); father.removeChild(node.getChildNodes().item(lom)); // remove the reference to Scocontent and // replace it with the dm inside for (int nodi = 0; nodi < dmre.getLength(); nodi++) { Node cloneref = dmre.item(nodi).cloneNode(true); document.adoptNode(cloneref); father.appendChild(cloneref); } } } } } // rename scoEntryItem in scoEntry node = changeNodename(node, document, "scoEntryContent"); } walkingthrough(node, document); } }
From source file:com.wavemaker.tools.pws.install.PwsInstall.java
public static Document insertImport(Document doc, String resource) { List<Node> targetList = new ArrayList<Node>(); // First, delete old lines if any. NodeList list = doc.getElementsByTagName("import"); Node node = null;/* w ww .j a va2s .c o m*/ for (int i = 0; i < list.getLength(); i++) { node = list.item(i); NamedNodeMap attributes = node.getAttributes(); for (int j = 0; j < attributes.getLength(); j++) { Node attr = attributes.item(j); if (attr.getNodeName().equals("resource") && attr.getNodeValue().equals(resource)) { targetList.add(node); break; } } } NodeList beans_list = doc.getElementsByTagName("beans"); Node beans_node = beans_list.item(0); if (targetList.size() > 0) { for (Node target : targetList) { beans_node.removeChild(target); } } // Now, add the new line NodeList list1 = beans_node.getChildNodes(); Node bean_node = null; for (int i = 0; i < list1.getLength(); i++) { Node node1 = list1.item(i); if (node1.getNodeName().equals("bean")) { bean_node = node1; break; } } Element elem = doc.createElement("import"); elem.setAttribute("resource", resource); try { if (bean_node != null) { beans_node.insertBefore(elem, bean_node); } else { beans_node.appendChild(elem); } } catch (DOMException ex) { ex.printStackTrace(); } return doc; }
From source file:org.jolokia.roo.JolokiaCommands.java
private void addLineBreak(Node pRootElement, Node pBeforeThis, Document pWebXmlDoc) { pRootElement.insertBefore(pWebXmlDoc.createTextNode("\n "), pBeforeThis); pRootElement.insertBefore(pWebXmlDoc.createTextNode("\n "), pBeforeThis); }
From source file:net.sf.joost.emitter.DOMEmitter.java
private void insertNode(Node newNode) { Node lastNode = (Node) stack.peek(); if (stack.size() == 1 && nextSiblingOfRootNodes != null) { lastNode.insertBefore(newNode, nextSiblingOfRootNodes); } else {/*from w ww . ja v a2s. c om*/ lastNode.appendChild(newNode); } }
From source file:org.piraso.maven.WebXmlPirasoModifier.java
private void insertContextParam(Node root, Node insertBefore, String name, String value) { Node buf = document.createTextNode("\n "); Node contextParam = createContextParam(name, value); root.insertBefore(buf, insertBefore); root.insertBefore(contextParam, buf); }