當前位置: 首頁>>代碼示例>>Java>>正文


Java Attr.setPrefix方法代碼示例

本文整理匯總了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);
    }
}
 
開發者ID:SimY4,項目名稱:xpath-to-xml,代碼行數:23,代碼來源:DomNavigator.java

示例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;
}
 
開發者ID:tranleduy2000,項目名稱:javaide,代碼行數:67,代碼來源:SdkRepoSource.java


注:本文中的org.w3c.dom.Attr.setPrefix方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。