List of usage examples for org.w3c.dom NamedNodeMap getLength
public int getLength();
From source file:Main.java
public static List<Attr> attributes(final Element element) { final NamedNodeMap attributeMap = element.getAttributes(); if (attributeMap == null || attributeMap.getLength() == 0) { return null; }//from ww w.ja v a 2 s .c o m final List<Attr> attributes = new ArrayList<Attr>(); for (int i = 0; i < attributeMap.getLength(); i++) { attributes.add((Attr) attributeMap.item(i)); } return attributes; }
From source file:Utils.java
/** * Get all prefixes defined on this element for the specified namespace. * // w w w . j a v a 2s . co m * @param element * @param namespaceUri * @param prefixes */ public static void getPrefixes(Element element, String namespaceUri, List<String> prefixes) { NamedNodeMap atts = element.getAttributes(); for (int i = 0; i < atts.getLength(); i++) { Node node = atts.item(i); String name = node.getNodeName(); if (namespaceUri.equals(node.getNodeValue()) && (name != null && (XMLNAMESPACE.equals(name) || name.startsWith(XMLNAMESPACE + ":")))) { prefixes.add(node.getPrefix()); } } }
From source file:Main.java
private static void appendAttributes(Node node, StringBuffer sb) { if (node instanceof Element) { NamedNodeMap nodeMap = node.getAttributes(); for (int i = 0; i < nodeMap.getLength(); i++) { sb.append(' '); sb.append(nodeMap.item(i).getNodeName()); sb.append('='); sb.append('"'); sb.append(nodeMap.item(i).getNodeValue()); sb.append('"'); }//from w w w . j a v a 2 s . co m } }
From source file:Main.java
public static void printTree(Node doc) { if (doc == null) { System.out.println("Nothing to print!!"); return;/*from w w w . j a v a 2 s . c om*/ } try { System.out.println(doc.getNodeName() + " " + doc.getNodeValue()); NamedNodeMap cl = doc.getAttributes(); for (int i = 0; i < cl.getLength(); i++) { Node node = cl.item(i); System.out.println("\t" + node.getNodeName() + " ->" + node.getNodeValue()); } NodeList nl = doc.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node node = nl.item(i); printTree(node); } } catch (Throwable e) { System.out.println("Cannot print!! " + e.getMessage()); } }
From source file:Main.java
/** * @param n Node to examine// www .j a va 2 s. c o m * @param attr Attribute to look for * @param def Default value to return if attribute is not present * @return if the Node contains the named Attribute, the value, if not, the def parameter */ public static String getAttributeIgnoreCase(Node n, String attr, String def) { NamedNodeMap attrs = n.getAttributes(); if (attrs == null) return def; for (int i = 0; i < attrs.getLength(); i++) { Node ret = attrs.item(i); if (ret.getNodeName().equalsIgnoreCase(attr)) return ret.getNodeValue(); } return def; }
From source file:Main.java
/** * // w ww . j a v a 2 s. co m * @param node * @param classname * @return */ public static Object decode(Element node, String classname) { try { Class classObject = Class.forName(classname); Object object = classObject.newInstance(); NamedNodeMap attributes = node.getAttributes(); for (int i = 0; i < attributes.getLength(); i++) { Node child = attributes.item(i); String nodeName = child.getNodeName(); Field field = classObject.getField(nodeName); field.setAccessible(true); field.set(object, child.getNodeValue()); } return object; } catch (ClassNotFoundException e) { e.printStackTrace(); return null; } catch (InstantiationException e) { e.printStackTrace(); return null; } catch (IllegalAccessException e) { e.printStackTrace(); return null; } catch (SecurityException e) { e.printStackTrace(); return null; } catch (NoSuchFieldException e) { e.printStackTrace(); return null; } }
From source file:Main.java
public static String nodeToString(Node node) { String ret = "<null/>"; if (node != null) { ret = "<" + node.getNodeName() + " "; NamedNodeMap attrs = node.getAttributes(); if (attrs != null) { for (int i = 0; i < attrs.getLength(); i++) { Node attr = (Node) attrs.item(i); ret = ret + attr.getNodeName() + "='"; ret = ret + attr.getNodeValue() + "' "; }/*from w w w . j a v a2s. c om*/ } ret = ret + "/>"; } return ret; }
From source file:Main.java
public static Map<String, String> findAttributesValues(Node node) { Map<String, String> retval = new HashMap<String, String>(); NamedNodeMap attrs = node.getAttributes(); if (attrs != null) { for (int i = 0; i < attrs.getLength(); i++) { Node attr = attrs.item(i); String name = attr.getNodeName(); String value = attr.getNodeValue(); retval.put(name, value);/*from w ww . jav a2 s .c o m*/ } } return retval; }
From source file:Main.java
public static <T> T loadBean(Node node, Class<T> beanClass) throws IntrospectionException, InstantiationException, IllegalAccessException, IllegalArgumentException, DOMException, InvocationTargetException { T store = beanClass.newInstance();//w ww. j a v a 2 s . co m Map<String, PropertyDescriptor> properties = new HashMap<String, PropertyDescriptor>(); BeanInfo beanInfo = Introspector.getBeanInfo(beanClass); for (PropertyDescriptor property : beanInfo.getPropertyDescriptors()) { properties.put(property.getName(), property); } NamedNodeMap attributes = node.getAttributes(); for (int i = 0; i < attributes.getLength(); i++) { Node attribute = attributes.item(i); PropertyDescriptor property = properties.get(attribute.getNodeName()); if (property != null && property.getPropertyType().equals(String.class) && property.getWriteMethod() != null) { property.getWriteMethod().invoke(store, attribute.getNodeValue()); } } NodeList childNodes = node.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node child = childNodes.item(i); PropertyDescriptor property = properties.get(child.getNodeName()); if (property != null && property.getPropertyType().equals(String.class) && property.getWriteMethod() != null) { property.getWriteMethod().invoke(store, child.getTextContent()); } } return store; }
From source file:Main.java
public static List<Node> getChildren(Node node) { ArrayList<Node> children = new ArrayList<Node>(); NodeList nodeList = node.getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { children.add(nodeList.item(i));// w ww.j a va 2 s . c o m } NamedNodeMap attributes = node.getAttributes(); if (attributes != null) { for (int i = 0; i < attributes.getLength(); i++) { children.add(attributes.item(i)); } } return children; }