List of usage examples for org.w3c.dom Document createElement
public Element createElement(String tagName) throws DOMException;
From source file:Main.java
public static void createElementAndAppend(String name, Date value, Document doc, Element appendeeElement, String attributeName, String attributeValue) { Element newElement = null;//from w w w. j a v a2 s . c o m if (value == null) { log.info("XMLUtil.createElementAndAppend() value == null for name = " + name); newElement = doc.createElement(name); Text text = doc.createTextNode(""); newElement.appendChild(text); } else { newElement = doc.createElement(name); Text text = doc.createTextNode(sdfObj.format(value)); newElement.appendChild(text); } if (attributeName != null && !attributeName.equals("")) { newElement.setAttribute(attributeName, attributeValue); } appendeeElement.appendChild(newElement); }
From source file:de.betterform.xml.xforms.model.constraints.RelevanceSelector.java
private static void addElement(Element relevantParent, Node instanceNode) { Document relevantDocument = relevantParent.getOwnerDocument(); Element relevantElement;// ww w . j a v a 2 s. c om if (instanceNode.getNamespaceURI() == null) { relevantElement = relevantDocument.createElement(instanceNode.getNodeName()); } else { relevantElement = relevantDocument.createElementNS(instanceNode.getNamespaceURI(), instanceNode.getNodeName()); } // needed in instance serializer ... relevantElement.setUserData("", instanceNode.getUserData(""), null); relevantParent.appendChild(relevantElement); addAttributes(relevantElement, instanceNode); addChildren(relevantElement, instanceNode); }
From source file:Main.java
/** * Create new XML document./*from w w w. j av a2 s .c o m*/ * * @param rootElementName name of the root element to add, or <code>null</code> if the * document should not have any root just yet * @throws ParserConfigurationException */ public static Document createEmptyDocument(String rootElementName, String namespace) throws ParserConfigurationException { final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); final DocumentBuilder builder = factory.newDocumentBuilder(); Document result = builder.newDocument(); if (rootElementName != null) { Element rootElement; if (namespace != null && !namespace.isEmpty()) { rootElement = result.createElementNS(rootElementName, namespace); } else { rootElement = result.createElement(rootElementName); } result.appendChild(rootElement); } return result; }
From source file:Main.java
public static Element appendNewElement(Document document, Element parent, String element, Object content, String namespace) {/* w w w . ja v a 2 s .c o m*/ Element childElement; if (namespace != null) { childElement = document.createElementNS(namespace, element); } else { childElement = document.createElement(element); } if (content != null) { // TODO: We'll have that on Android 2.2: // childElement.setTextContent(content.toString()); // Meanwhile: childElement.appendChild(document.createTextNode(content.toString())); } parent.appendChild(childElement); return childElement; }
From source file:com.omertron.thetvdbapi.tools.DOMHelper.java
/** * Add a child element to a parent element * * @param doc//from w w w . java 2s . c om * @param parentElement * @param elementName * @param elementValue */ public static void appendChild(Document doc, Element parentElement, String elementName, String elementValue) { Element child = doc.createElement(elementName); Text text = doc.createTextNode(elementValue); child.appendChild(text); parentElement.appendChild(child); }
From source file:Main.java
public static String map2Xml(Map<String, String> content, String root_elem_id, String item_elem_id) throws Exception { DocumentBuilder b = documentBuilder(); Document doc = b.newDocument(); String str = null;/*from w ww . j a va 2s.co m*/ SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance(); ByteArrayOutputStream out = new ByteArrayOutputStream(); Element root = doc.createElement(root_elem_id); doc.appendChild(root); // Now, add all the children for (Entry<String, String> e : content.entrySet()) { Element item = doc.createElement(item_elem_id); item.setAttribute("id", e.getKey()); CDATASection data = doc.createCDATASection(e.getValue()); item.appendChild(data); root.appendChild(item); } try { DOMSource ds = new DOMSource(doc); StreamResult sr = new StreamResult(out); TransformerHandler th = tf.newTransformerHandler(); th.getTransformer().setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); Properties format = new Properties(); format.put(OutputKeys.METHOD, "xml"); // format.put("{http://xml. customer .org/xslt}indent-amount", "4"); // format.put("indent-amount", "4"); // format.put(OutputKeys.DOCTYPE_SYSTEM, "myfile.dtd"); format.put(OutputKeys.ENCODING, "UTF-8"); format.put(OutputKeys.INDENT, "yes"); th.getTransformer().setOutputProperties(format); th.setResult(sr); th.getTransformer().transform(ds, sr); str = out.toString(); } catch (Exception e) { e.printStackTrace(); } return str; }
From source file:com.google.visualization.datasource.render.HtmlRenderer.java
/** * Appends <html>, <head>, <title>, and <body> elements to the document. * * @param document The containing document. * * @return The <body> element. *//*from w w w .j av a2 s. c om*/ private static Element appendHeadAndBody(Document document) { Element htmlElement = document.createElement("html"); document.appendChild(htmlElement); Element headElement = document.createElement("head"); htmlElement.appendChild(headElement); Element titleElement = document.createElement("title"); titleElement.setTextContent("Google Visualization"); headElement.appendChild(titleElement); Element bodyElement = document.createElement("body"); htmlElement.appendChild(bodyElement); return bodyElement; }
From source file:Main.java
public static Element cloneElementAs(Element srcEl, Document dstDoc, String elName) { if (srcEl.getNodeName().equals(elName)) { if (srcEl.getOwnerDocument() == dstDoc) { return (Element) srcEl.cloneNode(true); } else {//from w w w . j a v a 2 s . c o m return (Element) dstDoc.importNode(srcEl, true); } } else { final Element dstEl = dstDoc.createElement(elName); final NodeList srcChildren = srcEl.getChildNodes(); final int n = srcChildren.getLength(); for (int i = 0; i < n; ++i) { final Node srcChild = srcChildren.item(i); final Node dstChild = dstDoc.importNode(srcChild, true); dstEl.appendChild(dstChild); } return dstEl; } }
From source file:Main.java
/** * Creates a new <code>Node</code>-object with the specified <code>name</code> * and appends it as a child element to the provided <code>Node</code>- * parameter.//from w ww. j ava 2s . co m * * @param elem the <code>Node</code> to which the newly created * <code>Node</code> will be appended to. Not allowed to be <code>null</code>. * @param elemName the name of the to-be-created <code>Element</code>, not allowed * to be empty or <code>null</code>. * @return the created element * @exception IllegalArgumentException if <code>elem</code> and/ord * <code>elemName</code> are null (or empty in the case of <code>elemName</code>) */ public static Element createChild(Node node, String elemName) { if (node == null || elemName == null || "".equals(elemName)) throw new IllegalArgumentException("Arguments are not allowed to be null or empty"); Document document = null; if (node instanceof Document) { document = (Document) node; } else if (node.getOwnerDocument() != null) { document = node.getOwnerDocument(); } Element newChild = null; if (document != null) { newChild = document.createElement(elemName); node.appendChild(newChild); } return newChild; }
From source file:Main.java
public static void createElementAndAppend(String name, String value, Document doc, Element appendeeElement, String attributeName, String attributeValue) { if (value == null || value.equals("")) { log.info("XMLUtil.createElementAndAppend() value == null for name = " + name + "."); value = ""; }//from w w w .jav a2s . co m Element newElement = doc.createElement(name); Text text = doc.createTextNode(value); newElement.appendChild(text); if (attributeName != null && !attributeName.equals("")) { newElement.setAttribute(attributeName, attributeValue); } appendeeElement.appendChild(newElement); }