List of usage examples for org.w3c.dom Attr getName
public String getName();
From source file:Main.java
public static Map<String, String> getAttrList(Node node) { Map<String, String> map = new HashMap<String, String>(); NamedNodeMap list = node.getAttributes(); if (list != null) for (int i = 0; i < list.getLength(); i++) { Attr attr = (Attr) list.item(i); map.put(attr.getName(), attr.getValue()); }//from ww w.java2 s . c om return map; }
From source file:Main.java
static void printElement(Element element, String indent) { System.out.println("Element '" + element.getNodeName() + "'"); NodeList children = element.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); switch (child.getNodeType()) { case Node.ELEMENT_NODE: printElement((Element) child, indent + "\t"); break; case Node.ATTRIBUTE_NODE: Attr attr = (Attr) child; System.out.println("\tAttribute: '" + attr.getName() + "' = '" + attr.getValue() + "'"); break; case Node.COMMENT_NODE: Comment comment = (Comment) child; System.out.println("\tComment: '" + comment.getData() + "'"); break; case Node.CDATA_SECTION_NODE: CharacterData cdata = (CharacterData) child; System.out.println("\tCDatat: '" + cdata.getData() + "'"); break; case Node.TEXT_NODE: Text text = (Text) child; System.out.println("\tText: '" + text.getData() + "'"); break; default://from w ww . j av a 2 s.com System.out.println("\tUnknown node type: '" + child.getNodeType() + "'"); break; } } }
From source file:Main.java
/** * Returns a map of the passed node's attributes * @param node The nopde to get an attribute map for * @return a [possibly empty] map of the node's attributes */// w w w. j ava 2 s. c o m public static Map<String, String> getAttributeMap(final Node node) { if (node == null) throw new IllegalArgumentException("The passed node was null"); final NamedNodeMap nnm = node.getAttributes(); final int size = nnm.getLength(); if (size == 0) return Collections.emptyMap(); final Map<String, String> map = new LinkedHashMap<String, String>(size); for (int i = 0; i < size; i++) { final Attr attr = (Attr) nnm.item(i); map.put(attr.getName(), attr.getValue()); } return map; }
From source file:Main.java
protected static Map<String, String> createAttributeTable(Element e) { Map<String, String> attributeTable = new HashMap<>(); NamedNodeMap attrs = e.getAttributes(); if (attrs != null) { int numAttrs = attrs.getLength(); for (int i = 0; i < numAttrs; i++) { Node na = attrs.item(i); if (na.getNodeType() != Node.ATTRIBUTE_NODE) { continue; }//from w w w . jav a 2 s . com Attr a = (Attr) na; attributeTable.put(a.getName(), a.getValue()); } } return attributeTable; }
From source file:Main.java
public static String getPrefixNS(String uri, Node e) { while (e != null && e.getNodeType() == Node.ELEMENT_NODE) { NamedNodeMap attrs = e.getAttributes(); for (int n = 0; n < attrs.getLength(); n++) { Attr a = (Attr) attrs.item(n); String name = a.getName(); if (name.startsWith("xmlns:") && a.getNodeValue().equals(uri)) { return name.substring("xmlns:".length()); }//from www . j a va 2 s . c o m } e = e.getParentNode(); } return null; }
From source file:Main.java
public static String attributeName(Attr attribute) { String name = attribute.getLocalName(); if (null == name) { name = attribute.getName(); }//from w w w . j av a2 s . c o m return name; }
From source file:Main.java
@Nullable public static Map<String, String> getAllAttributesAsMap(@Nullable final Element aSrcNode) { if (aSrcNode != null) { final NamedNodeMap aNNM = aSrcNode.getAttributes(); if (aNNM != null) { final Map<String, String> aMap = new LinkedHashMap<String, String>(aNNM.getLength()); final int nMax = aNNM.getLength(); for (int i = 0; i < nMax; ++i) { final Attr aAttr = (Attr) aNNM.item(i); aMap.put(aAttr.getName(), aAttr.getValue()); }/* w w w. j a v a 2s . co m*/ return aMap; } } return null; }
From source file:Main.java
public static String nodeToString(Node node) { String strNode = ""; Node nodeIt = node;/*w ww. j a va2 s.co m*/ if (nodeIt instanceof Element) { Element elem = (Element) nodeIt; String strElem = ""; strElem += "<" + elem.getTagName(); NamedNodeMap attribs = elem.getAttributes(); int len = attribs.getLength(); for (int i = 0; i < len; i++) { Attr attr = (Attr) attribs.item(i); strElem += " " + attr.getName() + "=\"" + attr.getValue() + "\""; } strElem += ">"; strNode = strElem + strNode; } else if (nodeIt instanceof CharacterData) { CharacterData charNode = (CharacterData) nodeIt; strNode = charNode.getData() + strNode; } else if (nodeIt instanceof Document) strNode = strNode; // no hacemos nada else strNode = nodeIt.getNodeValue() + strNode; return strNode; }
From source file:Main.java
/** * Print all the attributes of the given node * * @param parent/*w w w . j a v a 2 s . c o m*/ */ public static void printNode(Node parent) { if (parent == null) { System.out.println("null node!"); return; } System.out.println(parent.getNodeName() + " node:"); NamedNodeMap attrs = parent.getAttributes(); for (int i = 0; i < attrs.getLength(); i++) { Attr attribute = (Attr) attrs.item(i); System.out.println(" " + attribute.getName() + " = " + attribute.getValue()); } }
From source file:Main.java
/** * @return A String[] of length two, [prefix, URI]. *///from w w w .j ava 2 s.c o m public static String[] getNamespaceDeclaration(Element ele, String prefixHint) { String[] ns = new String[2]; // prefix, URI NamedNodeMap attribs = ele.getAttributes(); for (int i = 0; i < attribs.getLength(); i++) { Attr attr = (Attr) attribs.item(i); if (attr.getName().startsWith("xmlns")) { if ((prefixHint != null && attr.getName().endsWith(prefixHint)) || attr.getName().equals("xmlns")) { ns[0] = attr.getLocalName(); // prefix ns[1] = attr.getTextContent(); // URI // catch default namespace change if (ns[0] == "xmlns") { ns[0] = UUID.randomUUID().toString(); } } } } if (ns[1] == null) { return getNamespaceDeclaration((Element) ele.getParentNode(), prefixHint); } else { return ns; } }