本文整理汇总了Java中org.w3c.dom.Attr.setPrefix方法的典型用法代码示例。如果您正苦于以下问题:Java Attr.setPrefix方法的具体用法?Java Attr.setPrefix怎么用?Java Attr.setPrefix使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.w3c.dom.Attr
的用法示例。
在下文中一共展示了Attr.setPrefix方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createAttribute
import org.w3c.dom.Attr; //导入方法依赖的package包/类
@Override
public DomNode createAttribute(DomNode parent, QName attribute) throws XmlBuilderException {
final org.w3c.dom.Node parentNode = parent.getNode();
if (org.w3c.dom.Node.ELEMENT_NODE != parentNode.getNodeType()) {
throw new XmlBuilderException("Unable to append attribute to a non-element node " + parent);
}
try {
Attr attr;
final Element parentElement = (Element) parentNode;
if (XMLConstants.NULL_NS_URI.equals(attribute.getNamespaceURI())) {
attr = document.createAttribute(attribute.getLocalPart());
} else {
attr = document.createAttributeNS(attribute.getNamespaceURI(), attribute.getLocalPart());
attr.setPrefix(attribute.getPrefix());
}
parentElement.setAttributeNode(attr);
return new DomNode(attr);
} catch (DOMException de) {
throw new XmlBuilderException("Unable to create attribute: " + attribute, de);
}
}
示例2: duplicateNode
import org.w3c.dom.Attr; //导入方法依赖的package包/类
/**
* Helper method used by {@link #findAlternateToolsXml(InputStream)} to duplicate a node
* and attach it to the given root in the new document.
*/
private Element duplicateNode(Element newRootNode, Element oldNode,
String namespaceUri, String prefix) {
// The implementation here is more or less equivalent to
//
// newRoot.appendChild(newDoc.importNode(oldNode, deep=true))
//
// except we can't just use importNode() since we need to deal with the fact
// that the old document is not namespace-aware yet the new one is.
Document newDoc = newRootNode.getOwnerDocument();
Element newNode = null;
String nodeName = oldNode.getNodeName();
int pos = nodeName.indexOf(':');
if (pos > 0 && pos < nodeName.length() - 1) {
nodeName = nodeName.substring(pos + 1);
newNode = newDoc.createElementNS(namespaceUri, nodeName);
newNode.setPrefix(prefix);
} else {
newNode = newDoc.createElement(nodeName);
}
newRootNode.appendChild(newNode);
// Merge in all the attributes
NamedNodeMap attrs = oldNode.getAttributes();
for (int i = 0; i < attrs.getLength(); i++) {
Attr attr = (Attr) attrs.item(i);
Attr newAttr = null;
String attrName = attr.getNodeName();
pos = attrName.indexOf(':');
if (pos > 0 && pos < attrName.length() - 1) {
attrName = attrName.substring(pos + 1);
newAttr = newDoc.createAttributeNS(namespaceUri, attrName);
newAttr.setPrefix(prefix);
} else {
newAttr = newDoc.createAttribute(attrName);
}
newAttr.setNodeValue(attr.getNodeValue());
if (pos > 0) {
newNode.getAttributes().setNamedItemNS(newAttr);
} else {
newNode.getAttributes().setNamedItem(newAttr);
}
}
// Merge all child elements and texts
for (Node child = oldNode.getFirstChild(); child != null; child = child.getNextSibling()) {
if (child.getNodeType() == Node.ELEMENT_NODE) {
duplicateNode(newNode, (Element) child, namespaceUri, prefix);
} else if (child.getNodeType() == Node.TEXT_NODE) {
Text newText = newDoc.createTextNode(child.getNodeValue());
newNode.appendChild(newText);
}
}
return newNode;
}