Java Code Examples for org.w3c.dom.Node.setNodeValue()

The following are Jave code examples for showing how to use setNodeValue() of the org.w3c.dom.Node class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: TuLiPA-frames   File: ViewTreeBuilder.java   Source Code and License Vote up 7 votes
public static ArrayList<Node> extractFeatures(Node n, Document D) {
    ArrayList<Node> attrs = new ArrayList<Node>();
    String type = n.getAttributes().getNamedItem("type").getNodeValue();
    NodeList features = n.getChildNodes();
    for (int i = 0; i < n.getChildNodes().getLength(); i++) {
        if (n.getChildNodes().item(i) instanceof Element) {
            features = n.getChildNodes().item(i).getChildNodes();
        }
    }
    for (int i = 0; i < features.getLength(); i++) {
        Node feature = features.item(i);
        if (feature instanceof Element) {
            String name = feature.getAttributes().getNamedItem("name")
                    .getNodeValue();
            String value = extractFeatureValue(feature);
            Node attr = D.createAttribute(type + ":" + name);
            attr.setNodeValue(value);
            attrs.add(attr);
        }
    }
    return attrs;
}
 
Example 2
Project: incubator-netbeans   File: Schema2BeansUtil.java   Source Code and License Vote up 7 votes
/**
 * Merge child nodes
 * 
 * @param node     current node base
 * @param childNode child node if exist
 * @param foundBean child bean
 * @param currentChild current child node
 * @param patternChild current pattern child
 * @param nodeMap node map
 * @param document document
 * @param patternBean pattern bean
 * @param children list relevant childs current node base
 */
private static void mergeChildNodes(Node node, Node childNode, BaseBean foundBean,
        Node currentChild, Node patternChild, Map nodeMap, Document document,
        BaseBean patternBean, List children) {
    Node foundChild = childNode;
    if (foundChild == null) {
        foundChild = takeEqualNode(children, patternChild);
    }
    if (foundChild != null) {
        if (foundChild != currentChild) {
            node.removeChild(foundChild);
            node.insertBefore(foundChild, currentChild);
        }
        if (foundBean != null) {
            mergeBeans(nodeMap, foundBean, patternBean);
        } else if (isRelevantNode(foundChild) && foundChild.hasChildNodes()) {
            mergeNode(nodeMap, foundChild, patternChild);
        } else {
            foundChild.setNodeValue(patternChild.getNodeValue());
        }
    } else {
        Node child = document.importNode(patternChild, true);
        node.insertBefore(child, currentChild);
    }
}
 
Example 3
Project: tomcat7   File: DomUtil.java   Source Code and License Vote up 7 votes
/** Set or replace the text value
 */
public static void setText(Node node, String val) {
    Node chld=DomUtil.getChild(node, Node.TEXT_NODE);
    if( chld == null ) {
        Node textN=node.getOwnerDocument().createTextNode(val);
        node.appendChild(textN);
        return;
    }
    // change the value
    chld.setNodeValue(val);
}
 
Example 4
Project: AndroidApktool   File: ResXmlPatcher.java   Source Code and License Vote up 6 votes
/**
 * Replaces package value with passed packageOriginal string
 *
 * @param file File for AndroidManifest.xml
 * @param packageOriginal Package name to replace
 * @throws AndrolibException
 */
public static void renameManifestPackage(File file, String packageOriginal) throws AndrolibException {
    try {
        Document doc = loadDocument(file);

        // Get the manifest line
        Node manifest = doc.getFirstChild();

        // update package attribute
        NamedNodeMap attr = manifest.getAttributes();
        Node nodeAttr = attr.getNamedItem("package");
        nodeAttr.setNodeValue(packageOriginal);
        saveDocument(file, doc);

    } catch (SAXException | ParserConfigurationException | IOException | TransformerException ignored) {
    }
}
 
Example 5
Project: convertigo-engine   File: Migration5_0_0.java   Source Code and License Vote up 6 votes
private static void makeCdataReplacements(Element element) throws Exception {
	try {
        Node cdata = XMLUtils.findChildNode(element, Node.CDATA_SECTION_NODE);
        if (cdata != null) {
            String s = cdata.getNodeValue();
            if (!s.equals("")) {
                StringEx sx = new StringEx(s);
                Enumeration<String> keys = nameReplacements.keys();
                String key, value;
                while (keys.hasMoreElements()) {
                	key = keys.nextElement();
                	value = nameReplacements.get(key);
                	sx.replaceAll(key, value);
                }
                s = sx.toString();
                cdata.setNodeValue(s);
            }
        }
	}
	catch (Exception e) {
		throw new Exception("Unable to make replacements",e);
	}
}
 
Example 6
Project: AndroidApktool   File: ResXmlPatcher.java   Source Code and License Vote up 5 votes
/**
 * Any @string reference in a <provider> value in AndroidManifest.xml will break on
 * build, thus preventing the application from installing. This is from a bug/error
 * in AOSP where public resources cannot be part of an authorities attribute within
 * a <provider> tag.
 *
 * This finds any reference and replaces it with the literal value found in the
 * res/values/strings.xml file.
 *
 * @param file File for AndroidManifest.xml
 * @throws AndrolibException
 */
public static void fixingPublicAttrsInProviderAttributes(File file) throws AndrolibException {
    if (file.exists()) {
        try {
            Document doc = loadDocument(file);
            XPath xPath = XPathFactory.newInstance().newXPath();
            XPathExpression expression = xPath.compile("/manifest/application/provider");

            Object result = expression.evaluate(doc, XPathConstants.NODESET);
            NodeList nodes = (NodeList) result;

            for (int i = 0; i < nodes.getLength(); i++) {
                Node node = nodes.item(i);
                NamedNodeMap attrs = node.getAttributes();

                if (attrs != null) {
                    Node provider = attrs.getNamedItem("android:authorities");

                    if (provider != null) {
                        String reference = provider.getNodeValue();
                        String replacement = pullValueFromStrings(file.getParentFile(), reference);

                        if (replacement != null) {
                            provider.setNodeValue(replacement);
                            saveDocument(file, doc);
                        }
                    }
                }
            }

        }  catch (SAXException | ParserConfigurationException | IOException |
                XPathExpressionException | TransformerException ignored) {
        }
    }
}
 
Example 7
Project: oscm   File: MigrationBillingResultDecimalPlace.java   Source Code and License Vote up 5 votes
private void updateAttributeValue(Node node, String attName,
        int decimalPlacesSetting) {
    if (node.hasAttributes()
            && !"NumberOfOccurrence".equals(node.getNodeName())
            && !"ParameterValue".equals(node.getNodeName())) {
        Node attribute = node.getAttributes().getNamedItem(attName);
        if (attribute != null) {
            String priceValue = attribute.getNodeValue();
            if (priceValue != null && priceValue.length() > 0) {
                attribute.setNodeValue(getConvertedPrice(priceValue,
                        decimalPlacesSetting));
            }
        }
    }
}
 
Example 8
Project: karate   File: XmlUtils.java   Source Code and License Vote up 5 votes
public static void setByPath(Node doc, String path, String value) {
    Node node = getNodeByPath(doc, path, true);
    if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
        node.setNodeValue(value);
    } else if (node.hasChildNodes() && node.getFirstChild().getNodeType() == Node.TEXT_NODE) {
        node.getFirstChild().setTextContent(value);
    } else if (node.getNodeType() == Node.ELEMENT_NODE) {
        node.setTextContent(value);
    }
}
 
Example 9
Project: Equella   File: DOMHelper.java   Source Code and License Vote up 5 votes
/**
 * Retrieves the text value from a node's child text node.
 */
static void setValueForNode(Node node, String value)
{
	if( node != null )
	{
		switch( node.getNodeType() )
		{
			case Node.ELEMENT_NODE: {
				Node child = node.getFirstChild();
				if( child == null )
				{
					Document doc = node.getOwnerDocument();
					node.appendChild(doc.createTextNode(value));
				}
				else
				{
					child.setNodeValue(value);
				}
				break;
			}

			case Node.ATTRIBUTE_NODE: {

				Attr attribute = (Attr) node;
				attribute.getOwnerElement().setAttribute(attribute.getName(), value);
				break;
			}

			default:
				break;
		}
	}
}
 
Example 10
Project: lams   File: DomUtil.java   Source Code and License Vote up 5 votes
/** Set or replace the text value 
 */ 
public static void setText(Node node, String val) {
    Node chld=DomUtil.getChild(node, Node.TEXT_NODE);
    if( chld == null ) {
        Node textN=node.getOwnerDocument().createTextNode(val);
        node.appendChild(textN);
        return;
    }
    // change the value
    chld.setNodeValue(val);           
}
 
Example 11
Project: OpenJSharp   File: ElementImpl.java   Source Code and License Vote up 5 votes
public void setValue(String value) {
    Node valueNode = getValueNodeStrict();
    if (valueNode != null) {
        valueNode.setNodeValue(value);
    } else {
        try {
            addTextNode(value);
        } catch (SOAPException e) {
            throw new RuntimeException(e.getMessage());
        }
    }
}
 
Example 12
Project: lazycat   File: DomUtil.java   Source Code and License Vote up 5 votes
/**
 * Set or replace the text value
 */
public static void setText(Node node, String val) {
	Node chld = DomUtil.getChild(node, Node.TEXT_NODE);
	if (chld == null) {
		Node textN = node.getOwnerDocument().createTextNode(val);
		node.appendChild(textN);
		return;
	}
	// change the value
	chld.setNodeValue(val);
}
 
Example 13
Project: openjdk-jdk10   File: ElementImpl.java   Source Code and License Vote up 5 votes
@Override
public void setValue(String value) {
    Node valueNode = getValueNodeStrict();
    if (valueNode != null) {
        valueNode.setNodeValue(value);
    } else {
        try {
            addTextNode(value);
        } catch (SOAPException e) {
            throw new RuntimeException(e.getMessage());
        }
    }
}
 
Example 14
Project: openjdk-jdk10   File: DOML3InputSourceFactoryImpl.java   Source Code and License Vote up 5 votes
@Override
public short acceptNode(Node n) {
    String localname = n.getLocalName();
    if (localname.equals("_test-04")) {
        Node child = n.getFirstChild();
        String text = child.getNodeValue();
        if (text.equals("T%e!s#t$")) {
            child.setNodeValue("T%E!S#T$");
        }
    }
    return FILTER_ACCEPT;
}
 
Example 15
Project: tomcat7   File: DomUtil.java   Source Code and License Vote up 4 votes
public static void setAttribute(Node node, String attName, String val) {
    NamedNodeMap attributes=node.getAttributes();
    Node attNode=node.getOwnerDocument().createAttribute(attName);
    attNode.setNodeValue( val );
    attributes.setNamedItem(attNode);
}
 
Example 16
Project: convertigo-engine   File: XMLSplitNodes.java   Source Code and License Vote up 4 votes
@Override
protected void appendToOutputDom(NodeList nodeList, Document outputDom) {
	Element doc = outputDom.getDocumentElement();
	
	try {
		NodeList liste = xpathApi.selectNodeList(doc, resultXpath);
		 
		for (int i = 0 ; i < liste.getLength() ; i++) {
			if (!isRequestedObjectRunning()) break;
			
		 	Node treatedNode = liste.item(i); 
		 	// look for the text node to modify under treated node
		 	NodeList itemNodes = treatedNode.getChildNodes();
		 	Node tmpNode = null;
			for (int j = 0 ; j < itemNodes.getLength() ; j++) {
				tmpNode = itemNodes.item(j);
				if (tmpNode.getNodeType() == Node.TEXT_NODE)
					break;
			}
			if (tmpNode == null)
				continue;
			
			// we found a text node under treated node
			String texte = tmpNode.getNodeValue();
			Pattern myPattern = Pattern.compile(regExp);
			Matcher myMatcher = myPattern.matcher(texte);
			List<String> splitString = new LinkedList<String>();
			int beginIndex = 0, startIndex, endIndex, j=0;
			while (myMatcher.find()) {
				startIndex = myMatcher.start();
				endIndex = myMatcher.end();
				splitString.add(texte.substring(beginIndex, startIndex));
				if (keepSeparator)
					splitString.add(texte.substring(startIndex, endIndex));
				beginIndex = endIndex;
			}
			if (beginIndex != texte.length())
				splitString.add(texte.substring(beginIndex, texte.length()));
			
			// for all split string : create a node and add it
			for(String tmp : splitString){
				Element elem = outputDom.createElement("item_" + (j++)); 
				Node text = outputDom.createTextNode(tmp);
				elem.appendChild(text);
				treatedNode.appendChild(elem);
			}
			// remove text node value now it has been split	
			tmpNode.setNodeValue(""); 
		} 
	} catch(TransformerException e) {
		Engine.logBeans.debug("Xpath error : no node list found for " + resultXpath);
	}
}
 
Example 17
Project: lams   File: DomUtil.java   Source Code and License Vote up 4 votes
public static void setAttribute(Node node, String attName, String val) {
    NamedNodeMap attributes=node.getAttributes();
    Node attNode=node.getOwnerDocument().createAttribute(attName);
    attNode.setNodeValue( val );
    attributes.setNamedItem(attNode);
}
 
Example 18
Project: apache-tomcat-7.0.73-with-comment   File: DomUtil.java   Source Code and License Vote up 4 votes
public static void setAttribute(Node node, String attName, String val) {
    NamedNodeMap attributes=node.getAttributes();
    Node attNode=node.getOwnerDocument().createAttribute(attName);
    attNode.setNodeValue( val );
    attributes.setNamedItem(attNode);
}
 
Example 19
Project: doctemplate   File: XmlMergeEngine.java   Source Code and License Vote up 4 votes
protected void preProcess(Document doc, Node src, Node dest) throws DocTemplateException {

		NodeList childElements = src.getChildNodes();
		for (int i = 0; i < childElements.getLength(); i++) {
			Node childElement = childElements.item(i);
			Node result = null;
			String nodeName = childElement.getNodeName();
			boolean field = nodeName.toUpperCase().endsWith(XML_FIELD);
			if (NAMESPACE_URI.equals(childElement.getNamespaceURI())) {
				String key = null;
				// In Fall des Fields key gleich mit dem "text:name" Eigenschaft
				if (field) {
					key = getValueOfAttribute(XML_FIELD_PATH, childElement);
					String formatter = getValueOfAttribute(XML_FIELD_FORMATTER, childElement);
					if (!StringUtils.isEmpty(formatter)) {
						String postFix = LdtConstants.FORMAT_SUFFIX + formatter;
						key = key + postFix;
						keyTranslationTable.put(postFix, postFix);
					}
				} else {
					for (String blockElement : BLOCK_MARKERS) {
						if (nodeName.toUpperCase().endsWith(blockElement)) {
							key = blockElement + "_" + childElement.getAttributes().item(0).getTextContent();
							break;
						}
					}
				}
				if (key != null) {
					result = doc.createElement(INTERNAL_BOOKMARK_TAG);
					result.setTextContent(field ? (getFieldPrefix() + key) : key);
					String sort = null;
					if ((sort = getValueOfAttribute(SORT, childElement)) != null) {
						if (!sort.equalsIgnoreCase(ASC) && !sort.equalsIgnoreCase(DESC)) {
							log.warn("Die Sortierung ist falsch: asc oder desc!");
						} else {
							dest.appendChild(result);
							result = doc.createElement(INTERNAL_BOOKMARK_TAG);
							String body = SORT.toUpperCase().concat("_").concat(getPfadOnly(key));
							if (sort.equalsIgnoreCase(DESC)) {
								body = body.concat("_").concat(sort.toUpperCase());
							}
							result.setTextContent(body);
						}
					}
					Node attr = childElement.getAttributes().getNamedItem("attribute");
					if (attr != null) {
						String s = attr.getNodeValue();
						Node srcAttr = src.getAttributes().getNamedItem(s);
						if (srcAttr != null) {
							srcAttr.setNodeValue(INTERNAL_BOOKMARK_XML_ATTR_START + result.getTextContent() + INTERNAL_BOOKMARK_XML_ATTR_END);
							dest.getAttributes().setNamedItem(doc.adoptNode(srcAttr.cloneNode(false)));
							continue;
						}
					}
					if (!field) {
						dest.appendChild(result);
						preProcess(doc, childElement, dest);
						result = doc.createElement(INTERNAL_BOOKMARK_TAG);
						result.setTextContent("END".concat(key));
						dest.appendChild(result);
						continue;
					}
				}
			}
			if (result == null) {
				result = doc.adoptNode(childElement.cloneNode(false));
			}
			dest.appendChild(result);
			preProcess(doc, childElement, result);
		}
	}
 
Example 20
Project: lazycat   File: DomUtil.java   Source Code and License Vote up 4 votes
public static void setAttribute(Node node, String attName, String val) {
	NamedNodeMap attributes = node.getAttributes();
	Node attNode = node.getOwnerDocument().createAttribute(attName);
	attNode.setNodeValue(val);
	attributes.setNamedItem(attNode);
}