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

The following are Jave code examples for showing how to use setTextContent() 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: call-IDE   File: BuildSys.java   Source Code and License Vote up 6 votes
/**
 * Description: Editing the xml file for compiling projects.
 * @param init path to the xml file
 * @param build path to the build directory
 * @param src  path to the source directory
 */
public static void setPropsForCompile(String init ,String build, String src) {
    try {
        String filepath = init;
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(filepath);
        
        // get project 
        //Node project = doc.getFirstChild();
        Node prop1 = doc.getElementsByTagName("property").item(0);
        Node prop2 = doc.getElementsByTagName("property").item(1);

        NamedNodeMap srcAttr = prop1.getAttributes();
        Node srcName = srcAttr.getNamedItem("location");
        srcName.setTextContent(src);
        
        NamedNodeMap buildAttr = prop2.getAttributes();
        Node buildName = buildAttr.getNamedItem("location");
        buildName.setTextContent(build);
        
        
        updateBuildFile( filepath, doc);
        
    } catch (Exception exc) {}
}
 
Example 2
Project: adobe-air-db-connector   File: XmlEncoder.java   Source Code and License Vote up 6 votes
public String encode(ResultSet rs) throws Exception {
	ResultSetMetaData rsmd;
	rsmd = rs.getMetaData();
	
	DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
	
	DocumentBuilder db = dbf.newDocumentBuilder();
	
	Document doc = db.newDocument();

	Element ele = doc.createElement("result");
	Integer count = 0;
	while(rs.next()){
		count++;
		Node rowNode = doc.createElement("row"); 
		for (int i=1; i<= rsmd.getColumnCount(); i++){
			 Node colNode = doc.createElement(rsmd.getColumnName(i)) ;
			 colNode.setTextContent(rs.getObject(colNode.getNodeName()).toString());
			 rowNode.appendChild(colNode);
		}
		ele.appendChild(rowNode);
	}
	ele.setAttribute("size", count.toString());
	doc.appendChild(ele);
	return toXmlString(doc);
}
 
Example 3
Project: karate   File: XmlUtils.java   Source Code and License Vote up 6 votes
public static void trimWhiteSpace(Node node) {
    NodeList children = node.getChildNodes();
    int count = children.getLength();
    for (int i = 0; i < count; ++i) {
        Node child = children.item(i);
        if (child.getNodeType() == Node.TEXT_NODE) {
            child.setTextContent(child.getTextContent().trim());
        }
        trimWhiteSpace(child);
    }
}
 
Example 4
Project: lams   File: RemoveToolContextClasspathTask.java   Source Code and License Vote up 6 votes
/**
    * Remove the tool's applicationContext entry from the param-value node of the context-param entry.
    * Should find and remove all the matching entries (should it somehow have have got in there m
    * 
    * @param doc
    * @param children
    * @param childNode
    */
   @Override
   protected void updateParamValue(Document doc, Node contextParamElement) {

NodeList valueChildren = contextParamElement.getChildNodes();
for (int i = 0; i < valueChildren.getLength(); i++) {
    Node valueChild = valueChildren.item(i);
    if (valueChild instanceof Text) {
	String value = valueChild.getNodeValue();
	String newValue = StringUtils.replace(value, getApplicationContextPathWithClasspath(), "");
	if (newValue.length() < value.length()) {
	    valueChild.setTextContent(newValue);
	    System.out.println(
		    "Removed context entry " + getApplicationContextPathWithClasspath() + " from document.");
	}
    }
}
   }
 
Example 5
Project: AgentWorkbench   File: BundleBuilder.java   Source Code and License Vote up 5 votes
/**
 * Sets the service description name.
 *
 * @param serviceDescriptionFile the service description file
 * @param serviceName the service name
 */
private void setServiceDescriptionName(File serviceDescriptionFile) {
	
	String newServiceName = this.getSymbolicBundleName() + "." + CLASS_LOAD_SERVICE_NAME;
	
	try {
		// --- Open the XML document ------------------
		DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
		Document doc = docBuilder.parse(serviceDescriptionFile);
		
		// --- Get the XML root/component element -----
		Node component = doc.getFirstChild();
		NamedNodeMap componentAttr = component.getAttributes();
		Node nameAttr = componentAttr.getNamedItem("name");
		nameAttr.setTextContent(newServiceName);
		
		// --- Save document in XML file --------------	
		TransformerFactory transformerFactory = TransformerFactory.newInstance();
		Transformer transformer = transformerFactory.newTransformer();
		DOMSource source = new DOMSource(doc);
		StreamResult result = new StreamResult(serviceDescriptionFile);
		transformer.transform(source, result);
		
	} catch (ParserConfigurationException pcEx) {
		pcEx.printStackTrace();
	} catch (SAXException saxEx) {
		saxEx.printStackTrace();
	} catch (IOException ioEx) {
		ioEx.printStackTrace();
	} catch (TransformerConfigurationException tcEx) {
		tcEx.printStackTrace();
	} catch (TransformerException tEx) {
		tEx.printStackTrace();
	}
	
}
 
Example 6
Project: call-IDE   File: BuildSys.java   Source Code and License Vote up 5 votes
/**
 * Description: Editing the xml file for compiling files.
 * @param init path to the xml file
 * @param sourceFile path to the source to be compiled
 */
public static void setPropsForCompileFile(String init, String sourceFile) {
    try {
        File file = new File(sourceFile);
        
        String filepath = init;
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(filepath);
        
        // get project 
        //Node project = doc.getFirstChild();
        Node prop1 = doc.getElementsByTagName("property").item(0);
        Node prop2 = doc.getElementsByTagName("property").item(1);
        Node prop3 = doc.getElementsByTagName("property").item(2);
        
        NamedNodeMap srcAttr = prop1.getAttributes();
        Node srcName = srcAttr.getNamedItem("location");
        srcName.setTextContent( file.getParent());
        
        NamedNodeMap buildAttr = prop2.getAttributes();
        Node buildName = buildAttr.getNamedItem("location");
        buildName.setTextContent( file.getParent());
        
        NamedNodeMap fileAttr = prop3.getAttributes();
        Node fileName = fileAttr.getNamedItem("value");
        fileName.setTextContent( file.getName());
        
        updateBuildFile( filepath, doc);
        
    } catch (Exception ex) {}
}
 
Example 7
Project: oscm   File: SamlAssertionBreakHandler.java   Source Code and License Vote up 5 votes
private void removeLastCharacter(Node node) {
    String value = node.getTextContent();
    if (value.length() > 0) {
        String textContent = value.substring(0, (value.length() - 1));
        node.setTextContent(textContent);
    }
}
 
Example 8
Project: oscm   File: ExceptionAdapter.java   Source Code and License Vote up 5 votes
private void convertException(SOAPMessageContext context,
        String newExceptionName) throws SOAPException {
    String saaSApplicationExceptionName = getSaaSApplicationExceptionName();
    NodeList nodeList = context.getMessage().getSOAPBody()
            .getElementsByTagName(PREFIX + newExceptionName);
    if (null == nodeList) {
        return;
    }
    Node node = nodeList.item(0);
    Element newNode = node.getOwnerDocument().createElementNS(
            node.getNamespaceURI(), PREFIX + saaSApplicationExceptionName);

    NodeList list = node.getChildNodes();
    if (null != list) {
        int childrenSize = list.getLength();
        for (int i = 0; i < childrenSize; i++) {
            Node child = list.item(0);
            if (child.getNodeName().equals(PREFIX + MESSAGEKEYTAG)) {
                String content = child.getTextContent().replaceAll(
                        newExceptionName, saaSApplicationExceptionName);
                child.setTextContent(content);
            }
            newNode.appendChild(child);
        }
        node.getParentNode().replaceChild(newNode, node);
    }
}
 
Example 9
Project: karate   File: KarateHtmlReporter.java   Source Code and License Vote up 5 votes
private void appendLog(Node parent, String log) {
    if (!log.isEmpty()) {
        Node pre = node("div", "preformatted");
        pre.setTextContent(log);
        parent.appendChild(pre);
    }
}
 
Example 10
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 11
Project: Equella   File: XmlDocument.java   Source Code and License Vote up 5 votes
public synchronized void setNodeValue(String xpath, Node nodeContext, String value)
{
	Node node = singleNodeFromList(nodeList(xpath, nodeContext));
	if( node != null )
	{
		node.setTextContent(value);
	}
}
 
Example 12
Project: doctemplate   File: ODTMergeEngine.java   Source Code and License Vote up 5 votes
@Override
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;
		boolean field = false;
		// OO Field Eigenschaft kann mit "variable-get" oder "variable-set" Knotename behandlen werden.
		if (childElement.getNodeName().endsWith(ODT_BOOKMARK_TAG_SUFFIX)
				|| (field = childElement.getNodeName().endsWith(OO_FIELD_GET_TAG_POSTFIX) || childElement.getNodeName().endsWith(OO_FIELD_SET_TAG_POSTFIX))) {
			String key = "";
			// In Fall des Fields key gleich mit dem "text:name" Eigenschaft
			if (field) {
				key = childElement.getAttributes().getNamedItem(TEXT_NAME).getTextContent();
			} else {
				key = childElement.getAttributes().item(0).getTextContent();
			}
			// mehrere gleiche Textmarken mit ALT-Suffix intern ohne ALT-Suffix anwenden
			int altPos = key.indexOf(ALTERNATE_SUFFIX);
			if (altPos > 0) {
				key = key.substring(0, altPos);
			}
			if (key != null && (key.startsWith(getFieldPrefix()) || key.startsWith(SORTFIELD_PREFIX) || key.startsWith(CONDITION_BEGIN) || key.startsWith(CONDITION_END)
					|| key.startsWith(ITERATION_BEGIN) || key.startsWith(ITERATION_END)) || field) {
				result = doc.createElement(INTERNAL_BOOKMARK_TAG);
				// In Fall des Fields wird ein Feld mit Prefix "FIELD_" generiert. Das bedautet, wir behandlen den
				// OO Field ebenso, als "FIELD_" Bookmark
				result.setTextContent(field ? (getFieldPrefix() + key) : key);
			}
		}
		if (result == null) {
			result = doc.adoptNode(childElement.cloneNode(false));
		}
		dest.appendChild(result);
		preProcess(doc, childElement, result);
	}
}
 
Example 13
Project: DeskChan   File: CharacterRange.java   Source Code and License Vote up 4 votes
public Node toXMLNode(Document doc, String name) {
	Node node = doc.createElement(name);
	node.setTextContent(start + " | " + end);
	return node;
}
 
Example 14
Project: DeskChan   File: Phrase.java   Source Code and License Vote up 4 votes
private void appendTo(Document doc, Node target, String name, String text) {
	Node n = doc.createElement(name);
	n.setTextContent(text);
	target.appendChild(n);
}
 
Example 15
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 16
Project: ccpa   File: TreeTransformer.java   Source Code and License Vote up 4 votes
private static void changeFunctionVariablesToGlobal(String name, Node node, Node globalData, HashSet<String> localVariables)
{
	if (node.getNodeName().equals("uses"))
		return;

	if (node.getNodeName().equals("value"))
	{
		if (localVariables.contains(node.getTextContent()))
		{
			node.setTextContent(name + ":" + node.getTextContent());
		}
	}

	if (node.getNodeName().equals("op"))
	{
		NamedNodeMap attr = node.getAttributes();
		NodeList children = node.getChildNodes();
		String operation = attr.getNamedItem("name").getTextContent();

		if (!operation.equals("STRING") && !operation.equals("FLOAT") && !operation.equals("INT"))
		{
			if (operation.equals("GET_MEMBER") || operation.equals("GET_ACCESS"))
			{
				changeFunctionVariablesToGlobal(name, children.item(1),globalData,localVariables);
			}
			else
			{
				for (int i = children.getLength() - 1; i >= 0; i--)
				{
					if (children.item(i).getNodeName().equals("uses"))
					{
						if (!localVariables.contains(children.item(i).getLastChild().getTextContent()))
						{
							localVariables.add(children.item(i).getLastChild().getTextContent());

							Node globalVariable = TreeToAE2.doc.createElement("uses");
							Node variableType = children.item(i).getFirstChild().cloneNode(true);
							Node variableName = children.item(i).getLastChild().cloneNode(true);

							variableName.setTextContent(name + ":" + children.item(i).getLastChild().getTextContent());

							globalVariable.appendChild(variableType);
							globalVariable.appendChild(variableName);
							globalData.appendChild(globalVariable);
						}

					}
					changeFunctionVariablesToGlobal(name,children.item(i),globalData,localVariables);
				}
			}
		}
	}

}
 
Example 17
Project: Hive_Game   File: OptionManager.java   Source Code and License Vote up 4 votes
public static void modifyOptions(int res, boolean fs, boolean help, boolean anim, boolean grid) throws SAXException, IOException, ParserConfigurationException, TransformerException{
    resolution = res;
    fullscreen = fs;
    helpEnable = help;
    animationsEnable = anim;
    gridEnable = grid;
    
    String filepath = "Hive_init/init.xml";
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document doc = docBuilder.parse(filepath);
    
    Node n = doc.getElementsByTagName("resolution").item(0);
    n.setTextContent(String.valueOf(res));
    
    n = doc.getElementsByTagName("fullscreen").item(0);
    if(fs)
        n.setTextContent("1");
    else
        n.setTextContent("0");
    
    n = doc.getElementsByTagName("help").item(0);
    if(help)
        n.setTextContent("1");
    else
        n.setTextContent("0");
    
    n = doc.getElementsByTagName("animations").item(0);
    if(anim)
        n.setTextContent("1");
    else
        n.setTextContent("0");
    
    n = doc.getElementsByTagName("grid").item(0);
    if(grid)
        n.setTextContent("1");
    else
        n.setTextContent("0");
    
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(new File(filepath));
    transformer.transform(source, result);
}
 
Example 18
Project: convertigo-engine   File: SqlTransaction.java   Source Code and License Vote up 4 votes
private Element parseResultsFlat(List<List<String>> lines, List<String> columnHeaders) {
	Document doc = createDOM(getEncodingCharSet());
	Element document = (Element) doc.appendChild(doc.createElement("document"));
	Element sqlOutput = (Element) document.appendChild(doc.createElement("sql_output"));
	Element element = null;
	
	if (columnHeaders.size() > 0) {
		for (List<String> line : lines) {
			element = doc.createElement(getRowTagname());

			Iterator<String> lineIterator = line.iterator();
			for (String columnName : columnHeaders) {
				if (columnName.isEmpty()) {
					columnName = getColumnTagname();
				}
				String normalizedColumnName = StringUtils.normalize(columnName);
				String value = lineIterator.next();

				if (xmlMode.is(XmlMode.raw)) {
					int x = 1;
					String t = "";
					while (!element.getAttribute(normalizedColumnName + t).equals("")) {
						t ="_" + String.valueOf(x);											
						x++;
					}

					element.setAttribute(normalizedColumnName + t,value);
				} else if (xmlMode.is(XmlMode.flat_element)) {
					Node node = doc.createElement(normalizedColumnName);
					node.setTextContent(value);
					element.appendChild(node);
				}
			}

			sqlOutput.appendChild(element);
		}
	}
	
	doc.getDocumentElement().appendChild(sqlOutput);
	
	Document output = context.outputDocument;
	Element elt = (Element) output.importNode(sqlOutput, true);
	return elt;
}
 
Example 19
Project: Equella   File: XmlDocument.java   Source Code and License Vote up 4 votes
public synchronized Node createNodeFromXPath(String xpath, String nodeValue)
{
	Node element = createNodeFromXPath(xpath);
	element.setTextContent(nodeValue);
	return element;
}
 
Example 20
Project: hashsdn-controller   File: DocumentedException.java   Source Code and License Vote up 4 votes
private Node createTextNode( final Document doc, final String tag, final String textContent ) {
    Node node = doc.createElementNS( URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0, tag );
    node.setTextContent( textContent );
    return node;
}