List of usage examples for org.w3c.dom Node getNamespaceURI
public String getNamespaceURI();
null
if it is unspecified (see ). From source file:Main.java
public static String getXPathForElement(Node e, NamespaceContext ctx) { StringBuffer sb = new StringBuffer(); List<Node> path = new ArrayList<Node>(); Node currentNode = e;/* ww w. j av a 2 s . c o m*/ while (currentNode.getParentNode() != currentNode.getOwnerDocument()) { path.add(0, currentNode); if (currentNode instanceof Attr) { Attr a = (Attr) currentNode; currentNode = a.getOwnerElement(); } else { currentNode = currentNode.getParentNode(); } } path.add(0, currentNode); // We need the root element for (Node n : path) { sb.append("/"); if (n.getNodeType() == Node.ATTRIBUTE_NODE) { sb.append("@"); } String namespaceURI = n.getNamespaceURI(); if (namespaceURI != null && !namespaceURI.equals("")) { sb.append(ctx.getPrefix(namespaceURI)).append(":"); } sb.append(n.getLocalName()); if (n.getNodeType() == Node.ELEMENT_NODE) { appendElementQualifier(sb, (Element) n); } } return sb.toString(); }
From source file:Main.java
/** * Prints a given Node<br>//from w ww. j a va 2s. c o m * <p/> * For debugging purpose only */ public static void printNodeBasics(Node node) { if (node == null) { System.out.println(" Null node"); return; } System.out.println(" Node[Namespace URI=" + node.getNamespaceURI() + " localname=" + node.getLocalName() + " name=" + node.getNodeName() + " type=" + getNodeTypeStr(node.getNodeType()) + " Value=" + node.getNodeValue() + "]"); }
From source file:Main.java
/** * This method searches children of Element element for element with tagName * and namespaceURI nsName. It searchs one level down only. * // ww w. j a va 2 s . c o m * @param element * The root element * @param nsName * NamespaceURI * @param tagName * A String representing the name of the tag to be searched for. * @return A List of elements that meet the criterial. */ public static List getElementsByTagNameNS1(Element element, String nsName, String tagName) { List list = new ArrayList(); if (element != null) { NodeList nl = element.getChildNodes(); int length = nl.getLength(); Node child = null; String childName; String childNS; for (int i = 0; i < length; i++) { child = nl.item(i); childName = child.getLocalName(); childNS = child.getNamespaceURI(); if ((childName != null) && (childName.equals(tagName)) && (childNS != null) && (childNS.equals(nsName))) { list.add(child); } } } return list; }
From source file:Main.java
/** * This method searches children of Element element for element with tagName * and namespaceURI nsName. It searchs one level down only. * @param element The root element/* w ww . j av a 2 s .c om*/ * @param nsName NamespaceURI * @param tagName A String representing the name of the tag to be searched * for. * @return A List of elements that meet the criterial. */ @SuppressWarnings("unchecked") public static List getElementsByTagNameNS1(Element element, String nsName, String tagName) { List list = new ArrayList(); if (element != null) { NodeList nl = element.getChildNodes(); int length = nl.getLength(); Node child = null; String childName; String childNS; for (int i = 0; i < length; i++) { child = nl.item(i); childName = child.getLocalName(); childNS = child.getNamespaceURI(); if ((childName != null) && (childName.equals(tagName)) && (childNS != null) && (childNS.equals(nsName))) { list.add(child); } } } return list; }
From source file:Main.java
/** * @param sibling// w ww . j a v a 2s. c o m * @param uri * @param nodeName * @return nodes with the constrain */ public static Element[] selectNodes(Node sibling, String uri, String nodeName) { int size = 20; Element[] a = new Element[size]; int curr = 0; //List list=new ArrayList(); while (sibling != null) { if (nodeName.equals(sibling.getLocalName()) && uri.equals(sibling.getNamespaceURI())) { a[curr++] = (Element) sibling; if (size <= curr) { int cursize = size << 2; Element[] cp = new Element[cursize]; System.arraycopy(a, 0, cp, 0, size); a = cp; size = cursize; } } sibling = sibling.getNextSibling(); } Element[] af = new Element[curr]; System.arraycopy(a, 0, af, 0, curr); return af; }
From source file:Main.java
/** * Search for an XML element in the direct children of parent only. * * This compares localName (nodeName if localName is null) to name, and * checks the tags namespace with the provided namespace. A * <code>null</code> namespace will match any namespace. * <p>/* w w w .j a v a 2s . c o m*/ * This is differs from the DOM version by: * <ul> * <li>not searching recursively</li> * <li>returns a single result</li> * </ul> * * @param parent a parent element * @param name the intended local name * @param namespace the intended namespace (or null) * @return the one child element with that name, or null if none * @throws IllegalArgumentException if there is multiple elements of the * same name * * @since 8.4 */ public static Element findElement(Element parent, String name, String namespace) throws IllegalArgumentException { Element result = null; NodeList l = parent.getChildNodes(); int nodeCount = l.getLength(); for (int i = 0; i < nodeCount; i++) { if (l.item(i).getNodeType() == Node.ELEMENT_NODE) { Node node = l.item(i); String localName = node.getLocalName(); localName = localName == null ? node.getNodeName() : localName; if (name.equals(localName) && (namespace == null || namespace.equals(node.getNamespaceURI()))) { if (result == null) { result = (Element) node; } else { throw new IllegalArgumentException("more than one element with same name found"); } } } } return result; }
From source file:Main.java
/** * Copy one node to another node./*from w w w. j a va 2s. c o m*/ * @param source source Node * @param dest destination Node * @return destination Node */ public static synchronized Node copyNode(Node source, Node dest) { if (source.getNodeType() == Node.TEXT_NODE) { Text tn = dest.getOwnerDocument().createTextNode(source.getNodeValue()); return tn; } Node attr = null; NamedNodeMap attrs = source.getAttributes(); if (attrs != null) { for (int i = 0; i < attrs.getLength(); i++) { attr = attrs.item(i); ((Element) dest).setAttribute(attr.getNodeName(), attr.getNodeValue()); } } Node child = null; NodeList list = source.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { child = list.item(i); if (!(child instanceof Text)) { Element en = dest.getOwnerDocument().createElementNS(child.getNamespaceURI(), child.getNodeName()); if (child.getNodeValue() != null) { en.setNodeValue(child.getNodeValue()); } Node n = copyNode(child, en); dest.appendChild(n); } else if (child instanceof CDATASection) { CDATASection cd = dest.getOwnerDocument().createCDATASection(child.getNodeValue()); dest.appendChild(cd); } else { Text tn = dest.getOwnerDocument().createTextNode(child.getNodeValue()); dest.appendChild(tn); } } return dest; }
From source file:Main.java
/** * Builds an XPath string referencing the given node relative to it's parent. * //w ww.j av a2 s .co m * @param node * @param context namespace context used to determine correct namespace prefixes, see * {@link SignavioNamespaceContext} * @return a relative XPath string or null (if no node given) */ private static String getNodeString(Node node, NamespaceContext context) { if (node == null) { return null; } // get qualified name String nodeName = node.getLocalName(); if (nodeName == null) nodeName = node.getNodeName(); if (node.getNamespaceURI() != null) { String prefix = context.getPrefix(node.getNamespaceURI()); nodeName = prefix + ":" + node.getLocalName(); } if (node instanceof Attr) { return "@" + nodeName; } else if (node instanceof Text) { nodeName = "text()"; } // determine position Node current = node; while (current.getPreviousSibling() != null) { current = current.getPreviousSibling(); } int position = 1; while (current != node) { if (current.getNodeName().equals(node.getNodeName())) position++; current = current.getNextSibling(); } return nodeName + "[" + position + "]"; }
From source file:Main.java
/** * Gets Nodes basic information such as Node name, type, value, namespace * <p/>/*from ww w. j a v a2 s. c o m*/ * and local name */ public static String getNodeBasics(Node node) { StringBuffer sb = new StringBuffer(); if (node == null) { sb.append(" Null node"); return sb.toString(); } sb.append(" Node[Namespace URI=" + node.getNamespaceURI() + " localname=" + node.getLocalName() + " name=" + node.getNodeName() + " type=" + getNodeTypeStr(node.getNodeType()) + " Value=" + node.getNodeValue() + "]"); return sb.toString(); }
From source file:Main.java
public static boolean nodeHasNameNamespace(Node node, String name, String namespaceURI) { String localName = node.getNodeName(); String pre = node.getPrefix(); if (pre != null) { localName = localName.substring(pre.length() + 1); }//from w w w . j a v a 2 s.com if (namespaceURI != null) { if (!namespaceURI.equals(node.getNamespaceURI())) { return false; } } return name.equals(localName); }