List of usage examples for org.w3c.dom NamedNodeMap getLength
public int getLength();
From source file:Main.java
/** * Clones the given DOM node into the given DOM document. * /*from www. j a v a2s . c o m*/ * @param node * The DOM node to clone. * @param doc * The target DOM document. * @return The cloned node in the target DOM document. */ public static Node cloneNode(Node node, Document doc) { Node clone = null; switch (node.getNodeType()) { case Node.ELEMENT_NODE: clone = doc.createElement(node.getNodeName()); NamedNodeMap attrs = node.getAttributes(); for (int i = 0; i < attrs.getLength(); i++) { Node attrNode = attrs.item(i); Attr attrClone = doc.createAttribute(attrNode.getNodeName()); attrClone.setNodeValue(attrNode.getNodeValue()); ((Element) clone).setAttributeNode(attrClone); } // Iterate through each child nodes. NodeList childNodes = node.getChildNodes(); if (childNodes != null) { for (int i = 0; i < childNodes.getLength(); i++) { Node childNode = childNodes.item(i); if (childNode == null) { continue; } Node childClone = cloneNode(childNode, doc); if (childClone == null) { continue; } clone.appendChild(childClone); } } break; case Node.TEXT_NODE: case Node.CDATA_SECTION_NODE: clone = doc.createTextNode(node.getNodeName()); clone.setNodeValue(node.getNodeValue()); break; } return clone; }
From source file:Main.java
/** * Puts the node attributes (if it has any) into HashMap<String,String> Retrieves only those * attributes that are in attr ArrayList Ignores other attributes values that are in node * //from w w w .j a v a2 s . c o m * @param node : XML node to look for its attributes * @param attrNames : attributes to fetch from node * @return Hashmap<String,String> of the node attributes */ public static HashMap<String, String> getAttributesByName(Node node, ArrayList<String> attrNames) { String attrName = null; String attrValue = null; HashMap<String, String> attributesMap = new HashMap<String, String>(); NamedNodeMap attributes = node.getAttributes(); for (int attrIndex = 0; attrIndex < attributes.getLength(); attrIndex++) { attrName = attributes.item(attrIndex).getNodeName(); attrValue = attributes.item(attrIndex).getNodeValue(); if (attrNames.contains(attrName)) { attributesMap.put(attrName, attrValue); } } return attributesMap.size() == 0 ? null : attributesMap; }
From source file:Main.java
@SuppressWarnings("fallthrough") private static void getSetRec(final Node rootNode, final Set<Node> result, final Node exclude, final boolean com) { if (rootNode == exclude) { return;//w w w . j a v a 2 s . c o m } switch (rootNode.getNodeType()) { case Node.ELEMENT_NODE: result.add(rootNode); Element el = (Element) rootNode; if (el.hasAttributes()) { NamedNodeMap nl = el.getAttributes(); for (int i = 0; i < nl.getLength(); i++) { result.add(nl.item(i)); } } //no return keep working case Node.DOCUMENT_NODE: for (Node r = rootNode.getFirstChild(); r != null; r = r.getNextSibling()) { if (r.getNodeType() == Node.TEXT_NODE) { result.add(r); while (r != null && r.getNodeType() == Node.TEXT_NODE) { r = r.getNextSibling(); } if (r == null) { return; } } getSetRec(r, result, exclude, com); } return; case Node.COMMENT_NODE: if (com) { result.add(rootNode); } return; case Node.DOCUMENT_TYPE_NODE: return; default: result.add(rootNode); } }
From source file:Main.java
/** * Finds attribute of node by name./*w w w.jav a 2s. c om*/ * @param node Node The context node. * @param name String * @return Node */ public static Node findAttribute(Node node, String name) { Node found = null; NamedNodeMap nm = node.getAttributes(); if (nm != null) { for (int i = 0; i < nm.getLength(); i++) { Node attr = nm.item(i); if (attr.getNodeName().compareToIgnoreCase(name) == 0) { found = attr; break; } } } return found; }
From source file:Main.java
static public List<Node> getAttributeNodeList(Element element, Pattern name) { NamedNodeMap nodes = element.getAttributes(); List<Node> attributes = new ArrayList<>(); int i, size = nodes.getLength(); Node node;//from w ww . jav a 2s . c o m for (i = 0; i < size; i++) { node = nodes.item(i); if (name.matcher(node.getNodeName()).find()) attributes.add(node); } return attributes; }
From source file:Main.java
public static void spreadNamespaces(Node node, String tns, boolean overwrite) { Document doc = node instanceof Document ? (Document) node : node.getOwnerDocument(); boolean isParent = false; while (node != null) { Node next = null;//from w w w.j a va 2 s . c om if (!isParent && node.getNodeType() == Node.ELEMENT_NODE) { if (node.getNamespaceURI() == null) { node = doc.renameNode(node, tns, node.getNodeName()); } else { if (overwrite) { tns = node.getNamespaceURI(); } } NamedNodeMap nodeMap = node.getAttributes(); int nodeMapLengthl = nodeMap.getLength(); for (int i = 0; i < nodeMapLengthl; i++) { Node attr = nodeMap.item(i); if (attr.getNamespaceURI() == null) { doc.renameNode(attr, tns, attr.getNodeName()); } } } isParent = (isParent || (next = node.getFirstChild()) == null) && (next = node.getNextSibling()) == null; node = isParent ? node.getParentNode() : next; if (isParent && node != null) { if (overwrite) { tns = node.getNamespaceURI(); } } } }
From source file:Main.java
public static Element overrideXml(Element target, Element parent) { if (parent != null) { NamedNodeMap namedNodeMap = parent.getAttributes(); for (int i = 0; i < namedNodeMap.getLength(); i++) { Node attributeNode = namedNodeMap.item(i); String parentAttributeName = attributeNode.getNodeName(); String parentAttributeValue = attributeNode.getNodeValue(); // attribute override if (!target.hasAttribute(parentAttributeName)) { target.setAttribute(parentAttributeName, parentAttributeValue); }/* w w w . j a va 2s. c o m*/ // children override if (parent.getChildNodes().getLength() > 0) { if (target.getChildNodes().getLength() == 0) { for (int j = 0; j < target.getChildNodes().getLength(); j++) { target.appendChild(target.getChildNodes().item(j)); } } } } } return target; }
From source file:Main.java
private static void fixupAttrsSingle(Element e) throws DOMException { removeXmlBase(e);//from w ww.jav a 2 s. c om Map<String, String> replace = new HashMap<String, String>(); NamedNodeMap attrs = e.getAttributes(); for (int j = 0; j < attrs.getLength(); j++) { Attr attr = (Attr) attrs.item(j); if (attr.getNamespaceURI() == null && !attr.getName().equals("xmlns")) { // NOI18N replace.put(attr.getName(), attr.getValue()); } } for (Map.Entry<String, String> entry : replace.entrySet()) { e.removeAttribute(entry.getKey()); e.setAttributeNS(null, entry.getKey(), entry.getValue()); } }
From source file:Main.java
private static void convert(Node toCopy, Node saveTo, Document doc) { Node newNode;//from w w w. j ava 2 s . c om switch (toCopy.getNodeType()) { case Node.ELEMENT_NODE: Element newElement = doc.createElementNS(toCopy.getNamespaceURI(), toCopy.getNodeName()); newNode = newElement; Element baseElement = (Element) toCopy; NamedNodeMap children = baseElement.getAttributes(); for (int i = 0; i < children.getLength(); i++) { convertAttribute((Attr) children.item(i), newElement, doc); } break; case Node.TEXT_NODE: newNode = doc.createTextNode(toCopy.getTextContent()); break; default: newNode = null; } if (newNode != null) { NodeList children = toCopy.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { convert(children.item(i), newNode, doc); } saveTo.appendChild(newNode); } }
From source file:Main.java
/** * Find all attribute in a node with specific prefix. * * @param node the note that contains the attributes * @param prefix the prefix to search for * @param removePrefix true: removes the prefix from attribute name (name is the key of resulting map) * @return the values or a empty map/*w w w. j a v a 2 s . c o m*/ */ public static HashMap<String, String> getAttributes(Node node, String prefix, boolean removePrefix) { final HashMap<String, String> result = new HashMap<>(); final NamedNodeMap attributes = node.getAttributes(); for (int i = 0; i < attributes.getLength(); i++) { final Node att = attributes.item(i); String name = att.getNodeName(); if (name.startsWith(prefix)) { if (removePrefix) { name = name.substring(prefix.length()); } result.put(name, att.getNodeValue()); } } return result; }