txtNode.setNodeValue(newValue);
private Element createTextNode(Document doc, String name, String value){ Text node = doc.createTextNode(name); node.setNodeValue(String.valueOf(value)); Element child = doc.createElement(name); child.appendChild(node); return child; }
/** * Extract nested text from an element. * Currently does not handle coalescing text nodes, CDATA sections, etc. * @param parent a parent element * @return the nested text, or null if none was found */ private static void replaceText(Element parent, String name) { NodeList l = parent.getChildNodes(); for (int i = 0; i < l.getLength(); i++) { if (l.item(i).getNodeType() == Node.TEXT_NODE) { Text text = (Text)l.item(i); text.setNodeValue(name); return; } } }
/** * Extract nested text from an element. * Currently does not handle coalescing text nodes, CDATA sections, etc. * @param parent a parent element * @return the nested text, or null if none was found */ private static void replaceText(Element parent, String name) { NodeList l = parent.getChildNodes(); for (int i = 0; i < l.getLength(); i++) { if (l.item(i).getNodeType() == Node.TEXT_NODE) { Text text = (Text)l.item(i); text.setNodeValue(name); return; } } }
... for (int i =0; i<list.getLength();i++) { Node node = list.item(i); //get the salary element, and update the value if("from".equals(node.getNodeName())){ Text text = (Text) ((Element) node).getChildNodes().item(0); text.setNodeValue("prasad"); } } TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(new File("/mnt/sdcard/one.xml")); transformer.transform(source, result); ...
private static void replaceText(Element parent, String name, String regex) { NodeList l = parent.getChildNodes(); for (int i = 0; i < l.getLength(); i++) { if (l.item(i).getNodeType() == Node.TEXT_NODE) { Text text = (Text) l.item(i); if (regex != null) { String s = text.getNodeValue(); text.setNodeValue(s.replaceAll(regex, name)); } else { text.setNodeValue(name); } return; } } }
public static void setTextValue(Node node, String value, boolean cdata) { if(node!=null) { if(node.getNodeType()==Node.ELEMENT_NODE) { for(Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) { if (child.getNodeType() == Node.TEXT_NODE && !cdata) { ((Text)child).setNodeValue(value); return; } if (child.getNodeType() == Node.CDATA_SECTION_NODE && cdata) { ((Text)child).setNodeValue(value); return; } } if(node.hasChildNodes()) { removeChildren(node); } Node textNode = cdata ? node.getOwnerDocument().createCDATASection(value) : node.getOwnerDocument().createTextNode(value); node.appendChild(textNode); } else { node.setNodeValue(value); } } }
@Test public void testXSLTTransforms () throws Exception { DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); Document doc = docBuilder.newDocument(); Element el = doc.createElement("Container"); doc.appendChild(el); Text e = doc.createTextNode("Character"); el.appendChild(e); //e.setNodeValue("\'"); //e.setNodeValue("\""); e.setNodeValue("&"); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(System.out); //This prints the original document to the command line. transformer.transform(source, result); InputStream xsltStream = getClass().getResourceAsStream("/characterswap.xslt"); Source xslt = new StreamSource(xsltStream); transformer = transformerFactory.newTransformer(xslt); //This one is the one you'd pipe to a file transformer.transform(source, result); }
for(Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) { if (child.getNodeType() == Node.TEXT_NODE) { ((Text)child).setNodeValue(value); return; ((Text)child).setNodeValue(value); return;
/** * Update the security-constraint element having url-pattern equal with * oldPattern by replacing this old pattern with the newUrlPattern * @param oldUrlPatter url-pattern to be replaced * @param newUrlPatterValue url-pattern to replace with */ public void updateSecurityConstraint(String oldUrlPatter, String newUrlPatterValue) { NodeList nl = getElement().getElementsByTagNameNS(J2EE_NS, SECURITY_CONSTRAINT); // loop over security constraints for (int i = 0; i < nl.getLength(); i++) { Element e = (Element) nl.item(i); // look at nested url-pattern Element(s) NodeList urlPatternCollection = e.getElementsByTagNameNS(J2EE_NS, URL_PATTERN); for (int j = 0; j < urlPatternCollection.getLength(); j++) { Element urlPatternElement = (Element) urlPatternCollection.item(j); Text urlPatternText = (Text) urlPatternElement.getFirstChild(); // if found urlPattern Element, replace its value if (urlPatternText.getNodeValue().equals(oldUrlPatter)) { urlPatternText.setNodeValue(newUrlPatterValue); } } } }
/** * Sets the template container location in the settings file */ protected void setContainerValue(IContainer container, Element element, String localName) { Element childElement = getChildWithLocalName(element, localName); Text text = null; for (Node node = childElement.getFirstChild(); node != null; node = node.getNextSibling()) { if (node.getNodeType() == Node.TEXT_NODE) { text = (Text)node; break; } } if (text == null) { text = element.getOwnerDocument().createTextNode(container.getProjectRelativePath().toString()); childElement.appendChild(text); } else { text.setNodeValue(container.getProjectRelativePath().toString()); } }
/** * Sets the template container locations in the settings file */ protected void setContainerValues(List<Object> containers, List<Object> sourceContainers, Element element, String localName) { Element childElement = getChildWithLocalName(element, localName); Text text = null; for (Node node = childElement.getFirstChild(); node != null; node = node.getNextSibling()) { if (node.getNodeType() == Node.TEXT_NODE) { text = (Text)node; break; } } if (text == null) { text = element.getOwnerDocument().createTextNode(getContainers(jetProject, containers, sourceContainers)); childElement.appendChild(text); } else { text.setNodeValue(getContainers(jetProject, containers, sourceContainers)); } }
/** * Sets the template container location in the settings file */ protected void setContainerValue(IContainer container, Element element, String localName) { Element childElement = getChildWithLocalName(element, localName); Text text = null; for (Node node = childElement.getFirstChild(); node != null; node = node.getNextSibling()) { if (node.getNodeType() == Node.TEXT_NODE) { text = (Text)node; break; } } if (text == null) { text = element.getOwnerDocument().createTextNode(container.getProjectRelativePath().toString()); childElement.appendChild(text); } else { text.setNodeValue(container.getProjectRelativePath().toString()); } }
/** * Sets the template container locations in the settings file */ protected void setContainerValues(List<Object> containers, List<Object> sourceContainers, Element element, String localName) { Element childElement = getChildWithLocalName(element, localName); Text text = null; for (Node node = childElement.getFirstChild(); node != null; node = node.getNextSibling()) { if (node.getNodeType() == Node.TEXT_NODE) { text = (Text)node; break; } } if (text == null) { text = element.getOwnerDocument().createTextNode(getContainers(jetProject, containers, sourceContainers)); childElement.appendChild(text); } else { text.setNodeValue(getContainers(jetProject, containers, sourceContainers)); } }
/** * Performs simple EL resolution for the child Text Node of the specified * source Element instance. * * @param srcElement Source Element for which child Text Node EL resolution * is to be performed. */ protected void resolveChildText(Element srcElement) { if (srcElement != null) { NodeList childNodes = srcElement.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node childNode = childNodes.item(i); if (childNode.getNodeType() == Node.TEXT_NODE) { Text textNode = (Text)childNode; String textNodeValue = textNode.getNodeValue(); try { String newTextNodeValue = (String)PageExpressionContext.getCurrent().evaluateExpression(textNodeValue, String.class, null); if (!textNodeValue.equals(newTextNodeValue)) { textNode.setNodeValue(newTextNodeValue); } } catch(Exception ex) { //ignore - could not resolve, do not change existing value } } } } }
retval = titleAttr.appendChild(textNode); clonedTitle = (Attr) titleAttr.cloneNode(false); textNode.setNodeValue("text_node_not_cloned"); value = clonedTitle.getValue(); assertEquals("attrValue", "Yesterday", value);
/** * If this is a Text node then this method will set its value, otherwise it sets the value of the immediate (Text) child of this node. * <p/> * The value of the immediate child of this node can be set only if, there is one child node and * that node is a Text node, or if there are no children in which case a child Text node will be created. * * @param value A value string * @throws IllegalStateException if the node is not a Text node and either has more than one child node or has a child node that is not a Text node. */ public void setValue(String value) { // The Text node should overwrite setValue if (this instanceof javax.xml.soap.Text) throw new WSException(BundleUtils.getMessage(bundle, "IS_NOT_TEXT")); org.w3c.dom.Node child = (org.w3c.dom.Node)getFirstChild(); if (child instanceof org.w3c.dom.Text) ((org.w3c.dom.Text)child).setNodeValue(value); if (child == null) { child = domNode.getOwnerDocument().createTextNode(value); appendChild(new TextImpl(child)); } }
txtNode.setNodeValue(PropertyConverter.escapeDelimiters( value.toString(), getListDelimiter()));
txtNode.setNodeValue(newValue);