List of usage examples for org.w3c.dom Element getAttributes
public NamedNodeMap getAttributes();
NamedNodeMap
containing the attributes of this node (if it is an Element
) or null
otherwise. From source file:Main.java
public static String nodeToString(Object node) { String result = ""; if (node instanceof String) { String str = ((String) node).replaceAll("\\s+", " ").trim(); result += str;//w w w .ja v a 2 s. c o m } else if (node instanceof Text) { String str = ((Text) node).getTextContent().replaceAll("\\s+", " ").trim(); result += str; } else if (node instanceof Element) { Element en = (Element) node; result += "<" + en.getTagName(); for (int j = 0; j < en.getAttributes().getLength(); j++) { Attr attr = (Attr) en.getAttributes().item(j); //if (!(attr.getNamespaceURI() != null && attr.getNamespaceURI().equals("http://www.w3.org/2000/xmlns/") && (attr.getLocalName().equals("xmlns") || attr.getLocalName().equals("xsi")))) { result += " " + attr.getName() + "=\"" + attr.getValue() + "\""; //} } if (en.getChildNodes().getLength() == 0) { result += "/>"; } else { result += ">"; ArrayList<Object> children = new ArrayList<Object>(); for (int i = 0; i < en.getChildNodes().getLength(); i++) { children.add(en.getChildNodes().item(i)); } result += nodesToString(children); result += "</" + en.getTagName() + ">"; } } return result; }
From source file:Main.java
/** * This is an ugly hack, there must be a nicer way to do it. * Using getPrefix doesn't work because it stops us from getting * xmlns: attributes, which is what I'm using this for. Using * getNamespace doesn't seem to work on xmlns attributes either. * Trims the prefix on the map key.//from w ww . j ava2 s . c o m * @param element the element from which to retrieve the attributes. * @param prefix the prefix of the attributes to retrieve * @return a Map containing the attributes names and their values. */ public static Map getAttributesWithPrefix(Element element, String prefix) { Map result = new HashMap(); prefix += ":"; NamedNodeMap attributes = element.getAttributes(); if (attributes == null) return result; for (int i = 0; i != attributes.getLength(); i++) { Node attribute = attributes.item(i); if (attribute.getNodeName().startsWith(prefix)) { result.put(attribute.getNodeName().substring(prefix.length()), attribute.getNodeValue()); } } return result; }
From source file:Main.java
/** * Identify variables in attribute values and CDATA contents and replace * with their values as defined in the variableDefs map. Variables without * definitions are left alone.// www . ja v a 2 s .com * * @param node the node to do variable replacement in * @param variableDefs map from variable names to values. */ public static void replaceVariables(final Node node, Map<String, String> variableDefs) { switch (node.getNodeType()) { case Node.ELEMENT_NODE: final Element element = (Element) node; final NamedNodeMap atts = element.getAttributes(); for (int i = 0; i < atts.getLength(); i++) { final Attr attr = (Attr) atts.item(i); attr.setNodeValue(replaceVariablesInString(attr.getValue(), variableDefs)); } break; case Node.CDATA_SECTION_NODE: String content = node.getTextContent(); node.setNodeValue(replaceVariablesInString(content, variableDefs)); break; default: break; } // process children final NodeList children = node.getChildNodes(); for (int childIndex = 0; childIndex < children.getLength(); childIndex++) replaceVariables(children.item(childIndex), variableDefs); }
From source file:Main.java
/** * Append XPath [expr] like /a/b[1] or /a/b[@name='x'] * @param sb/*from w w w. j a v a 2 s .com*/ * @param n */ private static void appendElementQualifier(StringBuffer sb, Element n) { if (n.getParentNode() == n.getOwnerDocument()) { return; } if (n.getAttributes() != null && n.getAttributes().getNamedItem("name") != null) { sb.append("[@name='").append(n.getAttributes().getNamedItem("name").getNodeValue()).append("']"); } else if (getChildrenCount((Element) n.getParentNode(), n.getNamespaceURI(), n.getLocalName()) != 1) { sb.append("[").append(getPosition(n)).append("]"); } }
From source file:XMLUtils.java
/** * Returns the value of the attribute of the given element. * @param base the element from where to search. * @param name of the attribute to get.//from w ww. j av a2 s .c o m * @return the value of this element. */ public static String getAttributeValue(final Element base, final String name) { // get attribute of this element... NamedNodeMap mapAttributes = base.getAttributes(); Node node = mapAttributes.getNamedItem(name); if (node != null) { return node.getNodeValue(); } return null; }
From source file:Main.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() + "\""); }//from w w w. j a v a2 s . com System.out.print(">"); 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:Main.java
/** * Gets the ID attribute of a DOM element. * /* w w w.j a v a 2 s. c o m*/ * @param domElement the DOM element * * @return the ID attribute or null if there isn't one */ public static Attr getIdAttribute(Element domElement) { if (!domElement.hasAttributes()) { return null; } NamedNodeMap attributes = domElement.getAttributes(); Attr attribute; for (int i = 0; i < attributes.getLength(); i++) { attribute = (Attr) attributes.item(i); if (attribute.isId()) { return attribute; } } return null; }
From source file:Main.java
/** * Dumps a debug listing of the attributes of the given element to * System.out.//from www.j a va 2s . c om * * @param elem the element to dump the attributes of */ public static void dumpAttrs(Element elem) { System.out.println("Attributes of " + elem.getNodeName() + ", NS: " + elem.getNamespaceURI()); NamedNodeMap attrs = elem.getAttributes(); int len = attrs.getLength(); for (int i = 0; i < len; ++i) { Node attr = attrs.item(i); System.out.println(" Name: " + attr.getNodeName() + ", Value: " + attr.getNodeValue() + ", NS: " + attr.getNamespaceURI()); } }
From source file:Main.java
@SuppressWarnings("fallthrough") private static void getSetRec(final Node rootNode, final Set<Node> result, final Node exclude, final boolean com) { if (rootNode == exclude) { return;//from w w w .j a v a 2 s . co m } switch (rootNode.getNodeType()) { case Node.ELEMENT_NODE: result.add(rootNode); Element el = (Element) rootNode; if (el.hasAttributes()) { NamedNodeMap nl = el.getAttributes(); for (int i = 0; i < nl.getLength(); i++) { result.add(nl.item(i)); } } //no return keep working case Node.DOCUMENT_NODE: for (Node r = rootNode.getFirstChild(); r != null; r = r.getNextSibling()) { if (r.getNodeType() == Node.TEXT_NODE) { result.add(r); while (r != null && r.getNodeType() == Node.TEXT_NODE) { r = r.getNextSibling(); } if (r == null) { return; } } getSetRec(r, result, exclude, com); } return; case Node.COMMENT_NODE: if (com) { result.add(rootNode); } return; case Node.DOCUMENT_TYPE_NODE: return; default: result.add(rootNode); } }
From source file:Main.java
public static void stripDuplicateAttributes(Node node, Node parent) { // The output depends on the type of the node switch (node.getNodeType()) { case Node.DOCUMENT_NODE: { Document doc = (Document) node; Node child = doc.getFirstChild(); while (child != null) { stripDuplicateAttributes(child, node); child = child.getNextSibling(); }//from ww w . ja v a 2 s . c o m break; } case Node.ELEMENT_NODE: { Element elt = (Element) node; NamedNodeMap attrs = elt.getAttributes(); ArrayList nodesToRemove = new ArrayList(); int nodesToRemoveNum = 0; for (int i = 0; i < attrs.getLength(); i++) { final Node a = attrs.item(i); for (int j = 0; j < attrs.getLength(); j++) { final Node b = attrs.item(j); //if there are two attributes with same name if (i != j && (a.getNodeName().equals(b.getNodeName()))) { nodesToRemove.add(b); nodesToRemoveNum++; } } } for (int i = 0; i < nodesToRemoveNum; i++) { Attr nodeToDelete = (Attr) nodesToRemove.get(i); Element nodeToDeleteParent = (Element) node; // nodeToDelete.getParentNode(); nodeToDeleteParent.removeAttributeNode(nodeToDelete); } nodesToRemove.clear(); Node child = elt.getFirstChild(); while (child != null) { stripDuplicateAttributes(child, node); child = child.getNextSibling(); } break; } default: //do nothing break; } }