List of usage examples for org.w3c.dom Attr getNodeName
public String getNodeName();
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true);// w w w.j av a2 s . c om factory.setExpandEntityReferences(false); Document doc = factory.newDocumentBuilder().parse(new File("filename")); Element element = doc.getElementById("key1"); NamedNodeMap attrs = element.getAttributes(); int numAttrs = attrs.getLength(); for (int i = 0; i < numAttrs; i++) { Attr attr = (Attr) attrs.item(i); String attrName = attr.getNodeName(); String attrValue = attr.getNodeValue(); } }
From source file:Main.java
License:asdf
public static void dom(Element element) { NamedNodeMap attrs = element.getAttributes(); int numAttrs = attrs.getLength(); for (int i = 0; i < numAttrs; i++) { Attr attr = (Attr) attrs.item(i); String attrName = attr.getNodeName(); System.out.println(attrName); String attrValue = attr.getNodeValue(); System.out.println(attrValue); }/* w w w.j av a2 s.co m*/ }
From source file:Main.java
public static List<String> getAllAttrValueByName(Node item, String name) { List<String> lst = null; NamedNodeMap attributes = item.getAttributes(); int numAttrs = attributes.getLength(); for (int i = 0; i < numAttrs; i++) { Attr attr = (Attr) attributes.item(i); String attrName = attr.getNodeName(); if (name.equals(attrName)) { if (lst == null) { lst = new ArrayList(); }/*from ww w . j av a 2 s. c om*/ lst.add(attr.getNodeValue()); } } return lst; }
From source file:Main.java
public static String getAttrvalue(Node item, String name, boolean ignoreNs) { NamedNodeMap attributes = item.getAttributes(); int numAttrs = attributes.getLength(); for (int i = 0; i < numAttrs; i++) { Attr attr = (Attr) attributes.item(i); String attrName = attr.getNodeName(); String NSName = attr.getNamespaceURI(); if (ignoreNs) { if ((attrName.indexOf(":" + name) != -1) || (name.equals(attrName))) return attr.getNodeValue(); } else {// w ww . ja v a 2s . c o m if (name.equals(attrName)) { return attr.getNodeValue(); } } } return null; }
From source file:Main.java
public static Map<String, String> getAttributes(Node n) { if (n == null) return null; NamedNodeMap attributes = n.getAttributes(); if (attributes == null || attributes.getLength() <= 0) return null; Map<String, String> result = new HashMap<>(); for (int i = 0; i < attributes.getLength(); i++) { Attr attr = (Attr) attributes.item(i); result.put(attr.getNodeName(), attr.getNodeValue()); }/*w ww.j ava 2 s. com*/ return result; }
From source file:Main.java
/** * Copy the attributes from n2 to n1./*from w ww . j a v a 2 s . co m*/ * * @param n1 The source of the attributes. * @param n2 What to copy into. */ public static void mergeAttributes(Element n1, Element n2) { if ((n1 == null) || (n2 == null)) { return; } NamedNodeMap nnm = n2.getAttributes(); if (nnm == null) { return; } for (int i = 0; i < nnm.getLength(); i++) { Attr attr = (Attr) nnm.item(i); n1.setAttribute(attr.getNodeName(), attr.getNodeValue()); } }
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 2s . c om 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 + ">"; }
From source file:Main.java
private static Node convertFromNamespaceForm(final Node node) { if (node instanceof Element) { final Document document = node.getOwnerDocument(); final Element newElement = document.createElementNS(null, node.getLocalName()); final NodeList children = node.getChildNodes(); for (int i = 0, n = children.getLength(); i < n; i++) { final Node oldChildNode = children.item(i); final Node newChildNode = convertFromNamespaceForm(oldChildNode); newElement.appendChild(newChildNode); }//from w w w . j a va2 s.co m final NamedNodeMap attributes = node.getAttributes(); for (int i = 0, n = attributes.getLength(); i < n; i++) { final Attr attr = (Attr) attributes.item(i); final String attrQualifiedName = attr.getNodeName(); final String attrLocalName = attr.getLocalName(); if (!attrQualifiedName.equals(XMLNS) && !attrQualifiedName.startsWith(XMLNS_COLON) && !attrLocalName.equals(XSI_SCHEMA_LOCATION_ATTR)) { newElement.setAttributeNS(null, attrLocalName, attr.getValue()); } } return newElement; } else { return node.cloneNode(true); } }
From source file:Main.java
private static void convertAttribute(Attr toCopy, Element saveTo, Document doc) { if (toCopy.getNamespaceURI() == null || !toCopy.getNamespaceURI().equals(xmlnsURI)) { saveTo.setAttributeNS(toCopy.getNamespaceURI(), toCopy.getNodeName(), toCopy.getValue()); }/*ww w. j a v a 2s . co m*/ }
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;// ww w.j av a 2 s .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(); } } } }