List of usage examples for org.w3c.dom Attr getName
public String getName();
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 www. jav a2s . c om*/ * @param nnm NamedNodeMap * @param name String * @return String */ public static String getAttributeValueByName(NamedNodeMap nnm, String name) { for (int i = 0; i < nnm.getLength(); i++) { Attr attr = (Attr) nnm.item(i); if (attr.getName().equalsIgnoreCase(name)) { return attr.getValue(); } } return null; }
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;// w w w .j av a2 s . co 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
public static void collectNamespaces(Node node, Map namespaces, Map prefixes) { if (node == null) { throw new IllegalArgumentException("nullArgument: node"); // i18n.getMessage("nullArgument", "node")); }//from ww w . j a v a 2 s . com if (namespaces == null) { throw new IllegalArgumentException("nullArgument: namespace"); // i18n.getMessage("nullArgument", "namespaces")); } if (prefixes == null) { prefixes = new HashMap(); } NamedNodeMap attributes = node.getAttributes(); if (attributes != null) { for (int i = 0; i < attributes.getLength(); i++) { Attr attr = (Attr) attributes.item(i); String name = attr.getName(); String value = attr.getValue(); if (name.startsWith("xmlns:")) { if (namespaces.get(value) == null) { // ns not defined if (prefixes.get(name) != null) { // find unique prefix int j = 1; do { name = "xmlns:ns" + j++; } while (prefixes.get(name) != null); } prefixes.put(name, value); namespaces.put(value, name); } } } } NodeList children = node.getChildNodes(); if (children != null) { for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); if (child.getNodeType() == Node.ELEMENT_NODE) { collectNamespaces(child, namespaces, prefixes); } } } }
From source file:Main.java
public static String logElement(Element El, int level) { String Es = ""; String indentText = " "; String addIndT = ""; // add indent int ind = 0;/*w w w. j av a 2 s.c o m*/ 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:DOMCopy.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() + "\""); }/*w w w . j a va 2s . 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:MainClass.java
static void listNodes(Node node, String indent) { String nodeName = node.getNodeName(); System.out.println(indent + " Node: " + nodeName); short type = node.getNodeType(); System.out.println(indent + " Node Type: " + nodeType(type)); if (type == TEXT_NODE) { System.out.println(indent + " Content is: " + ((Text) node).getWholeText()); } else if (node.hasAttributes()) { System.out.println(indent + " Element Attributes are:"); NamedNodeMap attrs = node.getAttributes(); for (int i = 0; i < attrs.getLength(); i++) { Attr attribute = (Attr) attrs.item(i); System.out.println(indent + " " + attribute.getName() + " = " + attribute.getValue()); }/*from w w w . j a v a 2 s . com*/ } NodeList list = node.getChildNodes(); if (list.getLength() > 0) { System.out.println(indent + " Child Nodes of " + nodeName + " are:"); for (int i = 0; i < list.getLength(); i++) { listNodes(list.item(i), indent + " "); } } }
From source file:Main.java
/** Takes XML node and prints to String. * * @param node Element to print to String * @return XML node as String//from www .j a va2 s . co m */ private static void printNode(StringBuffer sBuffer, Node node) { if (node.getNodeType() == Node.TEXT_NODE) { sBuffer.append(encodeXMLText(node.getNodeValue().trim())); } else if (node.getNodeType() == Node.CDATA_SECTION_NODE) { sBuffer.append("<![CDATA["); sBuffer.append(node.getNodeValue()); sBuffer.append("]]>"); } else if (node.getNodeType() == Node.ELEMENT_NODE) { Element el = (Element) node; sBuffer.append("<").append(el.getTagName()); NamedNodeMap attribs = el.getAttributes(); for (int i = 0; i < attribs.getLength(); i++) { Attr nextAtt = (Attr) attribs.item(i); sBuffer.append(" ").append(nextAtt.getName()).append("=\"").append(nextAtt.getValue()).append("\""); } NodeList nodes = node.getChildNodes(); if (nodes.getLength() == 0) { sBuffer.append("/>"); } else { sBuffer.append(">"); for (int i = 0; i < nodes.getLength(); i++) { printNode(sBuffer, nodes.item(i)); } sBuffer.append("</").append(el.getTagName()).append(">"); } } }
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 w w w . j av a 2s. 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; }
From source file:Main.java
/** * Searches throgh the passed NamedNodeMap for an attribute. If it is found, it will try to convert it to a boolean. * @param nnm NamedNodeMap/*from ww w . ja v a 2s . c o m*/ * @param name String * @throws RuntimeException on any failure to parse a boolean * @return boolean */ public static boolean getAttributeBooleanByName(NamedNodeMap nnm, String name) throws RuntimeException { for (int i = 0; i < nnm.getLength(); i++) { Attr attr = (Attr) nnm.item(i); if (attr.getName().equalsIgnoreCase(name)) { String tmp = attr.getValue().toLowerCase(); if (tmp.equalsIgnoreCase("true")) return true; if (tmp.equalsIgnoreCase("false")) return false; throw new RuntimeException("Attribute " + name + " value not boolean:" + tmp); } } throw new RuntimeException("Attribute " + name + " not found."); }
From source file:Main.java
protected static void writeXMLwalkTree(Node node, int indent, PrintWriter out) { if (node == null) throw new NullPointerException("Null node passed to writeXMLwalkTree()"); if (node.hasChildNodes()) { if (node instanceof Element) { Element elem = (Element) node; //elem.normalize(); out.print("\n"); for (int j = 0; j < indent; j++) { out.print(" "); }/*from ww w . j a v a 2 s . com*/ out.print("<" + elem.getTagName()); NamedNodeMap attrs = elem.getAttributes(); for (int i = 0; i < attrs.getLength(); i++) { Attr a = (Attr) attrs.item(i); out.print(" " + a.getName() + "=\"" + a.getValue() + "\""); } out.print(">"); NodeList nl = node.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { writeXMLwalkTree(nl.item(i), indent + 2, out); } // for(int j=0;j<indent;j++) { // out.print(" "); // } out.println("</" + elem.getTagName() + ">"); } } else { if (node instanceof Element) { Element elem = (Element) node; out.print("\n"); for (int j = 0; j < indent; j++) { out.print(" "); } out.print("<" + elem.getTagName()); NamedNodeMap attrs = elem.getAttributes(); for (int i = 0; i < attrs.getLength(); i++) { Attr a = (Attr) attrs.item(i); out.print(" " + a.getName() + "=\"" + a.getValue() + "\""); } out.println("/>"); } else if (node instanceof CDATASection) { CDATASection cdata = (CDATASection) node; // for(int j=0;j<indent;j++) { // out.print(" "); // } out.print("<![CDATA[" + cdata.getData() + "]]>"); } else if (node instanceof Text) { Text text = (Text) node; StringBuilder buf = new StringBuilder(text.getData().length()); for (int i = 0; i < text.getData().length(); i++) { if (text.getData().charAt(i) == '\n' || text.getData().charAt(i) == '\r' || text.getData().charAt(i) == ' ' || text.getData().charAt(i) == '\t') { if (buf.length() > 0 && buf.charAt(buf.length() - 1) != ' ') { buf.append(' '); } } else { buf.append(text.getData().charAt(i)); } } if (buf.length() > 0 && !buf.toString().equals(" ")) { StringBuilder buf2 = new StringBuilder(buf.length() + indent); // for(int j=0;j<indent;j++) { // buf2.append(' '); // } buf2.append(buf.toString()); out.print(buf2); } } } }