List of usage examples for org.w3c.dom NamedNodeMap getLength
public int getLength();
From source file:Main.java
/** * Compares the attributes of two XML <code>Node</code> objects. This method * returns <code>true</code> if all attribute name-value pairs match * disregarding their order of placement. * // w w w.ja v a2 s . co m * @param n1 first <code>Node</code> * @param n2 second <code>Node</code> * * @return boolean * */ public static boolean diffAttributes(Node n1, Node n2) { NamedNodeMap n1Atts = n1.getAttributes(); NamedNodeMap n2Atts = n2.getAttributes(); if (n1Atts.getLength() != n2Atts.getLength()) { return false; } for (int i = 0; i < n1Atts.getLength(); i++) { Node a1 = n1Atts.item(i); Node a2Val = n2Atts.getNamedItem(a1.getNodeName()); if (a2Val == null || !a1.getNodeValue().equals(a2Val.getNodeValue())) { return false; } } return true; }
From source file:Main.java
/** * Gets a attribute value.//from w w w . ja v a 2 s .c om * * @param pNode * the p node * @param pAttributeName * the p attribute name * @return the attribute value */ public static String getAttributeValue(Node pNode, String pAttributeName) { String returnString = null; NamedNodeMap attributes = pNode.getAttributes(); if (attributes != null) { for (int i = 0; i < attributes.getLength(); i++) { if (attributes.item(i).getNodeName().equalsIgnoreCase(pAttributeName)) { returnString = attributes.item(i).getNodeValue(); break; } } } return returnString; }
From source file:Main.java
public static int getAllChildrenString(Node start, int offset) { //String spacers = " "; String tagOpen = "<"; String tagClose = ">"; String tagEndOpen = "</"; String tagEndClose = ">"; String tagName = start.getNodeName(); String tagValue = (start.getNodeValue() == null ? "" : start.getNodeValue()); if (start.getNodeName().trim().equals("#text")) { tagOpen = ""; tagClose = ""; tagName = ""; tagEndOpen = ""; tagEndClose = ""; }/* w ww. j a v a 2 s . c o m*/ if (offset == 0) HTMLString = ""; else { HTMLString += //spacers.substring(0, offset) + tagOpen + tagName; if (start.getNodeType() == Element.ELEMENT_NODE) { NamedNodeMap startAttr = start.getAttributes(); for (int i = 0; i < startAttr.getLength(); i++) { Node attr = startAttr.item(i); HTMLString += " " + attr.getNodeName() + "=\"" + attr.getNodeValue() + "\""; } } HTMLString += tagClose + tagValue; } for (Node child = start.getFirstChild(); child != null; child = child.getNextSibling()) { getAllChildrenString(child, offset + 1); } if (offset > 0 && tagName.length() > 0) { HTMLString += //spacers.substring(0, offset) + tagEndOpen + tagName + tagEndClose; } return ++offset; }
From source file:Main.java
private static void printNote(NodeList nodeList) { for (int count = 0; count < nodeList.getLength(); count++) { Node tempNode = nodeList.item(count); // make sure it's element node. if (tempNode.getNodeType() == Node.ELEMENT_NODE) { // get node name and value System.out.println("\nNode Name =" + tempNode.getNodeName() + " [OPEN]"); System.out.println("Node Value =" + tempNode.getTextContent()); if (tempNode.hasAttributes()) { // get attributes names and values NamedNodeMap nodeMap = tempNode.getAttributes(); for (int i = 0; i < nodeMap.getLength(); i++) { Node node = nodeMap.item(i); System.out.println("attr name : " + node.getNodeName()); System.out.println("attr value : " + node.getNodeValue()); }//from ww w. j a v a 2s . c o m } if (tempNode.hasChildNodes()) { // loop again if has child nodes printNote(tempNode.getChildNodes()); } System.out.println("Node Name =" + tempNode.getNodeName() + " [CLOSE]"); } } }
From source file:Main.java
/** * Get a node from a list with a specific attribute value. * * @param nodeList the list of nodes/*w w w. j a v a 2 s . c o m*/ * @param name the name of the attribute to check against * @param value the value to match * @return the node with matching attribute value */ public static Node getNodeWithGivenValue(final NodeList nodeList, final String name, final String value) { Node node = null; for (int i = 0; i < nodeList.getLength(); i++) { final NamedNodeMap map = nodeList.item(i).getAttributes(); if (map != null) { for (int j = 0; j < map.getLength(); j++) { if ((name.equals(map.item(j).getNodeName())) && (value.equals(map.item(j).getTextContent()))) { node = nodeList.item(i); } } } } return node; }
From source file:Main.java
private static Set<String> getTree(Node doc) { final Set<String> set = new TreeSet<String>(); if (doc == null) return set; String path = new String(); if (doc.getPrefix() != null && doc.getLocalName() != null) { path = new String(doc.getPrefix() + ":" + doc.getLocalName()); set.add(path);/*from w ww . j a va 2s . c o m*/ } final NamedNodeMap attributes = doc.getAttributes(); for (int i = 0; attributes != null && i < attributes.getLength(); i++) { final Node attribute = attributes.item(i); String name = attribute.getLocalName(); if (attribute.getPrefix() != null) name = attribute.getPrefix() + ":" + name; set.add(path + "/@" + name); } final NodeList nodes = doc.getChildNodes(); for (int i = 0; i < nodes.getLength(); i++) { final Node node = nodes.item(i); if (node instanceof Element) { final Set<String> children = getTree(node); for (final String child : children) { set.add(path + "/" + child); } } } return set; }
From source file:Main.java
public static StringBuilder append(Node n, StringBuilder buf, int level) { if (n instanceof CharacterData) return _level(buf, level).append(n.getNodeValue()).append("\n"); _level(buf, level).append("<").append(n.getNodeName()); NamedNodeMap attr = n.getAttributes(); if (attr != null) { for (int i = 0; i < attr.getLength(); i++) { Node a = attr.item(i); buf.append(" ").append(a.getNodeName()).append("=\"").append(a.getNodeValue()).append("\" "); }//from w w w .jav a 2s.com } NodeList children = n.getChildNodes(); if (children == null || children.getLength() == 0) return buf.append("/>\n"); buf.append(">\n"); for (int i = 0; i < children.getLength(); i++) { Node c = children.item(i); append(c, buf, level + 1); } return _level(buf, level).append("</").append(n.getNodeName()).append(">\n"); }
From source file:Main.java
/** * recursively transform the prefix and the namespace of a node where getNamespaceURI is NULL * @param node the node to transform/*from w w w.j av a 2 s . c o m*/ * @param prefix the new prefix * @param namespaceuri the new namespace uri * @return the new node with NS and prefix changed */ static public Node setPrefixNamespace(Node node, String prefix, String namespaceuri) { Node dest = null; if (node.getNodeType() == Node.ELEMENT_NODE) { Element e = (Element) node; if (e.getNamespaceURI() == null) { Element e2 = node.getOwnerDocument().createElementNS(namespaceuri, (prefix != null ? prefix + ':' + e.getLocalName() : e.getLocalName())); NamedNodeMap nodes = e.getAttributes(); for (int i = 0; i < nodes.getLength(); ++i) { Attr att = (Attr) (node.getOwnerDocument().importNode(nodes.item(i), true)); e2.setAttributeNode(att); } dest = e2; } else { dest = node.getOwnerDocument().importNode(node, false); } } else { dest = node.getOwnerDocument().importNode(node, false); } for (Node c = node.getFirstChild(); c != null; c = c.getNextSibling()) { dest.appendChild(setPrefixNamespace(c, prefix, namespaceuri)); } return dest; }
From source file:DOMEdit.java
private static void outputElement(Element node, String indent) { System.out.print(indent + "<" + node.getTagName()); NamedNodeMap nm = node.getAttributes(); for (int i = 0; i < nm.getLength(); i++) { Attr attr = (Attr) nm.item(i); System.out.print(" " + attr.getName() + "=\"" + attr.getValue() + "\""); }/*ww w. j a va2 s . c o m*/ System.out.println(">"); NodeList list = node.getChildNodes(); for (int i = 0; i < list.getLength(); i++) outputloop(list.item(i), indent + TAB); System.out.println(indent + "</" + node.getTagName() + ">"); }
From source file:Main.java
/** Helper method - Converts an XML <I>Element</I> to a <I>String</I> object. @param pElement An <I>org.w3c.dom.Element</I> object to be serialized to a <I>String</I>. @return <I>String</I> representation of the <I>Element</I>. *///from w w w . j av a 2 s . c o m public static String convertElementToString(Element pElement) { // Local variables. int nCount = 0; // Start the element. String strName = pElement.getNodeName(); String strReturn = "<" + strName; // Get the list of attributes. NamedNodeMap pNodeMap = pElement.getAttributes(); nCount = pNodeMap.getLength(); // Build the attributes. for (int i = 0; i < nCount; i++) { Attr pAttr = (Attr) pNodeMap.item(i); strReturn += " " + pAttr.getNodeName() + "=\"" + pAttr.getNodeValue() + "\""; } // Get the list of children. NodeList pChildren = pElement.getChildNodes(); nCount = pChildren.getLength(); // If no children exist, return a single node. if (0 == nCount) return strReturn + " />"; // Close node. strReturn += ">"; // Build out the children nodes. for (int i = 0; i < nCount; i++) { Node pChild = pChildren.item(i); if (pChild instanceof CharacterData) strReturn += ((CharacterData) pChild).getData(); else if (pChild instanceof Element) strReturn += convertElementToString((Element) pChild); } return strReturn + "</" + strName + ">"; }