List of usage examples for org.w3c.dom Node getOwnerDocument
public Document getOwnerDocument();
Document
object associated with this node. From source file:Main.java
static public void setElementText(Element elm, String text) { Node node = elm.getFirstChild(); if (node == null) { if (text != null) elm.appendChild(elm.getOwnerDocument().createTextNode(text)); } else if (node.getNodeType() == Node.TEXT_NODE) { if (text == null) node.getParentNode().removeChild(node); else//ww w. j a v a2 s . c o m node.setNodeValue(text); } else if (text != null) { Text textNode = node.getOwnerDocument().createTextNode(text); elm.insertBefore(textNode, elm.getFirstChild()); } }
From source file:Main.java
/** * Add a CDATA element with attributes.// w w w. j a v a 2 s . c o m * <p/> * NOTE: Serializing a XML document via TRAX changes "\r\n" to "\r\r\n" in * a CDATA section. Serializing with the Xalan XMLSerializer works fine. * * @param parent parent node * @param name node name * @param data node data * @param attrs attributes */ public static Element addCDATAElement(Node parent, String name, String data, Attr[] attrs) { Element element; CDATASection cdata; if (parent instanceof Document) { element = ((Document) parent).createElement(name); /* * Creates a <code>CDATASection</code> node whose value is the * specified. */ cdata = ((Document) parent).createCDATASection(data); } else { element = parent.getOwnerDocument().createElement(name); cdata = parent.getOwnerDocument().createCDATASection(data); } if (attrs != null && attrs.length > 0) { for (Attr attr : attrs) { element.setAttributeNode(attr); } } element.appendChild(cdata); parent.appendChild(element); return element; }
From source file:curam.molsa.test.customfunctions.MOLSADOMReader.java
/** * Get unique child from parent node. If not exists, creates child. * /* w w w . j a v a2s . c o m*/ * @param parent * @param name * @return nodes */ public static Element child(final Node parent, final String name) { final List<Node> nodes = nodes(parent, name); if (nodes.isEmpty()) { final Element element = parent.getOwnerDocument().createElement(name); parent.appendChild(element); return element; } else { if (nodes.size() > 1) { throw new RuntimeException("There are more than one element with name=" + name); } return (Element) nodes.get(0); } }
From source file:Main.java
/** * Sets the text for a node/* w w w. ja v a 2 s .co m*/ */ public synchronized static void setText(Node n, String text) { if (n == null) throw new IllegalArgumentException("Node argument cannot be null"); if (text == null) throw new IllegalArgumentException("Node text argument cannot be null"); NodeList nl = n.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { if (nl.item(i).getNodeType() == Node.TEXT_NODE) { nl.item(i).setNodeValue(text); return; } } Node textNode = n.getOwnerDocument().createTextNode(text); n.appendChild(textNode); }
From source file:net.sf.joost.trax.TrAXHelper.java
/** * Converts a supplied <code>Source</code> to a <code>SAXSource</code>. * @param source The supplied input source * @param errorListener an ErrorListener object * @return a <code>SAXSource</code> */// w ww . j ava 2s .c o m public static SAXSource getSAXSource(Source source, ErrorListener errorListener) throws TransformerException { if (DEBUG) log.debug("getting a SAXSource from a Source"); //SAXSource if (source instanceof SAXSource) { if (DEBUG) log.debug("source is an instance of SAXSource, so simple return"); return (SAXSource) source; } //DOMSource if (source instanceof DOMSource) { if (DEBUG) log.debug("source is an instance of DOMSource"); InputSource is = new InputSource(); Node startNode = ((DOMSource) source).getNode(); Document doc; if (startNode instanceof Document) { doc = (Document) startNode; } else { doc = startNode.getOwnerDocument(); } if (DEBUG) log.debug("using DOMDriver"); DOMDriver driver = new DOMDriver(); driver.setDocument(doc); is.setSystemId(source.getSystemId()); driver.setSystemId(source.getSystemId()); return new SAXSource(driver, is); } //StreamSource if (source instanceof StreamSource) { if (DEBUG) log.debug("source is an instance of StreamSource"); InputSource isource = getInputSourceForStreamSources(source, errorListener); return new SAXSource(isource); } else { String errMsg = "Unknown type of source"; if (log != null) log.error(errMsg); IllegalArgumentException iE = new IllegalArgumentException(errMsg); TransformerConfigurationException tE = new TransformerConfigurationException(iE.getMessage(), iE); if (errorListener != null) errorListener.error(tE); else throw tE; return null; } }
From source file:com.portfolio.data.utils.DomUtils.java
public static String getInnerXml(Node node) { DOMImplementationLS lsImpl = (DOMImplementationLS) node.getOwnerDocument().getImplementation() .getFeature("LS", "3.0"); LSSerializer lsSerializer = lsImpl.createLSSerializer(); NodeList childNodes = node.getChildNodes(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < childNodes.getLength(); i++) { sb.append(lsSerializer.writeToString(childNodes.item(i))); }/*from w ww .j a v a 2 s .co m*/ // TODO Comprendre pourquoi CDATA est mal ferm if (sb.toString().startsWith("<![CDATA[")) sb.append("]]>"); return DomUtils.filtrerInnerXml(sb.toString()); }
From source file:be.fedict.eid.applet.service.signer.facets.XAdESXLSignatureFacet.java
public static Element createNamespaceElement(Node documentNode) { Document document = documentNode.getOwnerDocument(); Element nsElement = document.createElement("nsElement"); nsElement.setAttributeNS(Constants.NamespaceSpecNS, "xmlns:ds", Constants.SignatureSpecNS); nsElement.setAttributeNS(Constants.NamespaceSpecNS, "xmlns:xades", XADES_NAMESPACE); return nsElement; }
From source file:Main.java
/** * Sets a named attribute of a Node/*from www .j ava2 s . c om*/ * @param node The node * @param attr The name of the attribute to set * @param value The value to assign to the attribute */ public synchronized static void setAttribute(Node node, String attr, String value) { if (node == null) throw new IllegalArgumentException("Node argument cannot be null"); if (attr == null) throw new IllegalArgumentException("Node attribute argument cannot be null"); if (value == null) throw new IllegalArgumentException("Node attribute value argument cannot be null"); Node attrN = null; NamedNodeMap map = node.getAttributes(); if (map != null) attrN = map.getNamedItem(attr); if (attrN == null) { attrN = node.getOwnerDocument().createAttribute(attr); map.setNamedItem(attrN); } attrN.setNodeValue(value); }
From source file:Main.java
/** * Sets the text value of an Element. if current text of the element is * replaced with the new text if text is null any * current text value is deleted./* w w w . j a v a2 s . c o m*/ * * @param elem the Element for which the text value should be set * @param text the new text value of the element * @return true if the text could be set or false otherwise */ static public boolean setElementText(Node elem, Object text) { if (elem == null) return false; // Fehler // Find Text Node node = elem.getFirstChild(); while (node != null) { // Find all Text nodes if (node.getNodeType() == Node.TEXT_NODE) break; // gefunden node = node.getNextSibling(); } if (node != null) { // Set or remove text if (text != null) node.setNodeValue(text.toString()); else elem.removeChild(node); } else if (text != null) { // Add Text elem.appendChild(elem.getOwnerDocument().createTextNode(text.toString())); } return true; }
From source file:com.evolveum.midpoint.prism.xml.XmlTypeConverter.java
public static void appendBelowNode(Object val, QName xsdType, QName name, Node parentNode, boolean recordType) throws SchemaException { Object xsdElement = toXsdElement(val, xsdType, name, parentNode.getOwnerDocument(), recordType); if (xsdElement == null) { return;/* w ww . ja v a 2 s.c om*/ } if (xsdElement instanceof Element) { parentNode.appendChild((Element) xsdElement); // } else if (xsdElement instanceof JAXBElement) { // try { // JAXBUtil.marshal(xsdElement, parentNode); // } catch (JAXBException e) { // throw new SchemaException("Error marshalling element " + xsdElement + ": " + e.getMessage(), e); // } } else { throw new IllegalStateException("The XSD type converter returned unknown element type: " + xsdElement + " (" + xsdElement.getClass().getName() + ")"); } }