List of usage examples for org.w3c.dom Element setAttribute
public void setAttribute(String name, String value) throws DOMException;
From source file:Main.java
static public Document createDefaultMessage(String messagetype) { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentbuilder = null; try {//from w w w .ja v a 2 s . c o m documentbuilder = factory.newDocumentBuilder(); } catch (ParserConfigurationException e) { throw new RuntimeException(e); } Document doc = documentbuilder.newDocument(); Element root = doc.createElement("message"); doc.appendChild(root); long timestamp = System.currentTimeMillis(); root.setAttribute("timestamp", Long.toString(timestamp)); root.setAttribute("type", messagetype); return doc; }
From source file:Main.java
public static void setNamedChildValue(Element element, String name, String value) throws Exception { Element e = getNamedChild(element, name); if (e == null) throw new Exception("unable to find element " + name); e.setAttribute("value", value); }
From source file:Main.java
public static Element newElementWithAttrs(String tagName, Map<String, String> attrs) { Document document = newDocument(); Element element = newElement(document, tagName); for (Entry<String, String> entry : attrs.entrySet()) { element.setAttribute(entry.getKey(), entry.getValue()); }//from ww w. j a v a2s. c o m return element; }
From source file:Main.java
/** * copy all attributes form one element to another * @param fromEl// w ww. java 2 s .c o m * @param toEl */ public static void copyAttributes(Element fromEl, Element toEl) { for (int a = 0; a < fromEl.getAttributes().getLength(); a++) { Attr at = (Attr) fromEl.getAttributes().item(a); toEl.setAttribute(at.getName(), at.getValue()); } }
From source file:DOMEdit.java
public static void addAttribute(Document doc) { Element root = doc.getDocumentElement(); Element person = (Element) root.getFirstChild(); person.setAttribute("company", "yourCom"); }//from w ww . ja v a2 s . c o m
From source file:Main.java
/** * Add the breadcrumbs element to the document. This contains the * information for navigating back up through the application hierarchy. * * This creates a breadcrumbs element with child path elements in order * (each with a name and path attribute). The breadcrumbs element itself * also has a path attribute that specifies the root path. * * @param document/*from www . jav a2 s. c o m*/ * @param pathPrefix * @param relativePath */ public static void addBreadcrumbs(Document document, String pathPrefix, String relativePath) { if (document != null) { Element root = document.getDocumentElement(); if (root != null) { // Create the breadcrumbs element with the associated path being // the root path for the set of paths. Element element = document.createElement("breadcrumbs"); element.setAttribute("path", pathPrefix); StringBuilder path = new StringBuilder(pathPrefix); if (!"".equals(pathPrefix) && !pathPrefix.endsWith("/")) { path.append("/"); } boolean first = true; for (String term : relativePath.split("/")) { if (!"".equals(term) && !".".equals(term)) { if (!first) { path.append("/"); } path.append(term); Element pathElement = document.createElement("crumb"); pathElement.setAttribute("name", term); pathElement.setAttribute("path", path.toString()); element.appendChild(pathElement); first = false; } } root.appendChild(element); } } }
From source file:Main.java
public static void addNewNode(File xmlfile, String nodeXpath, String elementName, String attributeName, String attributeValue) throws Exception { // Get the Document object. //logger.debug("Add node method, adding a new node in xml file " + xmlfile.toString()); Document doc = getDocument(xmlfile); // get the Node Object for the xpath. Node node = getNodeObject(nodeXpath, doc); // Create a New Element Element element = doc.createElement(elementName); // Set the Attributes in to the Element element.setAttribute(attributeName, attributeValue); // Append The Element as a child in side the Parent Node node.appendChild(element);//from w w w. j a v a 2s.co m wirteXmlFile(doc, xmlfile); //logger.debug("New node is added to the xml file"); }
From source file:Main.java
/** * Method to delete old elements and add new elements in target file based on tag name of source file. * * @param source/* w w w. jav a 2 s .co m*/ * @param target * @param sourceTagName * @param targetTagNameParent * @param targetTagNameChild * @return document * @throws ParseException */ public static Document addNewElements(Document source, Document target, String sourceTagName, String targetTagNameParent, String targetTagNameChild, List<String> ignoreList) throws ParseException { //list for attributes to be more dynamic if reuse //remove old entries target = removeAllOptions(target); // add new entries NodeList nameList = source.getElementsByTagName(sourceTagName); NodeList parents = target.getElementsByTagName(targetTagNameParent); Element parent = (Element) parents.item(0); // g:options - only 1 // element //add space placeholder as first entry Element o = target.createElement(targetTagNameChild); o.setAttribute("g:value", ""); o.setAttribute("g:label", ""); parent.appendChild(o); for (int i = 0; i < nameList.getLength(); i++) { String value = nameList.item(i).getTextContent(); if (value != null && !value.equals("") && !ignoreList.contains(value)) { Element p = target.createElement(targetTagNameChild); p.setAttribute("g:value", value); p.setAttribute("g:label", value); parent.appendChild(p); } } return changeDate(target); }
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 w w . j a v a2 s.c om 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:Main.java
/** * adds an Element with one attribut to a DOM-tree. * //w ww. j a v a2 s.c o m * @param document * Document: the DOM-tree to add to * @param father * Element: the new element will be inserted directly under this * Element in the tree * @param name * String: the name of the new element * @param attOneName * String: the name of the attribut * @param attOneValue * String: the value of the attribut */ public static void addElement(Document document, Element father, String name, String attOneName, String attOneValue) { Element element = document.createElement(name); element.setAttribute(attOneName, attOneValue); father.appendChild(element); }