List of usage examples for org.w3c.dom Element setTextContent
public void setTextContent(String textContent) throws DOMException;
From source file:Main.java
public static Element createElementNsIn(Element parent, String ns, String name, String textContent) { Element el = parent.getOwnerDocument().createElementNS(ns, name); parent.appendChild(el);/* w w w . j a v a2 s . c om*/ el.setTextContent(textContent); return el; }
From source file:Main.java
public static void addNodeToXml(String nodeParentXpathStr, String xmlFilePath, String nodeName, String value, Map<String, String> attr) throws Exception { Document doc = null;// w w w. j a va 2s.com if (xmlFilePath == null || nodeParentXpathStr == null || nodeName == null || nodeParentXpathStr == null) throw new Exception("some parameters can not be null!"); doc = dombuilder.parse(new File(xmlFilePath)); Node pNode = (Node) xpath.compile(nodeParentXpathStr).evaluate(doc, XPathConstants.NODE); if (pNode == null) throw new Exception("can not find the node specified in nodeParentXpathStr!"); ; Element newNode = doc.createElement(nodeName); newNode.setTextContent(value); if (attr != null && !attr.isEmpty()) { for (String key : attr.keySet()) newNode.setAttribute(key, attr.get(key)); } pNode.appendChild(newNode); writeToXmlFile(doc, xmlFilePath); }
From source file:Main.java
/** * Method for handling serialization of class objects. * @author Tristan Bepler//from w ww . ja va2 s . co m */ private static boolean writeSpecialCaseObjectIsClass(Object o, Element cur, Document doc, List<Object> written) { if (o instanceof Class) { cur.setTextContent(((Class) o).getName()); return true; } return false; }
From source file:Main.java
/** * Method for handling serialization of String objects. * @author Tristan Bepler//ww w .j a va2 s .co m */ private static boolean writeSpecialCaseObjectIsString(Object o, Element cur, Document doc, List<Object> written) { if (o instanceof String) { cur.setTextContent((String) o); return true; } return false; }
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); for (Iterator itrName = attri.keySet().iterator(); itrName.hasNext();) { String attriKey = (String) itrName.next(); String attriValue = (String) attri.get(attriKey); device.setAttribute(attriKey, attriValue); }//w ww. j a v a2s . c o m 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); for (Iterator itrName = attri.keySet().iterator(); itrName.hasNext();) { String attriKey = (String) itrName.next(); String attriValue = (String) attri.get(attriKey); device.setAttribute(attriKey, attriValue); }// ww w . j a v a 2 s.c o m 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
/** * Creates and returns an element with the specified tag and text content * @param tag the tag of the element// www.j av a2 s . c o m * @param textContent the text content of the element * @param xml the Document to use to create the element * @return The element with the specified tag and text content */ public static Element createElement(String tag, String textContent, Document xml) { Element element = xml.createElement(tag); //Sets the text content of the element element.setTextContent(textContent); return element; }
From source file:Main.java
private static void attributize(Element root) { NamedNodeMap attributeMap = root.getAttributes(); for (int i = 0; i < attributeMap.getLength(); i++) { org.w3c.dom.Attr attr = (Attr) attributeMap.item(i); Element attrElement = root.getOwnerDocument().createElement(attr.getName()); attrElement.setTextContent(attr.getValue()); root.appendChild(attrElement);/*from w w w.j a v a 2 s . c o m*/ } NodeList children = root.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { if (children.item(i) instanceof Element) { attributize((Element) children.item(i)); } } }
From source file:Main.java
public static Element createElement(Node node, String name, String value, Map<String, Object> attributes) { Document doc = node.getNodeType() == Node.DOCUMENT_NODE ? (Document) node : node.getOwnerDocument(); Element element = doc.createElement(name); element.setTextContent(value); addAttributes(element, attributes);/*from w w w.j av a 2s. c o m*/ return element; }
From source file:Main.java
/** * Gets the arg element// w w w . j av a2 s . c o m * @param text the text content * @param xml the Document to use to create the element * @return the arg element */ public static Element getArg(String text, Document xml, boolean value) { Element arg = xml.createElement("arg"); if (value) arg.setAttribute("value", text); else arg.setTextContent(text); return arg; }