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 addApp(String file, String name, Hashtable<String, String> attri) throws Exception { DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance(); DocumentBuilder dombuilder = domfac.newDocumentBuilder(); FileInputStream is = new FileInputStream(file); Document doc = dombuilder.parse(is); NodeList nodeList = doc.getElementsByTagName("app"); if (nodeList != null && nodeList.getLength() >= 1) { Node deviceNode = nodeList.item(0); Element device = doc.createElement("aut"); device.setTextContent(name);/*from w w w .ja va2 s.co m*/ for (Iterator itrName = attri.keySet().iterator(); itrName.hasNext();) { String attriKey = (String) itrName.next(); String attriValue = (String) attri.get(attriKey); device.setAttribute(attriKey, attriValue); } deviceNode.appendChild(device); } //save TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); Properties props = t.getOutputProperties(); props.setProperty(OutputKeys.ENCODING, "GB2312"); t.setOutputProperties(props); DOMSource dom = new DOMSource(doc); StreamResult sr = new StreamResult(file); t.transform(dom, sr); }
From source file:Main.java
public static void addHandset(String file, String name, Hashtable<String, String> attri) throws Exception { DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance(); DocumentBuilder dombuilder = domfac.newDocumentBuilder(); FileInputStream is = new FileInputStream(file); Document doc = dombuilder.parse(is); NodeList nodeList = doc.getElementsByTagName("devices"); if (nodeList != null && nodeList.getLength() >= 1) { Node deviceNode = nodeList.item(0); Element device = doc.createElement("device"); device.setTextContent(name);/*from w w w. ja v a2 s . co m*/ for (Iterator itrName = attri.keySet().iterator(); itrName.hasNext();) { String attriKey = (String) itrName.next(); String attriValue = (String) attri.get(attriKey); device.setAttribute(attriKey, attriValue); } deviceNode.appendChild(device); } //save TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); Properties props = t.getOutputProperties(); props.setProperty(OutputKeys.ENCODING, "GB2312"); t.setOutputProperties(props); DOMSource dom = new DOMSource(doc); StreamResult sr = new StreamResult(file); t.transform(dom, sr); }
From source file:Utils.java
public static Element findElementElseCreateAndAttribute(Document document, Element parent, String element, String attributeName, String attributeValue) { NodeList nl = parent.getElementsByTagName(element); Element e = null;/*from w w w . j a v a 2s . c o m*/ if (nl.getLength() == 0) { parent.appendChild(document.createElement(element)); e = (Element) parent.getElementsByTagName(element).item(0); e.setAttribute(attributeName, attributeValue); } return e; }
From source file:Main.java
/** * Adds a new empty {@link Element} to the given parent {@link Element}. * // w ww . ja va2 s . c om * @param doc the document to add to. * @param parent the parent element. If {@code null} then the new child * will be added directly to the document. * @param name the name of the new element. * @return the created element. */ public static Element addElement(Document doc, Node parent, String name) { if (doc == null) { doc = parent.getOwnerDocument(); } final Element elem = doc.createElement(name); if (parent == null) { parent = doc; } parent.appendChild(elem); return elem; }
From source file:Main.java
public static Element getContactTypeWithText(Document doc, String elementName, String name, String title, String street, String city, String state, String zip, String officePhone, String mobilePhone, String email) {// w w w. j av a2s . c om Element elem_contact_type = doc.createElement(elementName); Element elem_name = getElementWithText(doc, "Name", name); Element elem_title = getElementWithText(doc, "Title", title); Element elem_address = getAddressTypeWithText(doc, street, city, state, zip); Element elem_officePhone = getPhoneTypeWithText(doc, "OfficePhone", officePhone); Element elem_mobilePhone = getPhoneTypeWithText(doc, "MobilePhone", mobilePhone); Element elem_email = getElementWithText(doc, "Email", email); elem_contact_type.appendChild(elem_name); elem_contact_type.appendChild(elem_title); if (street != null) { elem_contact_type.appendChild(elem_address); } if (officePhone != null) { elem_contact_type.appendChild(elem_officePhone); } if (mobilePhone != null) { elem_contact_type.appendChild(elem_mobilePhone); } elem_contact_type.appendChild(elem_email); return elem_contact_type; }
From source file:Main.java
private static Element addNewProfileSection(Document document, Element profiles, String profileIdVal) { //start with profile node Element profile = document.createElement(PROFILE_NODE_NAME); profiles.appendChild(profile);//from w w w.ja v a 2 s. co m //create the id tag Node id = document.createElement(PROFILE_ID_NAME); //id.setNodeValue(profileIdVal); id.setTextContent(profileIdVal); //add it to the profile profile.appendChild(id); //create activation tag Node activeByDefault = document.createElement(ACTIVEBYDEFAULT_NAME); //activeByDefault.setNodeValue(ACTIVEBYDEFAULT_VAL); activeByDefault.setTextContent(ACTIVEBYDEFAULT_VAL); Node activation = document.createElement(ACTIVATION_NAME); activation.appendChild(activeByDefault); //add it to profile profile.appendChild(activation); //create build tag Node build = document.createElement(BUILD_NAME); //add it to profile profile.appendChild(build); //create plugins tag, add to build Node plugins = document.createElement(PLUGINS_NAME); build.appendChild(plugins); return (Element) plugins; }
From source file:Main.java
/** * Converts a Map's keys and values to an XML document where the keys are * the elments, and the values are the textnodes (where value is String) or * subelements (where value is Map). In the case of a map entry with a List * as the value, it will create multiple elements, named by the key, and the * values will be the contents of the list, in list order. Note: use a * sorted list if you care about order./*from w w w . java 2 s . c o m*/ * <p/> * The following code, will produce the XML below it. For more examples, * you may look at the unit tests for MapTest * <pre> * final Map parameters; * final Map subElements; * final Map innerElements; * parameters = new HashMap(); * subElements = new HashMap(); * innerElements = new HashMap(); * parameters.put("element1", "value1"); * parameters.put("element2", "value2"); * parameters.put("element3", "value3"); * parameters.put("element4", subElements); * subElements.put("subelement1", "value1"); * subElements.put("subelement2", "value2"); * subElements.put("innermost", innerElements); * innerElements.put("innerelement1", "innervalue1"); * innerElements.put("innerelement2", "innervalue2"); * System.out.println(XMLUtil.mapToXML("root", parameters)); * </pre> * <pre> * <?xml version="1.0" encoding="UTF-8"?> * <root> * <element4> * <subelement2>value2</subelement2> * <subelement1>value1</subelement1> * <innermost> * <innerelement1>innervalue1</innerelement1> * <innerelement2>innervalue2</innerelement2> * </innermost> * </element4> * <element2>value2</element2> * <element3>value3</element3> * <element1>value1</element1> * </root> * </pre> * * @param rootElementName the name that you want the root element to have * @param elements the elements, whether a list of elements or a map * of key/value pairs * * @return the string representation of the XML document * * @throws TransformerException if an XSL transformation exception * occurs * @throws ParserConfigurationException if a JAXP configuration error * occurs */ public static String mapToXML(final String rootElementName, final Object elements) throws TransformerException, ParserConfigurationException { final Document mapDoc; final Element parent; mapDoc = createDocument(); parent = mapDoc.createElement(rootElementName); mapDoc.appendChild(parent); mapToNode(elements, parent, mapDoc, null); return documentToString(mapDoc); }
From source file:it.unitn.disi.smatch.webapi.model.JSONHelper.java
public static Document wrapJsonToXml(JSONObject obj) throws ParserConfigurationException { Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); Element root = doc.createElement(TAG_WEBAPI); Element el = doc.createElement(TAG_JSON_DATA); Text txt = doc.createTextNode(obj.toString()); el.appendChild(txt);//from w ww .ja v a 2s. c o m root.appendChild(el); doc.appendChild(root); return doc; }
From source file:de.betterform.xml.xforms.model.constraints.RelevanceSelector.java
private static void addElement(Document relevantDocument, Node instanceNode) { Element relevantElement;/*from w w w . j a va 2s . co m*/ if (instanceNode.getNamespaceURI() == null) { relevantElement = relevantDocument.createElement(instanceNode.getNodeName()); } else { relevantElement = relevantDocument.createElementNS(instanceNode.getNamespaceURI(), instanceNode.getNodeName()); } relevantDocument.appendChild(relevantElement); addAttributes(relevantElement, instanceNode); addChildren(relevantElement, instanceNode); }
From source file:Main.java
/** * * Convenience method to transfer a node (and all of its children) from one * * DOM XML document to another./*from w w w . j av a 2s . c o m*/ * * * * Note: this method is recursive. * * * * @param current the current Element to append the transfer to * * @param target the target document for the transfer * * @param n the Node to transfer * * @return Element the current element. */ public static Element transferNode(Element current, Document target, Node n) { String name = n.getNodeName(); String value = n.getNodeValue(); short type = n.getNodeType(); if (type == Node.ELEMENT_NODE) { // create a new element for this node in the target document Element e = target.createElement(name); // move all the attributes over to the target document NamedNodeMap attrs = n.getAttributes(); for (int i = 0; i < attrs.getLength(); i++) { Node a = attrs.item(i); e.setAttribute(a.getNodeName(), a.getNodeValue()); } // get the children for this node NodeList children = n.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { // transfer each of the children to the new document transferNode(e, target, children.item(i)); } // append the node to the target document element current.appendChild(e); } else if (type == Node.TEXT_NODE) { Text text = target.createTextNode(value); current.appendChild(text); } return current; }