Document createAttributeNS - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Document createAttributeNS method.

This code example shows how to use the following methods:createAttributeNS
        String nodeName;
        String nodeValue;
        String prefix;
        String namespaceURI;
        doc = (Document) load("staffNS", builder);
        attribute1 = doc.createAttributeNS(
                "http://www.w3.org/XML/1998/namespace", "xml:xml"); 
        name = attribute1.getName();
        nodeName = attribute1.getNodeName();
        nodeValue = attribute1.getNodeValue();
        prefix = attribute1.getPrefix();
        namespaceURI = attribute1.getNamespaceURI();
        assertEquals("documentcreateattributeNS02_att1_name", "xml:xml", name);
        assertEquals("documentcreateattributeNS02_att1_nodeName", "xml:xml", 
                nodeName);
        assertEquals("documentcreateattributeNS02_att1_nodeValue", "", 
                nodeValue);
        assertEquals("documentcreateattributeNS02_att1_prefix", "xml", prefix);
        assertEquals("documentcreateattributeNS02_att1_namespaceURI", 
                "http://www.w3.org/XML/1998/namespace", namespaceURI);

6
CodeRank
This code example shows how to use the following methods:createAttributeNS, createElementNS
      String elemNoPrefix;
      String elemPrefix;
      String attrNoPrefix;
      String attrPrefix;
      doc = (Document) load("staff", builder);
      element = doc.createElementNS("http://www.w3.org/DOM/Test/elem", "elem");
      qelement = doc.createElementNS("http://www.w3.org/DOM/Test/elem", "qual:qelem");
      attr = doc.createAttributeNS("http://www.w3.org/DOM/Test/attr", "attr");
      qattr = doc.createAttributeNS("http://www.w3.org/DOM/Test/attr", "qual:qattr");
      elemNoPrefix = element.getPrefix();
      elemPrefix = qelement.getPrefix();
      attrNoPrefix = attr.getPrefix();
      attrPrefix = qattr.getPrefix();
      assertNull("nodegetprefix03_1", elemNoPrefix);
      assertEquals("nodegetprefix03_2", "qual", elemPrefix);
      assertNull("nodegetprefix03_3", attrNoPrefix);
      assertEquals("nodegetprefix03_4", "qual", attrPrefix);
      } 
 
} 
6
CodeRank
This code example shows how to use the following methods:createAttributeNS, createElementNS, createTextNode, appendChild
		DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
		Document result = builder.newDocument();
		DOMResult domResult = new DOMResult(result);
		marshaller.marshal(flights, domResult);
		Document expected = builder.newDocument();
		Element flightsElement = expected.createElementNS("http://samples.springframework.org/flight", "tns:flights");
		Attr namespace = expected.createAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:tns");
		namespace.setNodeValue("http://samples.springframework.org/flight");
		flightsElement.setAttributeNode(namespace);
		expected.appendChild(flightsElement);
		Element flightElement = expected.createElementNS("http://samples.springframework.org/flight", "tns:flight");
		flightsElement.appendChild(flightElement);
		Element numberElement = expected.createElementNS("http://samples.springframework.org/flight", "tns:number");
		flightElement.appendChild(numberElement);
		Text text = expected.createTextNode("42");
		numberElement.appendChild(text);
		assertXMLEqual("Marshaller writes invalid DOMResult", expected, result);
	} 
 
	@Test
8
CodeRank
This code example shows how to use the following methods:createAttributeNS, createElementNS
      String attrNSURI;
      String attrNSURINull;
      String nullNS = null;
 
      doc = (Document) load("staff", builder);
      element = doc.createElementNS(nullNS, "elem");
      elementNS = doc.createElementNS("http://www.w3.org/DOM/Test/elem", "qual:qelem");
      attr = doc.createAttributeNS(nullNS, "attr");
      attrNS = doc.createAttributeNS("http://www.w3.org/DOM/Test/attr", "qual:qattr");
      elemNSURI = elementNS.getNamespaceURI();
      elemNSURINull = element.getNamespaceURI();
      attrNSURI = attrNS.getNamespaceURI();
      attrNSURINull = attr.getNamespaceURI();
      assertEquals("nodegetnamespaceuri03_elemNSURI", "http://www.w3.org/DOM/Test/elem", elemNSURI);
      assertNull("nodegetnamespaceuri03_1", elemNSURINull);
      assertEquals("nodegetnamespaceuri03_attrNSURI", "http://www.w3.org/DOM/Test/attr", attrNSURI);
      assertNull("nodegetnamespaceuri03_2", attrNSURINull);
      } 
 
} 
6
CodeRank
This code example shows how to use the following methods:createAttributeNS, createElementNS, createEntityReference
        Attr attribute2;
        EntityReference entRef;
        NodeList elementList;
 
        doc = (Document) load("staffNS", builder);
        element = doc.createElementNS("http://www.w3.org/DOM/Test", "elem1");
        attribute = doc.createAttributeNS("http://www.w3.org/DOM/Test", "attr");
        entRef = doc.createEntityReference("ent4");
        attribute.appendChild(entRef);
        element.setAttributeNodeNS(attribute);
        elementList = entRef.getChildNodes();
        element = (Element) elementList.item(0);
        attribute2 = doc.createAttributeNS("http://www.w3.org/DOM/Test",
                "attr2"); 
 
        { 
            boolean success = false;
            try { 
                element.setAttributeNodeNS(attribute2);
            } catch (DOMException ex) {
6
CodeRank
This code example shows how to use the following methods:createAttributeNS, createElementNS
      String localElemName;
      String localQElemName;
      String localAttrName;
      String localQAttrName;
      doc = (Document) load("staff", builder);
      element = doc.createElementNS("http://www.w3.org/DOM/Test/elem", "elem");
      qelement = doc.createElementNS("http://www.w3.org/DOM/Test/elem", "qual:qelem");
      attr = doc.createAttributeNS("http://www.w3.org/DOM/Test/attr", "attr");
      qattr = doc.createAttributeNS("http://www.w3.org/DOM/Test/attr", "qual:qattr");
      localElemName = element.getLocalName();
      localQElemName = qelement.getLocalName();
      localAttrName = attr.getLocalName();
      localQAttrName = qattr.getLocalName();
      assertEquals("nodegetlocalname03_localElemName", "elem", localElemName);
      assertEquals("nodegetlocalname03_localQElemName", "qelem", localQElemName);
      assertEquals("nodegetlocalname03_localAttrName", "attr", localAttrName);
      assertEquals("nodegetlocalname03_localQAttrName", "qattr", localQAttrName);
      } 
 
} 
6
CodeRank
This code example shows how to use the following methods:createAttributeNS, createElementNS
        String attrName;
        String attNodeName;
        String attrLocalName;
        String attrNS;
        doc = (Document) load("staffNS", builder);
        element = doc.createElementNS("namespaceURI", "root");
        attribute1 = doc.createAttributeNS("http://www.w3.org/DOM/Level2",
                "l2:att"); 
        element.setAttributeNodeNS(attribute1);
        attribute2 = doc.createAttributeNS("http://www.w3.org/DOM/Level1",
                "att"); 
        element.setAttributeNodeNS(attribute2);
        attribute = element.getAttributeNodeNS("http://www.w3.org/DOM/Level2",
                "att"); 
        attrValue = attribute.getNodeValue();
        attrName = attribute.getName();
        attNodeName = attribute.getNodeName();
        attrLocalName = attribute.getLocalName();
        attrNS = attribute.getNamespaceURI();
        assertEquals("elementgetattributenodens01_attrValue", "", attrValue);
6
CodeRank
This code example shows how to use the following methods:createAttributeNS, createElement, getDocumentElement, getElementsByTagName, lookupPrefix
This code example shows how to use the following methods:createAttributeNS, createElementNS
        Attr newAttr1;
        Attr newAttr2;
 
        String attrName;
        doc = (Document) load("staffNS", builder);
        element = doc.createElementNS("http://www.w3.org/DOM/Test", "root");
        newAttr1 = doc.createAttributeNS("http://www.w3.org/DOM/L1", "L1:att");
        ((Element) /* Node */element).setAttributeNodeNS(newAttr1);
        newAttr2 = doc.createAttributeNS("http://www.w3.org/DOM/L2", "L2:att");
        ((Element) /* Node */element).setAttributeNodeNS(newAttr2);
        attributes = element.getAttributes();
        attribute = (Attr) attributes.getNamedItemNS(
                "http://www.w3.org/DOM/L2", "att"); 
        attrName = attribute.getNodeName();
        assertEquals("namednodemapgetnameditemns03", "L2:att", attrName);
    } 
    public void testGetNamedItemNS4() throws Throwable {
        Document doc;
        NamedNodeMap attributes;
        Element element;
6
CodeRank
This code example shows how to use the following methods:createAttributeNS, createElementNS, createTextNode, appendChild
		DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
		Document result = builder.newDocument();
		DOMResult domResult = new DOMResult(result);
		marshaller.marshal(flights, domResult);
		Document expected = builder.newDocument();
		Element flightsElement = expected.createElementNS("http://samples.springframework.org/flight", "tns:flights");
		Attr namespace = expected.createAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:tns");
		namespace.setNodeValue("http://samples.springframework.org/flight");
		flightsElement.setAttributeNode(namespace);
		expected.appendChild(flightsElement);
		Element flightElement = expected.createElementNS("http://samples.springframework.org/flight", "tns:flight");
		flightsElement.appendChild(flightElement);
		Element numberElement = expected.createElementNS("http://samples.springframework.org/flight", "tns:number");
		flightElement.appendChild(numberElement);
		Text text = expected.createTextNode("42");
		numberElement.appendChild(text);
		assertXMLEqual("Marshaller writes invalid DOMResult", expected, result);
	} 
 
	@Test 
6
CodeRank
createAttributeNS Method Overview
Creates an attribute of the given qualified name and namespace URI.
See Code Examples for other Java 8 Document Methods: