List of usage examples for org.w3c.dom NamedNodeMap getLength
public int getLength();
From source file:XmlUtil.java
/** * Returns attribute value of a node or <code>null</code> if attribute name not found. * Specified attribute is searched on every call. * Consider {@link #getAllAttributes(org.w3c.dom.Node)} for better performances. *//*from w w w .j av a 2s .c o m*/ public static String getAttributeValue(Node node, String attrName) { NamedNodeMap nmm = node.getAttributes(); for (int j = 0; j < nmm.getLength(); j++) { Node attribute = nmm.item(j); if (attribute.getNodeType() != Node.ATTRIBUTE_NODE) { continue; } String nodeName = attribute.getNodeName(); if (nodeName.equals(attrName)) { return attribute.getNodeValue(); } } return null; }
From source file:Main.java
@SuppressWarnings("null") public static void copyInto(Node src, Node dest) throws DOMException { Document factory = dest.getOwnerDocument(); //Node start = src; Node parent = null;/*from w w w. j a va 2s .c om*/ Node place = src; // traverse source tree while (place != null) { // copy this node Node node = null; int type = place.getNodeType(); switch (type) { case Node.CDATA_SECTION_NODE: { node = factory.createCDATASection(place.getNodeValue()); break; } case Node.COMMENT_NODE: { node = factory.createComment(place.getNodeValue()); break; } case Node.ELEMENT_NODE: { Element element = factory.createElement(place.getNodeName()); node = element; NamedNodeMap attrs = place.getAttributes(); int attrCount = attrs.getLength(); for (int i = 0; i < attrCount; i++) { Attr attr = (Attr) attrs.item(i); String attrName = attr.getNodeName(); String attrValue = attr.getNodeValue(); element.setAttribute(attrName, attrValue); /* if (domimpl && !attr.getSpecified()) { ((Attr) element.getAttributeNode(attrName)).setSpecified(false); } */ } break; } case Node.ENTITY_REFERENCE_NODE: { node = factory.createEntityReference(place.getNodeName()); break; } case Node.PROCESSING_INSTRUCTION_NODE: { node = factory.createProcessingInstruction(place.getNodeName(), place.getNodeValue()); break; } case Node.TEXT_NODE: { node = factory.createTextNode(place.getNodeValue()); break; } default: { throw new IllegalArgumentException( "can't copy node type, " + type + " (" + node.getNodeName() + ')'); } } dest.appendChild(node); // iterate over children if (place.hasChildNodes()) { parent = place; place = place.getFirstChild(); dest = node; } else if (parent == null) { place = null; } else { // advance place = place.getNextSibling(); while (place == null && parent != null && dest != null) { place = parent.getNextSibling(); parent = parent.getParentNode(); dest = dest.getParentNode(); } } } }
From source file:Main.java
public static String logElement(final Element elem, final int level) { String estr = ""; String indentText = " "; String addIndT = ""; // add indent int ind = 0;/*from w w w .j ava2 s .c o m*/ while (ind < level) { addIndT = addIndT + indentText; ind++; } String name = elem.getNodeName(); estr = "\n" + addIndT + "<" + name + " "; // Attribs NamedNodeMap namedNodeMap = elem.getAttributes(); StringBuilder sb = new StringBuilder(estr); for (int i = 0; i < namedNodeMap.getLength(); i++) { Attr att = (Attr) namedNodeMap.item(i); sb.append(" " + estr + att.getName() + "=\"" + att.getNodeValue() + "\" "); } sb.append(">"); estr = sb.toString(); NodeList pl = elem.getChildNodes(); int index = pl.getLength(); // text nodes if (index > 0) { int i = 0; while (i < index) { Node domNode = pl.item(i); if ((domNode.getNodeType()) == org.w3c.dom.Node.TEXT_NODE) { String Etext = domNode.getNodeValue(); estr = estr + "\n " + addIndT + addIndT + Etext; } i++; } } // Child Elements if (index > 0) { int i = 0; while (i < index) { Node domNode = pl.item(i); if ((domNode.getNodeType()) == org.w3c.dom.Node.ELEMENT_NODE) { Element el = (Element) domNode; estr = estr + logElement(el, level + 1); } i++; } } estr = estr + "\n" + addIndT + "</" + name + ">"; return estr; }
From source file:Main.java
/** * Whenever you'd need to print a configuration node and/or its children. * * @param root the root node to print.// www . j a v a 2s. com * @param out the print stream that should be used to outpu * @param recurse boolean * @param prefix String */ public static void printChildElements(Element root, PrintStream out, boolean recurse, String prefix) { out.print(prefix + "<" + root.getNodeName()); NamedNodeMap attrs = root.getAttributes(); Node node; for (int i = 0; i < attrs.getLength(); i++) { node = attrs.item(i); out.print(" " + node.getNodeName() + "=\"" + node.getNodeValue() + "\""); } out.println(">"); String data = getText(root); if (data != null && data.trim().length() > 0) out.println(prefix + "\t" + data); data = getCData(root); if (data != null && data.trim().length() > 0) out.println(prefix + "\t<![CDATA[" + data + "]]>"); NodeList nodes = root.getChildNodes(); for (int i = 0; i < nodes.getLength(); i++) { node = nodes.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { if (recurse) printChildElements((Element) node, out, recurse, prefix + "\t"); else out.println(prefix + node.getNodeName()); } } out.println(prefix + "</" + root.getNodeName() + ">"); }
From source file:Main.java
public static String logElement(Element El, int level) { String Es = ""; String indentText = " "; String addIndT = ""; // add indent int ind = 0;//from w ww. j a va 2 s . c om while (ind < level) { addIndT = addIndT + indentText; ind++; } String name = El.getNodeName(); Es = "\n" + addIndT + "<" + name + " "; // Attribs NamedNodeMap namedNodeMap = El.getAttributes(); StringBuilder sb = new StringBuilder(Es); for (int i = 0; i < namedNodeMap.getLength(); i++) { Attr att = (Attr) namedNodeMap.item(i); sb.append(" " + Es + att.getName() + "=\"" + att.getNodeValue() + "\" "); // Es = " " + Es + att.getName() + "=\"" + att.getNodeValue() + // "\" "; } sb.append(">"); // Es = Es + ">"; Es = sb.toString(); NodeList pl = El.getChildNodes(); int index = pl.getLength(); // text nodes if (index > 0) { int i = 0; while (i < index) { Node DomNode = pl.item(i); if ((DomNode.getNodeType()) == org.w3c.dom.Node.TEXT_NODE) { String Etext = DomNode.getNodeValue(); Es = Es + "\n " + addIndT + addIndT + Etext; } i++; } } // Child Elements if (index > 0) { level++; int i = 0; while (i < index) { Node DomNode = pl.item(i); if ((DomNode.getNodeType()) == org.w3c.dom.Node.ELEMENT_NODE) { Element el = (Element) DomNode; Es = Es + logElement(el, level); } i++; } } Es = Es + "\n" + addIndT + "</" + name + ">"; return Es; }
From source file:Main.java
public static List<Node> listize(final NamedNodeMap nnm) { return new AbstractList<Node>() { public Node get(int i) { return nnm.item(i); }//from ww w .j av a 2 s.com public int size() { return nnm.getLength(); } }; }
From source file:Main.java
public static void copyInto(Node src, Node dest) throws DOMException { Document factory = dest.getOwnerDocument(); //Node start = src; Node parent = null;// w w w . j av a2 s. c o m Node place = src; // traverse source tree while (place != null) { // copy this node Node node = null; int type = place.getNodeType(); switch (type) { case Node.CDATA_SECTION_NODE: { node = factory.createCDATASection(place.getNodeValue()); break; } case Node.COMMENT_NODE: { node = factory.createComment(place.getNodeValue()); break; } case Node.ELEMENT_NODE: { Element element = factory.createElement(place.getNodeName()); node = element; NamedNodeMap attrs = place.getAttributes(); int attrCount = attrs.getLength(); for (int i = 0; i < attrCount; i++) { Attr attr = (Attr) attrs.item(i); String attrName = attr.getNodeName(); String attrValue = attr.getNodeValue(); element.setAttribute(attrName, attrValue); } break; } case Node.ENTITY_REFERENCE_NODE: { node = factory.createEntityReference(place.getNodeName()); break; } case Node.PROCESSING_INSTRUCTION_NODE: { node = factory.createProcessingInstruction(place.getNodeName(), place.getNodeValue()); break; } case Node.TEXT_NODE: { node = factory.createTextNode(place.getNodeValue()); break; } default: { throw new IllegalArgumentException( "can't copy node type, " + type + " (" + place.getNodeName() + ')'); } } dest.appendChild(node); // iterate over children if (place.hasChildNodes()) { parent = place; place = place.getFirstChild(); dest = node; } else if (parent == null) { place = null; } else { // advance place = place.getNextSibling(); while (place == null && parent != null) { place = parent.getNextSibling(); parent = parent.getParentNode(); dest = dest.getParentNode(); } } } }
From source file:Main.java
/** * Copy elements from one document to another attaching at the specified * element and translating the namespace. * * @param from copy the children of this element (exclusive) * @param to where to attach the copied elements * @param newNamespace destination namespace * * @since 8.4/*w w w .j a v a 2 s .c o m*/ */ public static void copyDocument(Element from, Element to, String newNamespace) { Document doc = to.getOwnerDocument(); NodeList nl = from.getChildNodes(); int length = nl.getLength(); for (int i = 0; i < length; i++) { Node node = nl.item(i); Node newNode = null; if (Node.ELEMENT_NODE == node.getNodeType()) { Element oldElement = (Element) node; newNode = doc.createElementNS(newNamespace, oldElement.getTagName()); NamedNodeMap m = oldElement.getAttributes(); Element newElement = (Element) newNode; for (int index = 0; index < m.getLength(); index++) { Node attr = m.item(index); newElement.setAttribute(attr.getNodeName(), attr.getNodeValue()); } copyDocument(oldElement, newElement, newNamespace); } else { newNode = node.cloneNode(true); newNode = to.getOwnerDocument().importNode(newNode, true); } if (newNode != null) { to.appendChild(newNode); } } }
From source file:Main.java
/** Prints the specified node, then prints all of its children. */ public static void printDOM(Node node) { int type = node.getNodeType(); switch (type) { // print the document element case Node.DOCUMENT_NODE: { System.out.print("<?xml version=\"1.0\" ?>"); printDOM(((Document) node).getDocumentElement()); break;// w w w . j a v a 2s . c o m } // print element with attributes case Node.ELEMENT_NODE: { System.out.println(); System.out.print("<"); System.out.print(node.getNodeName()); NamedNodeMap attrs = node.getAttributes(); for (int i = 0; i < attrs.getLength(); i++) { Node attr = attrs.item(i); System.out.print(" " + attr.getNodeName().trim() + "=\"" + attr.getNodeValue().trim() + "\""); } System.out.print(">"); NodeList children = node.getChildNodes(); if (children != null) { int len = children.getLength(); for (int i = 0; i < len; i++) printDOM(children.item(i)); } break; } // handle entity reference nodes case Node.ENTITY_REFERENCE_NODE: { System.out.print("&"); System.out.print(node.getNodeName().trim()); System.out.print(";"); break; } // print cdata sections case Node.CDATA_SECTION_NODE: { System.out.print("<![CDATA["); System.out.print(node.getNodeValue().trim()); System.out.print("]]>"); break; } // print text case Node.TEXT_NODE: { System.out.println(); System.out.print(node.getNodeValue().trim()); break; } // print processing instruction case Node.PROCESSING_INSTRUCTION_NODE: { System.out.print("<?"); System.out.print(node.getNodeName().trim()); String data = node.getNodeValue().trim(); { System.out.print(" "); System.out.print(data); } System.out.print("?>"); break; } } if (type == Node.ELEMENT_NODE) { System.out.println(); System.out.print("</"); System.out.print(node.getNodeName().trim()); System.out.print('>'); } }
From source file:Main.java
/** * Searches throgh the passed NamedNodeMap for an attribute and returns it if it is found. * If it is not found, returns a null.//from ww w .j ava2s . c o m * @param nnm NamedNodeMap * @param name String * @return String */ public static String getAttributeValueByName(NamedNodeMap nnm, String name) { if (nnm == null) return null; for (int i = 0; i < nnm.getLength(); i++) { Attr attr = (Attr) nnm.item(i); if (attr.getName().equalsIgnoreCase(name)) { return attr.getValue(); } } return null; }