org.w3c.dom.Element.setAttributeNode()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(11.9k)|赞(0)|评价(0)|浏览(281)

本文整理了Java中org.w3c.dom.Element.setAttributeNode()方法的一些代码示例,展示了Element.setAttributeNode()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Element.setAttributeNode()方法的具体详情如下:
包路径:org.w3c.dom.Element
类名称:Element
方法名:setAttributeNode

Element.setAttributeNode介绍

[英]Adds a new attribute node. If an attribute with that name ( nodeName) is already present in the element, it is replaced by the new one. Replacing an attribute node by itself has no effect.
To add a new attribute node with a qualified name and namespace URI, use the setAttributeNodeNS method.
[中]添加新的属性节点。如果元素中已存在具有该名称(nodeName)的属性,则该属性将被新属性替换。单独替换属性节点没有效果。
要添加具有限定名称和命名空间URI的新属性节点,请使用setAttributeNodeNS方法。

代码示例

代码示例来源:origin: 4thline/cling

  1. protected Element writeBodyElement(Document d) {
  2. Element envelopeElement = d.createElementNS(Constants.SOAP_NS_ENVELOPE, "s:Envelope");
  3. Attr encodingStyleAttr = d.createAttributeNS(Constants.SOAP_NS_ENVELOPE, "s:encodingStyle");
  4. encodingStyleAttr.setValue(Constants.SOAP_URI_ENCODING_STYLE);
  5. envelopeElement.setAttributeNode(encodingStyleAttr);
  6. d.appendChild(envelopeElement);
  7. Element bodyElement = d.createElementNS(Constants.SOAP_NS_ENVELOPE, "s:Body");
  8. envelopeElement.appendChild(bodyElement);
  9. return bodyElement;
  10. }

代码示例来源:origin: commons-digester/commons-digester

  1. attr = doc.createAttribute(atts.getQName(i));
  2. attr.setNodeValue(atts.getValue(i));
  3. ((Element)top).setAttributeNode(attr);
  4. } else {
  5. attr = doc.createAttributeNS(atts.getURI(i),

代码示例来源:origin: groovy/groovy-core

  1. public static void putAt(Element self, String property, Object value) {
  2. if (property.startsWith("@")) {
  3. String attributeName = property.substring(1);
  4. Document doc = self.getOwnerDocument();
  5. Attr newAttr = doc.createAttribute(attributeName);
  6. newAttr.setValue(value.toString());
  7. self.setAttributeNode(newAttr);
  8. return;
  9. }
  10. InvokerHelper.setProperty(self, property, value);
  11. }

代码示例来源:origin: robovm/robovm

  1. element.setAttributeNode(attr);

代码示例来源:origin: BroadleafCommerce/BroadleafCommerce

  1. @Override
  2. public Node[] merge(List<Node> nodeList1, List<Node> nodeList2, List<Node> exhaustedNodes) {
  3. if (CollectionUtils.isEmpty(nodeList1) || CollectionUtils.isEmpty(nodeList2)) {
  4. return null;
  5. }
  6. Node node1 = nodeList1.get(0);
  7. Node node2 = nodeList2.get(0);
  8. NamedNodeMap attributes2 = node2.getAttributes();
  9. Comparator<Object> nameCompare = new Comparator<Object>() {
  10. @Override
  11. public int compare(Object arg0, Object arg1) {
  12. return ((Node) arg0).getNodeName().compareTo(((Node) arg1).getNodeName());
  13. }
  14. };
  15. Node[] tempNodes = {};
  16. tempNodes = exhaustedNodes.toArray(tempNodes);
  17. Arrays.sort(tempNodes, nameCompare);
  18. int length = attributes2.getLength();
  19. for (int j = 0; j < length; j++) {
  20. Node temp = attributes2.item(j);
  21. int pos = Arrays.binarySearch(tempNodes, temp, nameCompare);
  22. if (pos < 0) {
  23. Attr clone = (Attr) temp.cloneNode(true);
  24. ((Element) node1).setAttributeNode((Attr) node1.getOwnerDocument().importNode(clone, true));
  25. }
  26. }
  27. return null;
  28. }

代码示例来源:origin: stackoverflow.com

  1. staff.setAttributeNode(attr);

代码示例来源:origin: spring-projects/spring-framework

  1. @Test
  2. public void marshalDOMResult() throws Exception {
  3. DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
  4. documentBuilderFactory.setNamespaceAware(true);
  5. DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
  6. Document result = builder.newDocument();
  7. DOMResult domResult = new DOMResult(result);
  8. marshaller.marshal(flights, domResult);
  9. Document expected = builder.newDocument();
  10. Element flightsElement = expected.createElementNS("http://samples.springframework.org/flight", "tns:flights");
  11. Attr namespace = expected.createAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:tns");
  12. namespace.setNodeValue("http://samples.springframework.org/flight");
  13. flightsElement.setAttributeNode(namespace);
  14. expected.appendChild(flightsElement);
  15. Element flightElement = expected.createElementNS("http://samples.springframework.org/flight", "tns:flight");
  16. flightsElement.appendChild(flightElement);
  17. Element numberElement = expected.createElementNS("http://samples.springframework.org/flight", "tns:number");
  18. flightElement.appendChild(numberElement);
  19. Text text = expected.createTextNode("42");
  20. numberElement.appendChild(text);
  21. assertThat("Marshaller writes invalid DOMResult", result, isSimilarTo(expected));
  22. }

代码示例来源:origin: spring-projects/spring-framework

  1. @Test
  2. public void marshalEmptyDOMResult() throws Exception {
  3. DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
  4. documentBuilderFactory.setNamespaceAware(true);
  5. DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
  6. DOMResult domResult = new DOMResult();
  7. marshaller.marshal(flights, domResult);
  8. assertTrue("DOMResult does not contain a Document", domResult.getNode() instanceof Document);
  9. Document result = (Document) domResult.getNode();
  10. Document expected = builder.newDocument();
  11. Element flightsElement = expected.createElementNS("http://samples.springframework.org/flight", "tns:flights");
  12. Attr namespace = expected.createAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:tns");
  13. namespace.setNodeValue("http://samples.springframework.org/flight");
  14. flightsElement.setAttributeNode(namespace);
  15. expected.appendChild(flightsElement);
  16. Element flightElement = expected.createElementNS("http://samples.springframework.org/flight", "tns:flight");
  17. flightsElement.appendChild(flightElement);
  18. Element numberElement = expected.createElementNS("http://samples.springframework.org/flight", "tns:number");
  19. flightElement.appendChild(numberElement);
  20. Text text = expected.createTextNode("42");
  21. numberElement.appendChild(text);
  22. assertThat("Marshaller writes invalid DOMResult", result, isSimilarTo(expected));
  23. }

代码示例来源:origin: kiegroup/jbpm

  1. org.w3c.dom.Node n = ((org.w3c.dom.Node) targetElem).getOwnerDocument().importNode(nl.item(i), true);
  2. if (n instanceof Attr) {
  3. ((Element) targetElem).setAttributeNode((Attr) n);
  4. } else {
  5. ((org.w3c.dom.Node) targetElem).appendChild(n);

代码示例来源:origin: kiegroup/jbpm

  1. @Test
  2. public void testExclusiveSplitXPathAdvanced() throws Exception {
  3. KieBase kbase = createKnowledgeBase("BPMN2-ExclusiveSplitXPath-advanced.bpmn2");
  4. ksession = createKnowledgeSession(kbase);
  5. ksession.getWorkItemManager().registerWorkItemHandler("Email",
  6. new SystemOutWorkItemHandler());
  7. Map<String, Object> params = new HashMap<String, Object>();
  8. Document doc = DocumentBuilderFactory.newInstance()
  9. .newDocumentBuilder().newDocument();
  10. Element hi = doc.createElement("hi");
  11. Element ho = doc.createElement("ho");
  12. hi.appendChild(ho);
  13. Attr attr = doc.createAttribute("value");
  14. ho.setAttributeNode(attr);
  15. attr.setValue("a");
  16. params.put("x", hi);
  17. params.put("y", "Second");
  18. ProcessInstance processInstance = ksession.startProcess(
  19. "com.sample.test", params);
  20. assertProcessInstanceCompleted(processInstance);
  21. }

代码示例来源:origin: kiegroup/jbpm

  1. @Test
  2. public void testExclusiveSplitXPathAdvanced2() throws Exception {
  3. KieBase kbase = createKnowledgeBase("BPMN2-ExclusiveSplitXPath-advanced-vars-not-signaled.bpmn2");
  4. ksession = createKnowledgeSession(kbase);
  5. ksession.getWorkItemManager().registerWorkItemHandler("Email",
  6. new SystemOutWorkItemHandler());
  7. Map<String, Object> params = new HashMap<String, Object>();
  8. Document doc = DocumentBuilderFactory.newInstance()
  9. .newDocumentBuilder().newDocument();
  10. Element hi = doc.createElement("hi");
  11. Element ho = doc.createElement("ho");
  12. hi.appendChild(ho);
  13. Attr attr = doc.createAttribute("value");
  14. ho.setAttributeNode(attr);
  15. attr.setValue("a");
  16. params.put("x", hi);
  17. params.put("y", "Second");
  18. ProcessInstance processInstance = ksession.startProcess(
  19. "com.sample.test", params);
  20. assertProcessInstanceCompleted(processInstance);
  21. }

代码示例来源:origin: kiegroup/jbpm

  1. @Test
  2. public void testExclusiveSplitXPathAdvancedWithVars() throws Exception {
  3. KieBase kbase = createKnowledgeBase("BPMN2-ExclusiveSplitXPath-advanced-with-vars.bpmn2");
  4. ksession = createKnowledgeSession(kbase);
  5. ksession.getWorkItemManager().registerWorkItemHandler("Email",
  6. new SystemOutWorkItemHandler());
  7. Map<String, Object> params = new HashMap<String, Object>();
  8. Document doc = DocumentBuilderFactory.newInstance()
  9. .newDocumentBuilder().newDocument();
  10. Element hi = doc.createElement("hi");
  11. Element ho = doc.createElement("ho");
  12. hi.appendChild(ho);
  13. Attr attr = doc.createAttribute("value");
  14. ho.setAttributeNode(attr);
  15. attr.setValue("a");
  16. params.put("x", hi);
  17. params.put("y", "Second");
  18. ProcessInstance processInstance = ksession.startProcess(
  19. "com.sample.test", params);
  20. assertProcessInstanceCompleted(processInstance);
  21. }

代码示例来源:origin: kingthy/TVRemoteIME

  1. protected Element writeBodyElement(Document d) {
  2. Element envelopeElement = d.createElementNS(Constants.SOAP_NS_ENVELOPE, "s:Envelope");
  3. Attr encodingStyleAttr = d.createAttributeNS(Constants.SOAP_NS_ENVELOPE, "s:encodingStyle");
  4. encodingStyleAttr.setValue(Constants.SOAP_URI_ENCODING_STYLE);
  5. envelopeElement.setAttributeNode(encodingStyleAttr);
  6. d.appendChild(envelopeElement);
  7. Element bodyElement = d.createElementNS(Constants.SOAP_NS_ENVELOPE, "s:Body");
  8. envelopeElement.appendChild(bodyElement);
  9. return bodyElement;
  10. }

代码示例来源:origin: stackoverflow.com

  1. staff.setAttributeNode(attr);

代码示例来源:origin: openmrs/openmrs-core

  1. private Document buildModuleConfig() throws ParserConfigurationException {
  2. DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
  3. DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
  4. Document doc = docBuilder.newDocument();
  5. Element rootElement = doc.createElement("module");
  6. doc.appendChild(rootElement);
  7. Element dwr = doc.createElement("dwr");
  8. dwr.appendChild(doc.createTextNode(""));
  9. rootElement.appendChild(dwr);
  10. Element allow = doc.createElement("allow");
  11. allow.appendChild(doc.createTextNode(""));
  12. dwr.appendChild(allow);
  13. Attr attr = doc.createAttribute("moduleId");
  14. attr.setValue("mymodule");
  15. allow.setAttributeNode(attr);
  16. Element create = doc.createElement("create");
  17. allow.appendChild(create);
  18. return doc;
  19. }

代码示例来源:origin: org.apache.cxf/cxf-api

  1. public void writeAttribute(String local, String value) throws XMLStreamException {
  2. Attr a;
  3. if (local.startsWith("xmlns") && (local.length() == 5 || local.charAt(5) == ':')) {
  4. a = document.createAttributeNS(XML_NS, local);
  5. } else {
  6. a = document.createAttributeNS(null, local);
  7. }
  8. a.setValue(value);
  9. ((Element)currentNode).setAttributeNode(a);
  10. }

代码示例来源:origin: apache/cxf

  1. public void writeAttribute(String local, String value) throws XMLStreamException {
  2. Attr a;
  3. if (local.startsWith("xmlns") && (local.length() == 5 || local.charAt(5) == ':')) {
  4. a = document.createAttributeNS(XML_NS, local);
  5. } else {
  6. a = document.createAttributeNS(null, local);
  7. }
  8. a.setValue(value);
  9. ((Element)currentNode).setAttributeNode(a);
  10. }

代码示例来源:origin: org.apache.cxf/cxf-core

  1. public void writeAttribute(String local, String value) throws XMLStreamException {
  2. Attr a;
  3. if (local.startsWith("xmlns") && (local.length() == 5 || local.charAt(5) == ':')) {
  4. a = document.createAttributeNS(XML_NS, local);
  5. } else {
  6. a = document.createAttributeNS(null, local);
  7. }
  8. a.setValue(value);
  9. ((Element)currentNode).setAttributeNode(a);
  10. }

代码示例来源:origin: org.codehaus.groovy/groovy-xml

  1. public static void putAt(Element self, String property, Object value) {
  2. if (property.startsWith("@")) {
  3. String attributeName = property.substring(1);
  4. Document doc = self.getOwnerDocument();
  5. Attr newAttr = doc.createAttribute(attributeName);
  6. newAttr.setValue(value.toString());
  7. self.setAttributeNode(newAttr);
  8. return;
  9. }
  10. InvokerHelper.setProperty(self, property, value);
  11. }

代码示例来源:origin: httpunit/httpunit

  1. public static Element importNode( DocumentImpl document, Element original, boolean deep ) {
  2. Element copy = document.createElementNS( original.getNamespaceURI(), original.getTagName() );
  3. NamedNodeMap attributes = original.getAttributes();
  4. for (int i = 0; i < attributes.getLength(); i++) {
  5. copy.setAttributeNode( (Attr) document.importNode( attributes.item(i), false ) );
  6. }
  7. if (deep) document.importChildren( original, copy );
  8. return copy;
  9. }

相关文章