List of usage examples for org.w3c.dom Node appendChild
public Node appendChild(Node newChild) throws DOMException;
newChild
to the end of the list of children of this node. From source file:Main.java
public static Element addElement(Node parent, String name, Attr[] attrs) { Element element;/*from w ww . j a v a 2s .c o m*/ if (parent instanceof Document) { element = ((Document) parent).createElement(name); } else { element = parent.getOwnerDocument().createElement(name); } if (attrs != null && attrs.length > 0) { for (Attr attr : attrs) { element.setAttributeNode(attr); } } parent.appendChild(element); return element; }
From source file:es.itecban.deployment.security.client.ws.LogonWS.java
private static void addTextNode(Element element, String nodeName, String textValue) { // Get the document Document doc = element.getOwnerDocument(); // Write the name Node resourceNameNode = doc.createElement(nodeName); element.appendChild(resourceNameNode); Text resourceNameText = doc.createTextNode(textValue); resourceNameNode.appendChild(resourceNameText); }
From source file:Main.java
public static Node addChildNode(Node pNode, String name, String value) { Document _doc = null;/* w w w . ja va2 s .co m*/ if (pNode.getNodeType() == Node.DOCUMENT_NODE) { _doc = (Document) pNode; } else { _doc = pNode.getOwnerDocument(); } Element _ele = _doc.createElement(name); Node _node = _doc.createTextNode(value); _ele.appendChild(_node); return pNode.appendChild(_ele); }
From source file:Main.java
public static Node duplicate(Node node, Document ownerDocument) { if (node instanceof Text) return ownerDocument.createTextNode(node.getNodeValue()); Node newNode = ownerDocument.createElement(node.getNodeName()); NamedNodeMap attribs = node.getAttributes(); for (int i = 0; i < attribs.getLength(); i++) { Node attrib = attribs.item(i); addAttribute(newNode, attrib.getNodeName(), attrib.getNodeValue()); }//from w ww. jav a2s.c o m for (Node n = node.getFirstChild(); n != null; n = n.getNextSibling()) { Node newN = duplicate(n, ownerDocument); newNode.appendChild(newN); } return newNode; }
From source file:Main.java
static public void SetNodeText(Node node, String value) { NodeList node_;/* w ww . j av a2 s . com*/ Node x; int n, nnode; if (value == null) value = ""; if (node.getNodeType() == Node.TEXT_NODE) node.setNodeValue(value); else { node_ = node.getChildNodes(); nnode = node_.getLength(); if (nnode == 0) { x = (Node) node.getOwnerDocument().createTextNode(value); node.appendChild(x); } else { for (n = 0; n < nnode; n++) { x = (Node) node_.item(n); if (x == null) continue; if (x.getNodeType() == Node.TEXT_NODE) { x.setNodeValue(value); break; } } } } }
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 ww . jav a2 s .c o 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);//w w w . j ava 2 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
/** * Adds a new child element to a parent. * /*from w w w .j av a2 s .c o m*/ * @param parent the Element to which to append the child * @param name the (tag) name of the new child * @param value the text value of the new element. (can be null!) * @return the new child element */ static public Element addElement(Node parent, String name, String value) { if (parent == null) return null; // Fehler // Name must not contain spaces if (name.indexOf(' ') >= 0) name = name.replace(' ', '_'); // Create Element Element child = parent.getOwnerDocument().createElement(name); if (value != null) setElementText(child, value); parent.appendChild(child); return child; }
From source file:Main.java
/** * Creates a Text child, or replaces all existing children of type Text. * Non-Text descendants are preserved without change. * @return The Text child which receives the specified text. * @see Node.setTextContent()//from w ww . j a va2s .c om */ public static Text setText(Node node, String text) { NodeList children = node.getChildNodes(); if (children != null) { for (int i = 0, n = children.getLength(); i < n; ++i) { Node child = children.item(i); if (child instanceof Text) { return ((Text) child).replaceWholeText(text); } } } Text ans = node.getOwnerDocument().createTextNode(text); node.appendChild(ans); return ans; }
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;/*from ww w . j a v a 2s . co m*/ 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); }