List of usage examples for org.w3c.dom Document createElement
public Element createElement(String tagName) throws DOMException;
From source file:Main.java
/** * Convenience function for creating elements. * /*from ww w. j a va 2 s .co m*/ * @param document * the DOM document we're creating the element in * @param name * the tagname for the element * @param attributes * a map from attribute names to attributes, or <CODE>null</CODE> * if this element should have no attributes * @param text * the text for the element, which will be made into a text node * and added as a child of the created element, or <CODE>null</CODE> * if the element should have no children * @return a new element */ public static Element createElement(Document doc, String name, Object text, Map<String, Object> attributes) { Element e = doc.createElement(name); // Set the attributes. if (attributes != null) { for (Entry<String, Object> entry : attributes.entrySet()) { e.setAttribute(entry.getKey(), entry.getValue().toString()); } } // Add the text element. if (text != null) e.appendChild(createTextNode(doc, text.toString())); return e; }
From source file:Main.java
/** * Inserts a new value for an XML tag specified by <code>tagName</code> name * in a <code>Element</code> object. * /* w ww . j a va2s . com*/ * @param elementToAppend * Element object. * @param tagName * Name of the tag as String. * @param tagValue * Value of the tag as String. */ public static Element insertTagInElement(Document document, Element elementToAppend, String tagName, String tagValue) { Element newElement = document.createElement(tagName); elementToAppend.appendChild(newElement); newElement.appendChild(document.createTextNode(tagValue)); return newElement; }
From source file:Main.java
public static void createXMLDocument(File file, String rootName) { if (!file.exists()) { try {//w w w .ja v a2 s .c o m DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.newDocument(); Element root = document.createElement(rootName); document.appendChild(root); saveXMLDocument(document, file); } catch (Exception ex) { } } }
From source file:Main.java
public static Node addNode(Document doc, Node parent, String name) { Node child = null;/*from w w w . ja v a 2 s. c om*/ try { child = doc.createElement(name); return parent.appendChild(child); } catch (DOMException ex) { try { if (child != null) parent.removeChild(child); } catch (DOMException ex2) { } return null; } }
From source file:Main.java
public static Document createXMLResult(String rootName, Map<String, String> map) { try {/* w ww.j ava 2 s .c om*/ DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder; docBuilder = dbfac.newDocumentBuilder(); Document doc = docBuilder.newDocument(); Element root = doc.createElement(rootName); doc.appendChild(root); for (String elementName : map.keySet()) { Element child = doc.createElement(elementName); Text text = doc.createTextNode(map.get(elementName)); child.appendChild(text); root.appendChild(child); } return doc; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }
From source file:Main.java
public static Element addCDataElement(final Document dom, final Element el, final String name, final String value) { Element newElement = dom.createElement(name); newElement.appendChild(dom.createCDATASection(value)); el.appendChild(newElement);//from ww w . jav a2s. c o m return el; }
From source file:Main.java
public static Element getElementWithText(Document doc, String elementName, String textValue) { Element elem_text = doc.createElement(elementName); elem_text.appendChild(doc.createTextNode(textValue)); return elem_text; }
From source file:Main.java
public static Element appendNewElement(Document document, Element parent, String element) { Element child = document.createElement(element); parent.appendChild(child);/* ww w . ja va 2 s . c om*/ return child; }
From source file:Main.java
public static Element createSimpleTextElementWithoutNS(final Document documentParent, final String elementTagName, final String elementText) { Element element = documentParent.createElement(elementTagName); element.appendChild(documentParent.createTextNode(elementText)); return element; }
From source file:Main.java
/** * Given a certain parent {@link Element} and {@link Document}, append a * child {@link Element} with Tag Name (which cannot be null), Node * Attribute Name, Node Attribute Value (which both are null at the same * time or none is null at all), Node Value (which can be null). * /*from ww w . ja v a 2s .c o m*/ * @param nodeTagName * Node Tag Name. * @param nodeAttributeName * Node Attribute Name. * @param nodeAttributeValue * Node Attribute Value. * @param nodeValue * Node Value. * @param elementToBeApppendedTo * Parent {@link Element} which the new created {@link Element} * will be appended to. * @param document * {@link Document} which everything belongs to. * * @return Returns the created {@link Element}. */ private static Element appendNewElementToNode(String nodeTagName, String nodeAttributeName, String nodeAttributeValue, String nodeValue, Element elementToBeApppendedTo, Document document) { // create element and append it Element element = document.createElement(nodeTagName); if ((nodeAttributeName != null) && (nodeAttributeValue != null)) { element.setAttribute(nodeAttributeName, nodeAttributeValue); } if (nodeValue != null) { element.setTextContent(nodeValue); } elementToBeApppendedTo.appendChild(element); return element; }