List of usage examples for org.w3c.dom Element insertBefore
public Node insertBefore(Node newChild, Node refChild) throws DOMException;
newChild
before the existing child node refChild
. From source file:Main.java
/** * Create a new element and add it as the first child * * @param parent/*from ww w . j ava 2 s.c om*/ * the parent to which to add the element * @param name * the name of the element * @return the new element */ public static Element addElementFirst(final Element parent, final String name) { final Element ele = parent.getOwnerDocument().createElement(name); parent.insertBefore(ele, null); return ele; }
From source file:Main.java
public static void addComment(Document doc) { Element root = doc.getDocumentElement(); Element folks = (Element) root.getLastChild(); Comment comment = doc.createComment("Text of the comment"); root.insertBefore(comment, folks); }
From source file:Main.java
/** * adds a child at the first place //from w w w. ja v a 2 s . com * @param parent * @param child */ public static void prependChild(Element parent, Element child) { Node first = parent.getFirstChild(); if (first == null) parent.appendChild(child); else { parent.insertBefore(child, first); } }
From source file:DOMEdit.java
public static void insert(Document doc, String name, String phone, String email) { Element personNode = doc.createElement("person"); Element nameNode = doc.createElement("name"); personNode.appendChild(nameNode);/*from www . j a va2 s. c om*/ Text nametextNode = doc.createTextNode(name); nameNode.appendChild(nametextNode); Element phoneNode = doc.createElement("phone"); personNode.appendChild(phoneNode); Text phonetextNode = doc.createTextNode(phone); phoneNode.appendChild(phonetextNode); Element emailNode = doc.createElement("email"); personNode.appendChild(emailNode); Text emailtextNode = doc.createTextNode(email); emailNode.appendChild(emailtextNode); Element root = doc.getDocumentElement(); Node firstChildNode = root.getFirstChild(); root.insertBefore(personNode, firstChildNode); }
From source file:Main.java
public static void addProcessingInstruction(Document doc) { Element root = doc.getDocumentElement(); Element folks = (Element) root.getLastChild(); ProcessingInstruction pi;//from w ww . ja va 2 s . c o m pi = (ProcessingInstruction) doc.createProcessingInstruction("validate", "phone=\"lookup\""); root.insertBefore(pi, folks); }
From source file:DOMEdit.java
public static void addComment(Document doc) { Element root = doc.getDocumentElement(); Element folks = (Element) root.getLastChild(); Comment comment = doc.createComment("Text of the comment"); root.insertBefore(comment, folks); }/*from w w w.j av a 2 s.c o m*/
From source file:Main.java
public static Element prependChildElement(Element parent, Element child, boolean addWhitespace, Document doc) { Node firstChild = parent.getFirstChild(); if (firstChild == null) { parent.appendChild(child);//from w w w. j a v a2s .co m } else { parent.insertBefore(child, firstChild); } if (addWhitespace) { Node whitespaceText = doc.createTextNode("\n"); parent.insertBefore(whitespaceText, child); } return child; }
From source file:Main.java
/** * Create a child of the given node at the given index. * //from www .j a va 2 s .c o m * @param doc * a document * @param element * an element * @param index * an index * @param nodeName * a node name * @return org.w3c.dom.Element */ public static Element createChildElement(Document doc, Element element, int index, String nodeName) { Element element2 = doc.createElement(nodeName); try { NodeList childList = element.getElementsByTagName(nodeName); Node child = childList.item(index); element.insertBefore(element2, child); } catch (Exception e) { element.appendChild(element2); } return element2; }
From source file:DOMEdit.java
public static void addProcessingInstruction(Document doc) { Element root = doc.getDocumentElement(); Element folks = (Element) root.getLastChild(); ProcessingInstruction pi; pi = (ProcessingInstruction) doc.createProcessingInstruction("validate", "phone=\"lookup\""); root.insertBefore(pi, folks); }//from ww w . ja va2s . c om
From source file:Main.java
static public void setElementText(Element elm, String text) { Node node = elm.getFirstChild(); if (node == null) { if (text != null) elm.appendChild(elm.getOwnerDocument().createTextNode(text)); } else if (node.getNodeType() == Node.TEXT_NODE) { if (text == null) node.getParentNode().removeChild(node); else/*from w w w . j a va 2 s . c o m*/ node.setNodeValue(text); } else if (text != null) { Text textNode = node.getOwnerDocument().createTextNode(text); elm.insertBefore(textNode, elm.getFirstChild()); } }