本文整理了Java中org.w3c.dom.Element.setAttribute()
方法的一些代码示例,展示了Element.setAttribute()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Element.setAttribute()
方法的具体详情如下:
包路径:org.w3c.dom.Element
类名称:Element
方法名:setAttribute
[英]Adds a new attribute. If an attribute with that name is already present in the element, its value is changed to be that of the value parameter. This value is a simple string; it is not parsed as it is being set. So any markup (such as syntax to be recognized as an entity reference) is treated as literal text, and needs to be appropriately escaped by the implementation when it is written out. In order to assign an attribute value that contains entity references, the user must create an Attr
node plus any Text
and EntityReference
nodes, build the appropriate subtree, and use setAttributeNode
to assign it as the value of an attribute.
To set an attribute with a qualified name and namespace URI, use the setAttributeNS
method.
[中]添加新属性。如果元素中已存在具有该名称的属性,则其值将更改为value参数的值。这个值是一个简单的字符串;它在设置时不会被解析。因此,任何标记(如要识别为实体引用的语法)都被视为文字文本,并且需要在写出时由实现进行适当的转义。为了分配包含实体引用的属性值,用户必须创建Attr
节点加上任何Text
和EntityReference
节点,构建适当的子树,并使用setAttributeNode
将其分配为属性值。
要设置具有限定名称和命名空间URI的属性,请使用setAttributeNS
方法。
代码示例来源:origin: pmd/pmd
private Element createExcludeElement(String exclude) {
Element element = document.createElementNS(RULESET_2_0_0_NS_URI, "exclude");
element.setAttribute("name", exclude);
return element;
}
代码示例来源:origin: plantuml/plantuml
private Element getRootNode() {
// Create the root node named svg and append it to
// the document.
final Element svg = (Element) document.createElement("svg");
document.appendChild(svg);
// Set some attributes on the root node that are
// required for proper rendering. Note that the
// approach used here is somewhat different from the
// approach used in the earlier program named Svg01,
// particularly with regard to the style.
svg.setAttribute("xmlns", "http://www.w3.org/2000/svg");
svg.setAttribute("xmlns:xlink", "http://www.w3.org/1999/xlink");
svg.setAttribute("version", "1.1");
return svg;
}
代码示例来源:origin: apache/nifi
private static void addVariable(final Element parentElement, final String variableName, final String variableValue) {
final Element variableElement = parentElement.getOwnerDocument().createElement("variable");
variableElement.setAttribute("name", variableName);
variableElement.setAttribute("value", variableValue);
parentElement.appendChild(variableElement);
}
代码示例来源:origin: jMonkeyEngine/jmonkeyengine
/**
* appends a new Element with the given name to currentElement, sets
* currentElement to be new Element, and returns the new Element as well
*/
private Element appendElement(String name) {
Element ret = doc.createElement(name);
if (currentElement == null) {
ret.setAttribute("format_version", Integer.toString(FormatVersion.VERSION));
doc.appendChild(ret);
} else {
currentElement.appendChild(ret);
}
currentElement = ret;
return ret;
}
代码示例来源:origin: robovm/robovm
Element node = factory.createElement("foundJar");
node.setAttribute("name", keyStr.substring(0, keyStr.indexOf("-")));
node.setAttribute("desc", keyStr.substring(keyStr.indexOf("-") + 1));
node.appendChild(factory.createTextNode((String)subhash.get(keyStr)));
container.appendChild(node);
Element node = factory.createElement("foundJar");
node.appendChild(factory.createTextNode(ERROR + " Reading " + key + " threw: " + e.toString()));
container.appendChild(node);
代码示例来源:origin: plantuml/plantuml
private void addFilter(Element filter, String name, String... data) {
final Element elt = (Element) document.createElement(name);
for (int i = 0; i < data.length; i += 2) {
elt.setAttribute(data[i], data[i + 1]);
}
filter.appendChild(elt);
}
代码示例来源:origin: org.apache.poi/poi-ooxml
/**
* Use to append default types XML elements, use by the save() method.
*
* @param root
* XML parent element use to append this default type element.
* @param entry
* The values to append.
* @see #save(java.io.OutputStream)
*/
private void appendDefaultType(Element root, Entry<String, String> entry) {
Element defaultType = root.getOwnerDocument().createElementNS(TYPES_NAMESPACE_URI, DEFAULT_TAG_NAME);
defaultType.setAttribute(EXTENSION_ATTRIBUTE_NAME, entry.getKey());
defaultType.setAttribute(CONTENT_TYPE_ATTRIBUTE_NAME, entry.getValue());
root.appendChild(defaultType);
}
代码示例来源:origin: plutext/docx4j
static class LoggingErrorListener implements ErrorListener {
// See http://www.cafeconleche.org/slides/sd2003west/xmlandjava/346.html
boolean strict;
public LoggingErrorListener(boolean strict) {
}
public void warning(TransformerException exception) {
log.warn(exception.getMessage(), exception);
// Don't throw an exception and stop the processor
// just for a warning; but do log the problem
}
public void error(TransformerException exception)
throws TransformerException {
log.error(exception.getMessage(), exception);
// XSLT is not as draconian as XML. There are numerous errors
// which the processor may but does not have to recover from;
// e.g. multiple templates that match a node with the same
// priority. If I do not want to allow that, I'd throw this
// exception here.
if (strict) {
throw exception;
}
代码示例来源:origin: ehcache/ehcache3
private Element createServerElement(Document doc, ClusteringServiceConfiguration clusteringServiceConfiguration) {
if (!(clusteringServiceConfiguration.getConnectionSource() instanceof ConnectionSource.ServerList)) {
throw new IllegalArgumentException("When connection URL is null, source of connection MUST be of type ConnectionSource.ServerList.class");
}
ConnectionSource.ServerList servers = (ConnectionSource.ServerList)clusteringServiceConfiguration.getConnectionSource();
Element rootElement = doc.createElementNS(getNamespace().toString(), TC_CLUSTERED_NAMESPACE_PREFIX + CLUSTER_ELEMENT_NAME);
Element connElement = createConnectionElementWrapper(doc, clusteringServiceConfiguration);
servers.getServers().forEach(server -> {
Element serverElement = doc.createElement(TC_CLUSTERED_NAMESPACE_PREFIX + SERVER_ELEMENT_NAME);
serverElement.setAttribute(HOST_ATTRIBUTE_NAME, server.getHostName());
/*
If port is greater than 0, set the attribute. Otherwise, do not set. Default value will be taken.
*/
if (server.getPort() > 0) {
serverElement.setAttribute(PORT_ATTRIBUTE_NAME, Integer.toString(server.getPort()));
}
connElement.appendChild(serverElement);
});
rootElement.appendChild(connElement);
return rootElement;
}
代码示例来源:origin: marytts/marytts
public Document createComponentXML() throws ParserConfigurationException {
DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
fact.setNamespaceAware(true);
Document doc = fact.newDocumentBuilder().newDocument();
Element root = (Element) doc.appendChild(doc.createElementNS(installerNamespaceURI, "marytts-install"));
Element desc = (Element) root.appendChild(doc.createElementNS(installerNamespaceURI, getComponentTypeString()));
desc.setAttribute("locale", MaryUtils.locale2xmllang(locale));
desc.setAttribute("name", name);
desc.setAttribute("version", version);
Element descriptionElt = (Element) desc.appendChild(doc.createElementNS(installerNamespaceURI, "description"));
descriptionElt.setTextContent(description);
Element licenseElt = (Element) desc.appendChild(doc.createElementNS(installerNamespaceURI, "license"));
if (license != null) {
licenseElt.setAttribute("href", license.toString());
Element packageElt = (Element) desc.appendChild(doc.createElementNS(installerNamespaceURI, "package"));
packageElt.setAttribute("size", Integer.toString(packageSize));
packageElt.setAttribute("md5sum", packageMD5);
packageElt.setAttribute("filename", packageFilename);
for (URL l : locations) {
Element lElt = (Element) packageElt.appendChild(doc.createElementNS(installerNamespaceURI, "location"));
lElt.setAttribute("href", urlString);
lElt.setAttribute("folder", String.valueOf(isFolder));
代码示例来源:origin: marytts/marytts
/**
* Enclose the elements' closest common ancestor.
*
* @param first
* first
* @param last
* last
*/
protected void slowDown(Element first, Element last) {
Element phonol = MaryDomUtils.encloseNodesWithNewElement(first, last, MaryXML.PHONOLOGY);
phonol.setAttribute("precision", "precise");
Document doc = phonol.getOwnerDocument();
Element prosody = MaryXML.createElement(doc, MaryXML.PROSODY);
prosody.setAttribute("rate", "-20%");
phonol.getParentNode().insertBefore(prosody, phonol);
prosody.appendChild(phonol);
}
}
代码示例来源:origin: marytts/marytts
@Override
public Document createComponentXML() throws ParserConfigurationException {
Document doc = super.createComponentXML();
NodeList nodes = doc.getElementsByTagName(getComponentTypeString());
assert nodes.getLength() == 1;
Element voiceElt = (Element) nodes.item(0);
voiceElt.setAttribute("type", type);
voiceElt.setAttribute("gender", gender);
Element dependsElt = (Element) voiceElt.appendChild(doc.createElementNS(ComponentDescription.installerNamespaceURI,
"depends"));
dependsElt.setAttribute("language", dependsLanguage);
dependsElt.setAttribute("version", dependsVersion);
return doc;
}
}
代码示例来源:origin: marytts/marytts
protected MaryData createMaryDataFromText(String text, Locale locale) {
Document doc = MaryXML.newDocument();
doc.getDocumentElement().setAttribute("xml:lang", MaryUtils.locale2xmllang(locale));
doc.getDocumentElement().appendChild(doc.createTextNode(text));
MaryData md = new MaryData(MaryDataType.RAWMARYXML, locale);
md.setDocument(doc);
return md;
}
代码示例来源:origin: stackoverflow.com
final DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
final DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
doc = docBuilder.newDocument();
} catch (final ParserConfigurationException e) {
throw new RuntimeException("Can't create SAX parser / DOM builder.", e);
throws SAXException {
addTextIfNeeded();
final Element el = doc.createElement(qName);
for (int i = 0; i < attributes.getLength(); i++) {
el.setAttribute(attributes.getQName(i), attributes.getValue(i));
if (textBuffer.length() > 0) {
final Element el = elementStack.peek();
final Node textNode = doc.createTextNode(textBuffer.toString());
el.appendChild(textNode);
textBuffer.delete(0, textBuffer.length());
代码示例来源:origin: marytts/marytts
/**
* Append one paragraph of text to the rawmaryxml document. If the text language (as determined by #getLanguage(text)) differs
* from the enclosing document's language, the paragraph element is enclosed with a <code><voice xml:lang="..."></code>
* element.
*
* @param text
* the paragraph text.
* @param root
* the root node of the rawmaryxml document, where to insert the paragraph.
* @param defaultLocale
* the default locale, in case the language of the text cannot be determined.
*/
private void appendParagraph(String text, Element root, Locale defaultLocale) {
Element insertHere = root;
String rootLanguage = root.getAttribute("xml:lang");
String textLanguage = MaryUtils.locale2xmllang(determineLocale(text, defaultLocale));
if (!textLanguage.equals(rootLanguage)) {
Element voiceElement = MaryXML.appendChildElement(root, MaryXML.VOICE);
voiceElement.setAttribute("xml:lang", textLanguage);
insertHere = voiceElement;
}
insertHere = MaryXML.appendChildElement(insertHere, MaryXML.PARAGRAPH);
// Now insert the entire plain text as a single text node
insertHere.appendChild(root.getOwnerDocument().createTextNode(text));
// And, for debugging, read it:
Text textNode = (Text) insertHere.getFirstChild();
String textNodeString = textNode.getData();
logger.debug("textNodeString=`" + textNodeString + "'");
}
代码示例来源:origin: robolectric/robolectric
private static Element createMetaDataNode(String name, String value) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
Element metaDataElement;
try {
DocumentBuilder db = dbf.newDocumentBuilder();
metaDataElement = db.newDocument().createElement("meta-data");
metaDataElement.setAttribute("android:name", name);
metaDataElement.setAttribute("android:value", value);
} catch (ParserConfigurationException e) {
throw new RuntimeException(e);
}
return metaDataElement;
}
}
代码示例来源:origin: osmandapp/Osmand
protected static void copyAndReplaceElement(Element oldElement, Element newElement) {
while(oldElement.getChildNodes().getLength() > 0) {
newElement.appendChild(oldElement.getChildNodes().item(0));
}
NamedNodeMap attrs = oldElement.getAttributes();
for(int i = 0; i < attrs.getLength(); i++) {
Node ns = attrs.item(i);
newElement.setAttribute(ns.getNodeName(), ns.getNodeValue());
}
((Element)oldElement.getParentNode()).replaceChild(newElement, oldElement);
}
}
代码示例来源:origin: jamesagnew/hapi-fhir
public static void addTextTag(Document doc, Element element, String name, String namespace, String text, int indent) {
Node node = doc.createTextNode("\n"+Utilities.padLeft("", ' ', indent));
element.appendChild(node);
Element child = doc.createElementNS(namespace, name);
element.appendChild(child);
child.setAttribute("value", text);
}
代码示例来源:origin: pmd/pmd
private Element createDuplicationElement(Document doc, Match match) {
Element duplication = doc.createElement("duplication");
duplication.setAttribute("lines", String.valueOf(match.getLineCount()));
duplication.setAttribute("tokens", String.valueOf(match.getTokenCount()));
return duplication;
}
}
代码示例来源:origin: plutext/docx4j
private static Document makeErr(String msg) {
Document d=XmlUtils.getNewDocumentBuilder().newDocument();
Element span = d.createElement("span");
span.setAttribute("style", "color:red;");
d.appendChild(span);
Text err = d.createTextNode( msg );
span.appendChild(err);
return d;
}
内容来源于网络,如有侵权,请联系作者删除!