List of usage examples for org.w3c.dom Node 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 retrieveNodeAsString(Node node) { String nodeStr = new String("<"); nodeStr += node.getNodeName() + internal; if (node.hasAttributes()) { NamedNodeMap attrs = node.getAttributes(); // add the attrubite name-value pairs for (int i = 0; i < attrs.getLength(); i++) { Node a = attrs.item(i); nodeStr += a.getNodeName() + "=" + a.getNodeValue() + internal; }//from w ww .j av a2 s . c o m } if (node.hasChildNodes()) { nodeStr += ">\n"; NodeList ns = node.getChildNodes(); for (int i = 0; i < ns.getLength(); i++) { nodeStr += logXMLSubNode(ns.item(i), 1); } nodeStr += "<" + node.getNodeName() + "/>\n"; } else { nodeStr += "/>\n"; } return nodeStr; }
From source file:Main.java
public static Node findWithAttrubute(Node root, String name, String attributeName) { final NodeList list = root.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { final Node subnode = list.item(i); if (subnode.getNodeType() == Node.ELEMENT_NODE) { if (subnode.getNodeName().equals(name)) { if (subnode.getAttributes().getNamedItem(attributeName) != null) { return subnode; }//ww w .j a v a 2 s .com } } } return null; }
From source file:io.wcm.testing.mock.osgi.OsgiMetadataUtil.java
public static Map<String, Object> getProperties(Document document) { Map<String, Object> props = new HashMap<>(); if (document != null) { try {//from w w w. j a v a2 s. c om XPath xpath = XPATH_FACTORY.newXPath(); xpath.setNamespaceContext(NAMESPACE_CONTEXT); NodeList nodes = (NodeList) xpath.evaluate( "/components/component[1]/property[@name!='' and @value!='']", document, XPathConstants.NODESET); if (nodes != null) { for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); String name = node.getAttributes().getNamedItem("name").getNodeValue(); String value = node.getAttributes().getNamedItem("value").getNodeValue(); String type = null; Node typeAttribute = node.getAttributes().getNamedItem("type"); if (typeAttribute != null) { type = typeAttribute.getNodeValue(); } if (StringUtils.equals("Integer", type)) { props.put(name, Integer.parseInt(value)); } else { props.put(name, value); } } } } catch (XPathExpressionException ex) { throw new RuntimeException("Error evaluating XPath.", ex); } } return props; }
From source file:Main.java
/** * Builds an XPointer that refers to the given node. If a shorthand pointer * (using a schema-determined identifier) cannot be constructed, then a * scheme-based pointer is derived that indicates the absolute location path * of a node in a DOM document. The location is specified as a scheme-based * XPointer having two parts://w w w . jav a 2s . c o m * <ul> * <li>an xmlns() part that declares a namespace binding context;</li> * <li>an xpointer() part that includes an XPath expression using the * abbreviated '//' syntax for selecting a descendant node.</li> * </ul> * * @param node * A node in a DOM document. * @return A String containing either a shorthand or a scheme-based pointer * that refers to the node. * * @see <a href="http://www.w3.org/TR/xptr-framework/"target="_blank"> * XPointer Framework</a> * @see <a href="http://www.w3.org/TR/xptr-xmlns/" target="_blank">XPointer * xmlns() Scheme</a> * @see <a href="http://www.w3.org/TR/xptr-xpointer/" * target="_blank">XPointer xpointer() Scheme</a> */ public static String buildXPointer(Node node) { if (null == node) { return ""; } StringBuilder xpointer = new StringBuilder(); if (null != node.getAttributes() && null != node.getAttributes().getNamedItem("id")) { String id = node.getAttributes().getNamedItem("id").getNodeValue(); xpointer.append(node.getLocalName()).append("[@id='").append(id).append("']"); return xpointer.toString(); } String nsURI = node.getNamespaceURI(); String nsPrefix = node.getPrefix(); if (null == nsPrefix) nsPrefix = "tns"; // WARNING: Escaping rules are currently ignored. xpointer.append("xmlns(").append(nsPrefix).append("=").append(nsURI).append(")"); xpointer.append("xpointer(("); switch (node.getNodeType()) { case Node.ELEMENT_NODE: // Find the element in the list of all similarly named descendants // of the document root. NodeList elementsByName = node.getOwnerDocument().getElementsByTagNameNS(nsURI, node.getLocalName()); for (int i = 0; i < elementsByName.getLength(); i++) { if (elementsByName.item(i).isSameNode(node)) { xpointer.append("//"); xpointer.append(nsPrefix).append(':').append(node.getLocalName()).append(")[").append(i + 1) .append("])"); break; } } break; case Node.DOCUMENT_NODE: xpointer.append("/"); break; case Node.ATTRIBUTE_NODE: Attr attrNode = (Attr) node; xpointer = new StringBuilder(buildXPointer(attrNode.getOwnerElement())); xpointer.insert(xpointer.lastIndexOf(")"), "/@" + attrNode.getName()); break; default: xpointer.setLength(0); break; } return xpointer.toString(); }
From source file:io.wcm.testing.mock.osgi.OsgiMetadataUtil.java
public static Set<String> getServiceInterfaces(Document document) { Set<String> serviceInterfaces = new HashSet<>(); if (document != null) { try {//from ww w .j ava 2s .co m XPath xpath = XPATH_FACTORY.newXPath(); xpath.setNamespaceContext(NAMESPACE_CONTEXT); NodeList nodes = (NodeList) xpath.evaluate( "/components/component[1]/service/provide[@interface!='']", document, XPathConstants.NODESET); if (nodes != null) { for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); String serviceInterface = node.getAttributes().getNamedItem("interface").getNodeValue(); if (StringUtils.isNotBlank(serviceInterface)) { serviceInterfaces.add(serviceInterface); } } } } catch (XPathExpressionException ex) { throw new RuntimeException("Error evaluating XPath.", ex); } } return serviceInterfaces; }
From source file:Main.java
/** * Print all the attributes of the given node * * @param parent/*from w w w. jav a 2s.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
public static Object transformXmlNodesIntoMap(Node node) { Map<String, Object> nodeMap = new HashMap<String, Object>(); NodeList subNodes = node.getChildNodes(); NamedNodeMap nodeAttrs = node.getAttributes(); for (int nodeAttrIdx = 0; nodeAttrIdx < nodeAttrs.getLength(); nodeAttrIdx++) { Node attrNode = nodeAttrs.item(nodeAttrIdx); nodeMap.put("@" + attrNode.getNodeName(), attrNode.getTextContent()); }/*from w ww . j ava 2s . c o m*/ if (nodeAttrs.getLength() == 0) if (subNodes.getLength() == 0) return ""; else if (subNodes.getLength() == 1 && subNodes.item(0).getNodeType() == Node.TEXT_NODE) return subNodes.item(0).getTextContent(); for (int subNodeIdx = 0; subNodeIdx < subNodes.getLength(); subNodeIdx++) { Node subNode = subNodes.item(subNodeIdx); if (subNode.getNodeType() == Node.TEXT_NODE) { nodeMap.put(subNode.getNodeName(), subNode.getTextContent()); } else { if (nodeMap.containsKey(subNode.getNodeName())) { Object subObject = nodeMap.get(subNode.getNodeName()); if (subObject instanceof List<?>) { ((List<Object>) subObject).add(transformXmlNodesIntoMap(subNode)); } else { List<Object> subObjectList = new ArrayList<Object>(); subObjectList.add(subObject); subObjectList.add(transformXmlNodesIntoMap(subNode)); nodeMap.put(subNode.getNodeName(), subObjectList); } } else { nodeMap.put(subNode.getNodeName(), transformXmlNodesIntoMap(subNode)); } } } return nodeMap; }
From source file:eu.planets_project.services.utils.cli.CliMigrationPaths.java
private static URI decodeURI(Node uri) throws URISyntaxException { NamedNodeMap attrs = uri.getAttributes(); Node item = attrs.getNamedItem("value"); String urivalue = item.getNodeValue(); return new URI(urivalue); }
From source file:jp.go.nict.langrid.client.soap.io.SoapResponseParser.java
private static Node resolveHref(XPathWorkspace w, Node node) throws DOMException { Node href = node.getAttributes().getNamedItem("href"); if (href != null) { return findFirstDescendantHasAttr(node.getOwnerDocument().getDocumentElement(), "id", href.getTextContent().substring(1)); } else {//from w w w . j a v a 2 s. c om return node; } }
From source file:Main.java
/*** * Print every attribute and value of a node, one line at a time. * If {@link entry} is null, then outputs "<null/>". * * @param outs where to send output, cannot be null. * @param entry XML node to examine, OK if null. *//*from w ww. jav a 2 s. c o m*/ public static void XmlPrintAttrs(PrintStream outs, Node entry) { if (entry == null) { outs.print("<null/>"); return; } // see http://www.w3.org/2003/01/dom2-javadoc/org/w3c/dom/NamedNodeMap.html NamedNodeMap attrs = entry.getAttributes(); for (int k = attrs.getLength(); --k >= 0;) { Node n = attrs.item(k); outs.printf("+++ has attr %s = %s\n", n.getNodeName(), n.getNodeValue()); } }