当前位置: 首页>>代码示例>>Java>>正文


Java Document.createAttributeNS方法代码示例

本文整理汇总了Java中org.w3c.dom.Document.createAttributeNS方法的典型用法代码示例。如果您正苦于以下问题:Java Document.createAttributeNS方法的具体用法?Java Document.createAttributeNS怎么用?Java Document.createAttributeNS使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.w3c.dom.Document的用法示例。


在下文中一共展示了Document.createAttributeNS方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: constructAttribute

import org.w3c.dom.Document; //导入方法依赖的package包/类
/**
 * Constructs an attribute owned by the given document with the given name.
 * 
 * @param document the owning document
 * @param namespaceURI the URI for the namespace the attribute is in
 * @param localName the local name
 * @param prefix the prefix of the namespace that attribute is in
 * 
 * @return the constructed attribute
 */
public static Attr constructAttribute(Document document, String namespaceURI, String localName, String prefix) {
    String trimmedLocalName = DatatypeHelper.safeTrimOrNullString(localName);

    if (trimmedLocalName == null) {
        throw new IllegalArgumentException("Local name may not be null or empty");
    }

    String qualifiedName;
    String trimmedPrefix = DatatypeHelper.safeTrimOrNullString(prefix);
    if (trimmedPrefix != null) {
        qualifiedName = trimmedPrefix + ":" + DatatypeHelper.safeTrimOrNullString(trimmedLocalName);
    } else {
        qualifiedName = DatatypeHelper.safeTrimOrNullString(trimmedLocalName);
    }

    if (DatatypeHelper.isEmpty(namespaceURI)) {
        return document.createAttributeNS(null, qualifiedName);
    } else {
        return document.createAttributeNS(namespaceURI, qualifiedName);
    }
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:32,代码来源:XMLHelper.java

示例2: duplicateNode

import org.w3c.dom.Document; //导入方法依赖的package包/类
static Node duplicateNode(Document document, Node node) {
    Node newNode;
    if (node.getNamespaceURI() != null) {
        newNode = document.createElementNS(node.getNamespaceURI(), node.getLocalName());
    } else {
        newNode = document.createElement(node.getNodeName());
    }

    // copy the attributes
    NamedNodeMap attributes = node.getAttributes();
    for (int i = 0 ; i < attributes.getLength(); i++) {
        Attr attr = (Attr) attributes.item(i);

        Attr newAttr;
        if (attr.getNamespaceURI() != null) {
            newAttr = document.createAttributeNS(attr.getNamespaceURI(), attr.getLocalName());
            newNode.getAttributes().setNamedItemNS(newAttr);
        } else {
            newAttr = document.createAttribute(attr.getName());
            newNode.getAttributes().setNamedItem(newAttr);
        }

        newAttr.setValue(attr.getValue());
    }

    // then duplicate the sub-nodes.
    NodeList children = node.getChildNodes();
    for (int i = 0 ; i < children.getLength() ; i++) {
        Node child = children.item(i);
        if (child.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }
        Node duplicatedChild = duplicateNode(document, child);
        newNode.appendChild(duplicatedChild);
    }

    return newNode;
}
 
开发者ID:tranleduy2000,项目名称:javaide,代码行数:39,代码来源:NodeUtils.java

示例3: addAttribute

import org.w3c.dom.Document; //导入方法依赖的package包/类
static void addAttribute(Document document, Node node,
                         String namespaceUri, String attrName, String attrValue) {
    Attr attr;
    if (namespaceUri != null) {
        attr = document.createAttributeNS(namespaceUri, attrName);
    } else {
        attr = document.createAttribute(attrName);
    }

    attr.setValue(attrValue);

    if (namespaceUri != null) {
        node.getAttributes().setNamedItemNS(attr);
    } else {
        node.getAttributes().setNamedItem(attr);
    }
}
 
开发者ID:tranleduy2000,项目名称:javaide,代码行数:18,代码来源:NodeUtils.java

示例4: getNullNode

import org.w3c.dom.Document; //导入方法依赖的package包/类
protected Attr getNullNode(Document ownerDocument) {
    if (nullNode == null) {
        try {
            nullNode = ownerDocument.createAttributeNS(
                                Constants.NamespaceSpecNS, XMLNS);
            nullNode.setValue("");
        } catch (Exception e) {
            throw new RuntimeException("Unable to create nullNode: " + e);
        }
    }
    return nullNode;
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:13,代码来源:CanonicalizerBase.java

示例5: testWellFormed001

import org.w3c.dom.Document; //导入方法依赖的package包/类
/**
 * Equivalence class partitioning with state and input values orientation
 * for public void setParameter(String name, Object value) throws
 * DOMException, <br>
 * <b>pre-conditions</b>: the attribute has EntityReference to '&lt;', <br>
 * <b>name</b>: well-formed <br>
 * <b>value</b>: true. <br>
 * <b>Expected results</b>: An error is reported
 */
@Test
public void testWellFormed001() {
    Document doc = null;
    try {
        doc = loadDocument(null, test2_xml);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }

    DOMConfiguration config = doc.getDomConfig();
    if (!config.canSetParameter("well-formed", Boolean.TRUE)) {
        Assert.fail("setting 'well-formed' to true is not supported");
    }
    config.setParameter("well-formed", Boolean.TRUE);

    Element root = doc.getDocumentElement();

    Attr attr = doc.createAttributeNS(null, "attr");

    try {
        attr.appendChild(doc.createEntityReference("<"));
    } catch (DOMException domException) {
        System.out.println("testWellFormed001: Expected DOMException for Attribute value = '<'" + domException.toString());
        return; // OK
    }

    root.setAttributeNode(attr);

    TestHandler testHandler = new TestHandler();
    config.setParameter("error-handler", testHandler);

    doc.normalizeDocument();

    if (testHandler.getError() == null && null == testHandler.getFatalError()) {
        Assert.fail("no error was reported when attribute has <");
    }

    return; // Status.passed("OK");
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:49,代码来源:DOMConfigurationTest.java

示例6: testWellFormed002

import org.w3c.dom.Document; //导入方法依赖的package包/类
/**
 * Equivalence class partitioning with state and input values orientation
 * for public void setParameter(String name, Object value) throws
 * DOMException, <br>
 * <b>pre-conditions</b>: the attribute has EntityReference to '&lt;', <br>
 * <b>name</b>: well-formed <br>
 * <b>value</b>: false. <br>
 * <b>Expected results</b>: No error is reported
 */
@Test
public void testWellFormed002() {
    Document doc = null;
    try {
        doc = loadDocument(null, test2_xml);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }

    DOMConfiguration config = doc.getDomConfig();
    if (!config.canSetParameter("well-formed", Boolean.FALSE)) {
        System.out.println("OK, setting 'well-formed' to false is not supported");
        return;
    }
    config.setParameter("well-formed", Boolean.FALSE);

    Element root = doc.getDocumentElement();

    Attr attr = doc.createAttributeNS(null, "attr");
    attr.appendChild(doc.createEntityReference("x"));

    root.setAttributeNode(attr);

    TestHandler testHandler = new TestHandler();
    config.setParameter("error-handler", testHandler);

    doc.normalizeDocument();

    if (testHandler.getError() != null || null != testHandler.getFatalError()) {
        Assert.fail("unexpected error: " + testHandler.getFatalError() + "; " + testHandler.getError());
    }

    return; // Status.passed("OK");

}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:45,代码来源:DOMConfigurationTest.java

示例7: testCreateAttributeNS

import org.w3c.dom.Document; //导入方法依赖的package包/类
@Test(dataProvider = "valid-nsuri")
public void testCreateAttributeNS(String namespaceURI, String name) throws Exception {
    Document document = createDOMWithNS("DocumentTest01.xml");
    Attr attr = document.createAttributeNS(namespaceURI, name);
    assertEquals(attr.getNamespaceURI(), namespaceURI);
    assertEquals(attr.getName(), name);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:8,代码来源:DocumentTest.java

示例8: testAddNewAttributeNode

import org.w3c.dom.Document; //导入方法依赖的package包/类
@Test
public void testAddNewAttributeNode() throws Exception {
    Document document = createDOMWithNS("DocumentTest01.xml");

    NodeList nodeList = document.getElementsByTagNameNS("http://www.w3.org/TR/REC-html40", "body");
    NodeList childList = nodeList.item(0).getChildNodes();
    Element child = (Element) childList.item(1);
    Attr a = document.createAttributeNS("urn:BooksAreUs.org:BookInfo", "attributeNew");
    child.setAttributeNode(a);
    assertNotNull(child.getAttributeNodeNS("urn:BooksAreUs.org:BookInfo", "attributeNew"));
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:12,代码来源:DocumentTest.java

示例9: testSetAttributeNodeNS

import org.w3c.dom.Document; //导入方法依赖的package包/类
@Test(dataProvider = "nsattribute")
public void testSetAttributeNodeNS(String qualifiedName, String localName, String value) throws Exception {
    Document document = createDOM("ElementSample03.xml");
    Element elemNode = document.createElement("pricetag2");
    Attr myAttr = document.createAttributeNS(XML_NS_URI, qualifiedName);
    myAttr.setValue(value);
    assertNull(elemNode.setAttributeNodeNS(myAttr));
    assertEquals(elemNode.getAttributeNS(XML_NS_URI, localName), value);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:10,代码来源:ElementTest.java

示例10: testSetNamedItemNS

import org.w3c.dom.Document; //导入方法依赖的package包/类
@Test
public void testSetNamedItemNS() throws Exception {
    final String nsURI = "urn:BooksAreUs.org:BookInfo";
    Document document = createDOMWithNS("NamedNodeMap01.xml");
    NodeList nodeList = document.getElementsByTagName("body");
    nodeList = nodeList.item(0).getChildNodes();
    Node n = nodeList.item(3);

    NamedNodeMap namedNodeMap = n.getAttributes();

    // creating an Attribute using createAttributeNS
    // method having the same namespaceURI
    // and the same qualified name as the existing one in the xml file
    Attr attr = document.createAttributeNS(nsURI, "b:style");
    // setting to a new Value
    attr.setValue("newValue");
    Node replacedAttr = namedNodeMap.setNamedItemNS(attr); // return the replaced attr
    assertEquals(replacedAttr.getNodeValue(), "font-family");
    Node updatedAttr = namedNodeMap.getNamedItemNS(nsURI, "style");
    assertEquals(updatedAttr.getNodeValue(), "newValue");


    // creating a non existing attribute node
    attr = document.createAttributeNS(nsURI, "b:newNode");
    attr.setValue("newValue");

    assertNull(namedNodeMap.setNamedItemNS(attr)); // return null

    // checking if the node could be accessed
    // using the getNamedItemNS method
    Node newAttr = namedNodeMap.getNamedItemNS(nsURI, "newNode");
    assertEquals(newAttr.getNodeValue(), "newValue");
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:34,代码来源:NamedNodeMapTest.java

示例11: testCreateNewUser

import org.w3c.dom.Document; //导入方法依赖的package包/类
/**
 * Checking when creating an XML document using DOM Level 2 validating
 * it without having a schema source or a schema location It must throw a
 * sax parse exception.
 *
 * @throws Exception If any errors occur.
 */
@Test
public void testCreateNewUser() throws Exception {
    String resultFile = USER_DIR + "accountInfoOut.xml";
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    dbf.setValidating(true);

    DocumentBuilder docBuilder = dbf.newDocumentBuilder();
    MyErrorHandler eh = new MyErrorHandler();
    docBuilder.setErrorHandler(eh);

    Document document = docBuilder.newDocument();

    Element account = document.createElementNS(PORTAL_ACCOUNT_NS, "acc:Account");
    Attr accountID = document.createAttributeNS(PORTAL_ACCOUNT_NS, "acc:accountID");
    account.setAttributeNode(accountID);

    account.appendChild(document.createElement("FirstName"));
    account.appendChild(document.createElementNS(PORTAL_ACCOUNT_NS, "acc:LastName"));
    account.appendChild(document.createElement("UserID"));

    DOMImplementationLS impl
            = (DOMImplementationLS) DOMImplementationRegistry
                    .newInstance().getDOMImplementation("LS");
    LSSerializer writer = impl.createLSSerializer();
    LSParser builder = impl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);
    try(FileOutputStream output = new FileOutputStream(resultFile)) {
        MyDOMOutput domOutput = new MyDOMOutput();
        domOutput.setByteStream(output);
        writer.write(account, domOutput);
        docBuilder.parse(resultFile);
    }
    assertTrue(eh.isAnyError());
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:42,代码来源:UserController.java

示例12: duplicateNode

import org.w3c.dom.Document; //导入方法依赖的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

示例13: testCreateAttributeNSNeg

import org.w3c.dom.Document; //导入方法依赖的package包/类
@Test(dataProvider = "invalid-nsuri", expectedExceptions = DOMException.class)
public void testCreateAttributeNSNeg(String namespaceURI, String name) throws Exception {
    Document document = createDOMWithNS("DocumentTest01.xml");
    document.createAttributeNS(namespaceURI, name);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:6,代码来源:DocumentTest.java


注:本文中的org.w3c.dom.Document.createAttributeNS方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。