public static void copyContent(Element source, Element destination) { NamedNodeMap attributes = source.getAttributes(); for (int i = 0; i < attributes.getLength(); i++) { Attr attr = (Attr) attributes.item(i); Attr clone = (Attr) attr.cloneNode(true); destination.setAttributeNode(clone); } NodeList childNodes = source.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node item = childNodes.item(i); destination.appendChild(item); } }
public void copyAttributes(Element from, Element to) { NamedNodeMap attributes = from.getAttributes(); for (int i = 0; i < attributes.getLength(); i++) { Attr node = (Attr) attributes.item(i); to.setAttributeNode((Attr) node.cloneNode(false)); } }
private Attr findXMLNSDeclInParentNode(final Element element, final String prefix) { String attrName = XMLConstants.XMLNS_ATTRIBUTE + ":" + prefix; Element cur = element; while (cur != null) { Attr attr = cur.getAttributeNode(attrName); if (attr != null) { // we don't remove this node, so we have to clone it return (Attr) attr.cloneNode(false); } Node parent = cur.getParentNode(); if (Node.ELEMENT_NODE == parent.getNodeType()) { cur = (Element) parent; } else { cur = null; } } return null; }
private static void swallowStartElement(Element root) { NamedNodeMap attrMap = root.getAttributes(); NodeList childNodes = root.getChildNodes(); if(attrMap.getLength() == 0 && childNodes.getLength() == 1 && childNodes.item(0).getNodeType() == Node.ELEMENT_NODE) { Element child = (Element)childNodes.item(0); if(child.getNodeName().equals(root.getNodeName())) { NamedNodeMap attrs = child.getAttributes(); for(int i=0; i<attrs.getLength(); i++) { Attr attrNode = (Attr)attrs.item(i); root.setAttributeNode(((Attr)attrNode.cloneNode(true))); } while(child.hasChildNodes()) { root.appendChild(child.removeChild(child.getFirstChild())); } root.removeChild(child); } } }
e.setAttributeNodeNS((Attr) a.cloneNode(true));
e.setAttributeNodeNS((Attr) a.cloneNode(true));
e.setAttributeNodeNS((Attr) a.cloneNode(true));
e.setAttributeNodeNS((Attr) a.cloneNode(true));
protected void runTest() throws Throwable { Document document = dbf.newDocumentBuilder().newDocument(); Attr attr = document.createAttributeNS(null, "attr"); attr.appendChild(document.createTextNode("foo")); attr.appendChild(document.createTextNode("bar")); // Note that for an attribute, cloneNode always copies the children, even if deep=false Attr clone = (Attr)attr.cloneNode(deep); Node child = clone.getFirstChild(); assertNotNull(child); assertEquals(Node.TEXT_NODE, child.getNodeType()); assertEquals("foo", child.getNodeValue()); child = child.getNextSibling(); assertEquals(Node.TEXT_NODE, child.getNodeType()); assertEquals("bar", child.getNodeValue()); assertNull(child.getNextSibling()); } }
decl.setAttributeNodeNS( (Attr)a.cloneNode(true) );
decl.setAttributeNodeNS( (Attr)a.cloneNode(true) );
decl.setAttributeNodeNS( (Attr)a.cloneNode(true) );
decl.setAttributeNodeNS( (Attr)a.cloneNode(true) );
decl.setAttributeNodeNS( (Attr)a.cloneNode(true) );
/** * Runs the test case. * @throws Throwable Any uncaught exception causes test to fail */ public void runTest() throws Throwable { Document doc; Attr attr1; Attr attr2; Element addrElement; NodeList elementList; boolean isEqual; String nullNSURI = null; doc = (Document) load("hc_staff", false); elementList = doc.getElementsByTagName("p"); addrElement = (Element) elementList.item(3); attr1 = addrElement.getAttributeNodeNS(nullNSURI, "dir"); attr2 = (Attr) attr1.cloneNode(true); isEqual = attr1.isEqualNode(attr2); assertTrue("nodeisequalnode16", isEqual); } /**
/** * Add child topics' {@link Element} representation to current Element * (recursively called) * @param currentTopicElt current Topic {@link Element} representation * @param currentchildTopic currentChildTo convert as {@link Element} and add * @return */ private static Element addChildren(Element parentTopicElt, List<TopicType> childTopics, Document docToBuildElt, Attr topicAttr){ Element result = null, currentChild = null; List<TopicType> children = null; for (TopicType topicItem : childTopics) { currentChild = docToBuildElt.createElement(topicItem.getName()); currentChild.setAttributeNodeNS(topicAttr); children = topicItem.getTopics(); if (children != null && children.size() > 0){ parentTopicElt.appendChild(WstopDatatypeUtils.addChildren(currentChild, topicItem.getTopics(), docToBuildElt,(Attr) topicAttr.cloneNode(true))); } } return result; }
textNode = doc.createTextNode("terday"); retval = titleAttr.appendChild(textNode); clonedTitle = (Attr) titleAttr.cloneNode(false); textNode.setNodeValue("text_node_not_cloned"); value = clonedTitle.getValue();
/** * Runs the test case. * @throws Throwable Any uncaught exception causes test to fail */ public void runTest() throws Throwable { Document doc; Element element; Element element2; Attr attribute; Attr attributeCloned; Attr newAttr; NodeList elementList; String attrName; String attrValue; String nullNS = null; doc = (Document) load("staffNS", true); elementList = doc.getElementsByTagNameNS("http://www.nist.gov", "address"); element = (Element) elementList.item(1); attribute = element.getAttributeNodeNS(nullNS, "street"); attributeCloned = (Attr) attribute.cloneNode(true); element2 = (Element) elementList.item(2); newAttr = element2.setAttributeNodeNS(attributeCloned); attrName = newAttr.getNodeName(); attrValue = newAttr.getNodeValue(); assertEquals("elementsetattributenodens02_attrName", "street", attrName); assertEquals("elementsetattributenodens02_attrValue", "Yes", attrValue); } /**
currentTopicTree.appendChild(WstopDatatypeUtils.addChildren(currentTopicTree,topicItem.getTopics(),doc,(Attr) topicAttr.cloneNode(true)));
/** * Runs the test case. * @throws Throwable Any uncaught exception causes test to fail */ public void runTest() throws Throwable { Document doc; DocumentFragment docFrag; Attr attr; Attr attrCloned; int docFragPosition; int position1; int position2; int position3; doc = (Document) load("hc_staff", true); docFrag = doc.createDocumentFragment(); attr = doc.createAttributeNS("http://www.w3.org/XML/1998/namespace", "xml:lang"); attrCloned = (Attr) attr.cloneNode(true); position1 = (int) docFrag.compareDocumentPosition(attrCloned); assertEquals("isImplSpecificDisconnected1", 33 & 57, position1 & 57); position2 = (int) attrCloned.compareDocumentPosition(docFrag); assertNotEquals("notBothPreceding", position1 & 2, position2 & 2); assertNotEquals("notBothFollowing", position1 & 4, position2 & 4); assertEquals("isImplSpecificDisconnected2", 33 & 57, position2 & 57); position3 = (int) docFrag.compareDocumentPosition(attrCloned); assertEquals("isConsistent", position1, position3); } /**