List of usage examples for org.w3c.dom Document createCDATASection
public CDATASection createCDATASection(String data) throws DOMException;
CDATASection
node whose value is the specified string. From source file:Main.java
public static CDATASection addCDataText(Node node, String data) { Document doc = null; if (node.getNodeType() == Node.DOCUMENT_NODE) { doc = (Document) node;/*from ww w. ja va 2s . com*/ } else { doc = node.getOwnerDocument(); } CDATASection e = doc.createCDATASection(data); node.appendChild(e); return e; }
From source file:Main.java
public static Element createElmWithText(Document doc, String tagName, String text) { Element elm = doc.createElement(tagName); text = text != null ? text.trim() : ""; Node node = (text.indexOf('<') >= 0 || text.indexOf('\n') >= 0) ? doc.createCDATASection(text) : doc.createTextNode(text);//from w ww .jav a2s . c o m elm.appendChild(node); return elm; }
From source file:Main.java
public static Node cloneNode(Document d, Node n) { Node r = null;/* www.j av a 2 s. co m*/ switch (n.getNodeType()) { case Node.TEXT_NODE: r = d.createTextNode(((Text) n).getData()); break; case Node.CDATA_SECTION_NODE: r = d.createCDATASection(((CDATASection) n).getData()); break; case Node.ELEMENT_NODE: r = d.createElement(((Element) n).getTagName()); NamedNodeMap map = n.getAttributes(); for (int i = 0; i < map.getLength(); i++) { ((Element) r).setAttribute(((Attr) map.item(i)).getName(), ((Attr) map.item(i)).getValue()); } break; } return r; }
From source file:DOMEdit.java
public static void addCDATA(Document doc) { Element root = doc.getDocumentElement(); Element place = (Element) root.getFirstChild(); Element directions = (Element) place.getLastChild(); String dirtext = "cdData."; CDATASection dirdata = doc.createCDATASection(dirtext); directions.replaceChild(dirdata, directions.getFirstChild()); }/* www.jav a 2s. c om*/
From source file:Main.java
/** * Set the text of the specified element to the given string. * /*from w w w. j a v a2s.c o m*/ * @param e The element. * @param text The text string. */ public static void setText(Element e, String text) { NodeList lst = e.getChildNodes(); int size = lst.getLength(); for (int i = 0; i < size; i++) { Node n = lst.item(i); if (n.getNodeType() == Node.TEXT_NODE) { Text t = (Text) n; t.setData(text.trim()); return; } } Document doc = e.getOwnerDocument(); // bit of a hack - we preserve the cdata on the way in so we can serialize correctly // This only works on xml to xml // TODO need to have a "preserve format" or some such on the mdmi structure if (text.startsWith("<![CDATA[") && text.endsWith("]]>")) { CDATASection cdata = doc .createCDATASection(text != null ? text.substring(9, text.lastIndexOf("]]>")) : null); e.appendChild(cdata); } else { Text txt = doc.createTextNode(text != null ? text.trim() : null); e.appendChild(txt); } }
From source file:Main.java
@SuppressWarnings("null") public static void copyInto(Node src, Node dest) throws DOMException { Document factory = dest.getOwnerDocument(); //Node start = src; Node parent = null;/* w ww. j av a2s .c o m*/ Node place = src; // traverse source tree while (place != null) { // copy this node Node node = null; int type = place.getNodeType(); switch (type) { case Node.CDATA_SECTION_NODE: { node = factory.createCDATASection(place.getNodeValue()); break; } case Node.COMMENT_NODE: { node = factory.createComment(place.getNodeValue()); break; } case Node.ELEMENT_NODE: { Element element = factory.createElement(place.getNodeName()); node = element; NamedNodeMap attrs = place.getAttributes(); int attrCount = attrs.getLength(); for (int i = 0; i < attrCount; i++) { Attr attr = (Attr) attrs.item(i); String attrName = attr.getNodeName(); String attrValue = attr.getNodeValue(); element.setAttribute(attrName, attrValue); /* if (domimpl && !attr.getSpecified()) { ((Attr) element.getAttributeNode(attrName)).setSpecified(false); } */ } break; } case Node.ENTITY_REFERENCE_NODE: { node = factory.createEntityReference(place.getNodeName()); break; } case Node.PROCESSING_INSTRUCTION_NODE: { node = factory.createProcessingInstruction(place.getNodeName(), place.getNodeValue()); break; } case Node.TEXT_NODE: { node = factory.createTextNode(place.getNodeValue()); break; } default: { throw new IllegalArgumentException( "can't copy node type, " + type + " (" + node.getNodeName() + ')'); } } dest.appendChild(node); // iterate over children if (place.hasChildNodes()) { parent = place; place = place.getFirstChild(); dest = node; } else if (parent == null) { place = null; } else { // advance place = place.getNextSibling(); while (place == null && parent != null && dest != null) { place = parent.getNextSibling(); parent = parent.getParentNode(); dest = dest.getParentNode(); } } } }
From source file:Main.java
public static void copyInto(Node src, Node dest) throws DOMException { Document factory = dest.getOwnerDocument(); //Node start = src; Node parent = null;//from ww w.j a v a 2s . com Node place = src; // traverse source tree while (place != null) { // copy this node Node node = null; int type = place.getNodeType(); switch (type) { case Node.CDATA_SECTION_NODE: { node = factory.createCDATASection(place.getNodeValue()); break; } case Node.COMMENT_NODE: { node = factory.createComment(place.getNodeValue()); break; } case Node.ELEMENT_NODE: { Element element = factory.createElement(place.getNodeName()); node = element; NamedNodeMap attrs = place.getAttributes(); int attrCount = attrs.getLength(); for (int i = 0; i < attrCount; i++) { Attr attr = (Attr) attrs.item(i); String attrName = attr.getNodeName(); String attrValue = attr.getNodeValue(); element.setAttribute(attrName, attrValue); } break; } case Node.ENTITY_REFERENCE_NODE: { node = factory.createEntityReference(place.getNodeName()); break; } case Node.PROCESSING_INSTRUCTION_NODE: { node = factory.createProcessingInstruction(place.getNodeName(), place.getNodeValue()); break; } case Node.TEXT_NODE: { node = factory.createTextNode(place.getNodeValue()); break; } default: { throw new IllegalArgumentException( "can't copy node type, " + type + " (" + place.getNodeName() + ')'); } } dest.appendChild(node); // iterate over children if (place.hasChildNodes()) { parent = place; place = place.getFirstChild(); dest = node; } else if (parent == null) { place = null; } else { // advance place = place.getNextSibling(); while (place == null && parent != null) { place = parent.getNextSibling(); parent = parent.getParentNode(); dest = dest.getParentNode(); } } } }
From source file:Main.java
/** * Copies the source tree into the specified place in a destination * tree. The source node and its children are appended as children * of the destination node./*from w ww .j a v a 2s . com*/ * <p> * <em>Note:</em> This is an iterative implementation. */ public static void copyInto(Node src, Node dest) throws DOMException { // get node factory Document factory = dest.getOwnerDocument(); boolean domimpl = factory instanceof DocumentImpl; // placement variables Node start = src; Node parent = src; Node place = src; // traverse source tree while (place != null) { // copy this node Node node = null; int type = place.getNodeType(); switch (type) { case Node.CDATA_SECTION_NODE: { node = factory.createCDATASection(place.getNodeValue()); break; } case Node.COMMENT_NODE: { node = factory.createComment(place.getNodeValue()); break; } case Node.ELEMENT_NODE: { Element element = factory.createElement(place.getNodeName()); node = element; NamedNodeMap attrs = place.getAttributes(); int attrCount = attrs.getLength(); for (int i = 0; i < attrCount; i++) { Attr attr = (Attr) attrs.item(i); String attrName = attr.getNodeName(); String attrValue = attr.getNodeValue(); element.setAttribute(attrName, attrValue); if (domimpl && !attr.getSpecified()) { ((AttrImpl) element.getAttributeNode(attrName)).setSpecified(false); } } break; } case Node.ENTITY_REFERENCE_NODE: { node = factory.createEntityReference(place.getNodeName()); break; } case Node.PROCESSING_INSTRUCTION_NODE: { node = factory.createProcessingInstruction(place.getNodeName(), place.getNodeValue()); break; } case Node.TEXT_NODE: { node = factory.createTextNode(place.getNodeValue()); break; } default: { throw new IllegalArgumentException( "can't copy node type, " + type + " (" + node.getNodeName() + ')'); } } dest.appendChild(node); // iterate over children if (place.hasChildNodes()) { parent = place; place = place.getFirstChild(); dest = node; } // advance else { place = place.getNextSibling(); while (place == null && parent != start) { place = parent.getNextSibling(); parent = parent.getParentNode(); dest = dest.getParentNode(); } } } }
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;/* w w w . ja v a2s . com*/ 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
public static void setElementTextValue(Element e, String text, boolean cdata) { Document root = e.getOwnerDocument(); e.normalize();/* w w w.j a v a 2 s.c om*/ if (e.hasChildNodes()) { NodeList nl = e.getChildNodes(); /* This suxx: NodeList Object is changed when removing children !!! I will store all nodes that should be deleted in a Vector and delete them afterwards */ int length = nl.getLength(); List<Node> v = new ArrayList<Node>(nl.getLength()); for (int i = 0; i < length; i++) if (nl.item(i) instanceof CharacterData) v.add(nl.item(i)); for (Node n : v) e.removeChild(n); } if (cdata) { e.appendChild(root.createCDATASection(text)); } else { e.appendChild(root.createTextNode(text)); } }