Java tutorial
//package com.java2s; import java.text.SimpleDateFormat; import java.util.Date; import org.apache.log4j.Logger; import org.w3c.dom.*; public class Main { public static final SimpleDateFormat sdfObj = new SimpleDateFormat("yyyy-MM-dd_k:mm"); static Logger log = null; public static void createElementAndAppend(String name, int value, Document doc, Element appendeeElement, String attributeName, String attributeValue) { Element newElement = doc.createElement(name); Text text = doc.createTextNode(String.valueOf(value)); newElement.appendChild(text); if (attributeName != null && !attributeName.equals("")) { newElement.setAttribute(attributeName, attributeValue); } appendeeElement.appendChild(newElement); } public static void createElementAndAppend(String name, double value, Document doc, Element appendeeElement, String attributeName, String attributeValue) { Element newElement = doc.createElement(name); Text text = doc.createTextNode(String.valueOf(value)); newElement.appendChild(text); if (attributeName != null && !attributeName.equals("")) { newElement.setAttribute(attributeName, attributeValue); } appendeeElement.appendChild(newElement); } public static void createElementAndAppend(String name, Date value, Document doc, Element appendeeElement, String attributeName, String attributeValue) { Element newElement = null; 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); } 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 = ""; } 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); } }