org.w3c.dom.Node.getNamespaceURI()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(10.5k)|赞(0)|评价(0)|浏览(374)

本文整理了Java中org.w3c.dom.Node.getNamespaceURI()方法的一些代码示例,展示了Node.getNamespaceURI()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Node.getNamespaceURI()方法的具体详情如下:
包路径:org.w3c.dom.Node
类名称:Node
方法名:getNamespaceURI

Node.getNamespaceURI介绍

[英]The namespace URI of this node, or null if it is unspecified (see ).
This is not a computed value that is the result of a namespace lookup based on an examination of the namespace declarations in scope. It is merely the namespace URI given at creation time.
For nodes of any type other than ELEMENT_NODE and ATTRIBUTE_NODE and nodes created with a DOM Level 1 method, such as Document.createElement(), this is always null.

Note: Per the Namespaces in XML Specification [XML Namespaces] an attribute does not inherit its namespace from the element it is attached to. If an attribute is not explicitly given a namespace, it simply has no namespace.
[中]此节点的命名空间URI,如果未指定,则为null(请参阅)。
这不是一个计算值,它不是基于对范围中的命名空间声明的检查而进行的命名空间查找的结果。它只是创建时给定的名称空间URI。
对于ELEMENT_NODEATTRIBUTE_NODE之外的任何类型的节点,以及使用DOM级别1方法创建的节点,例如Document.createElement(),这始终是null
注意:根据XML规范[XML Namespaces]中的名称空间,属性不会从其附加到的元素继承其名称空间。如果一个属性没有显式地给出名称空间,那么它就没有名称空间。

代码示例

代码示例来源:origin: org.freemarker/freemarker

  1. public String getNodeNamespace() {
  2. int nodeType = node.getNodeType();
  3. if (nodeType != Node.ATTRIBUTE_NODE && nodeType != Node.ELEMENT_NODE) {
  4. return null;
  5. }
  6. String result = node.getNamespaceURI();
  7. if (result == null && nodeType == Node.ELEMENT_NODE) {
  8. result = "";
  9. } else if ("".equals(result) && nodeType == Node.ATTRIBUTE_NODE) {
  10. result = null;
  11. }
  12. return result;
  13. }

代码示例来源:origin: plutext/docx4j

  1. /**
  2. * @param sibling
  3. * @param uri
  4. * @param nodeName
  5. * @param number
  6. * @return nodes with the constrain
  7. */
  8. public static Element selectNode(Node sibling, String uri, String nodeName, int number) {
  9. while (sibling != null) {
  10. if (sibling.getNamespaceURI() != null && sibling.getNamespaceURI().equals(uri)
  11. && sibling.getLocalName().equals(nodeName)) {
  12. if (number == 0){
  13. return (Element)sibling;
  14. }
  15. number--;
  16. }
  17. sibling = sibling.getNextSibling();
  18. }
  19. return null;
  20. }

代码示例来源:origin: jaxen/jaxen

  1. /**
  2. * Constructor.
  3. *
  4. * @param parent the parent DOM element for the attributes.
  5. */
  6. AttributeIterator (Node parent)
  7. {
  8. this.map = parent.getAttributes();
  9. this.pos = 0;
  10. for (int i = this.map.getLength()-1; i >= 0; i--) {
  11. Node node = map.item(i);
  12. if (! "http://www.w3.org/2000/xmlns/".equals(node.getNamespaceURI())) {
  13. this.lastAttribute = i;
  14. break;
  15. }
  16. }
  17. }

代码示例来源:origin: robovm/robovm

  1. /**
  2. * Look up the index of an attribute by Namespace name.
  3. *
  4. * @param uri The Namespace URI, or the empty string if
  5. * the name has no Namespace URI.
  6. * @param localPart The attribute's local name.
  7. * @return The index of the attribute, or -1 if it does not
  8. * appear in the list.
  9. */
  10. public int getIndex(String uri, String localPart)
  11. {
  12. for(int i=m_attrs.getLength()-1;i>=0;--i)
  13. {
  14. Node a=m_attrs.item(i);
  15. String u=a.getNamespaceURI();
  16. if( (u==null ? uri==null : u.equals(uri))
  17. &&
  18. a.getLocalName().equals(localPart) )
  19. return i;
  20. }
  21. return -1;
  22. }

代码示例来源:origin: org.netbeans.api/org-openide-util

  1. int nodeCount = l.getLength();
  2. for (int i = 0; i < nodeCount; i++) {
  3. if (l.item(i).getNodeType() == Node.ELEMENT_NODE) {
  4. Node node = l.item(i);
  5. String localName = node.getLocalName();
  6. localName = localName == null ? node.getNodeName() : localName;
  7. && (namespace == null || namespace.equals(node.getNamespaceURI()))) {
  8. if (result == null) {
  9. result = (Element)node;

代码示例来源:origin: robovm/robovm

  1. Node p = (root.getNodeType() == Node.ATTRIBUTE_NODE) ? ((org.w3c.dom.Attr)root).getOwnerElement() : root.getParentNode();
  2. for (; p != null; p = p.getParentNode())
  3. handle=dtm.getAttributeNode(handle,node.getNamespaceURI(),node.getLocalName());

代码示例来源:origin: org.xmlunit/xmlunit-core

  1. /**
  2. * Extracts a Node's name, namespace URI (if any) and prefix as a
  3. * QName.
  4. */
  5. public static QName getQName(Node n) {
  6. String s = n.getLocalName();
  7. String p = n.getPrefix();
  8. return s != null
  9. ? new QName(n.getNamespaceURI(), s,
  10. p != null ? p: XMLConstants.DEFAULT_NS_PREFIX)
  11. : new QName(n.getNodeName());
  12. }

代码示例来源:origin: org.freemarker/freemarker

  1. @Override
  2. void getChildren(Object node, String localName, String namespaceUri, List result) {
  3. if ("".equals(namespaceUri)) {
  4. namespaceUri = null;
  5. }
  6. NodeList children = ((Node) node).getChildNodes();
  7. for (int i = 0; i < children.getLength(); ++i) {
  8. Node subnode = children.item(i);
  9. // IMO, we should get the text nodes as well -- will discuss.
  10. if (subnode.getNodeType() == Node.ELEMENT_NODE || subnode.getNodeType() == Node.TEXT_NODE) {
  11. if (localName == null || (equal(subnode.getNodeName(), localName) && equal(subnode.getNamespaceURI(), namespaceUri))) {
  12. result.add(subnode);
  13. }
  14. }
  15. }
  16. }

代码示例来源:origin: org.apache.poi/poi-ooxml

  1. protected void write(OutputStream out) throws IOException {
  2. XmlObject rootObject = XmlObject.Factory.newInstance();
  3. XmlCursor rootCursor = rootObject.newCursor();
  4. rootCursor.toNextToken();
  5. rootCursor.beginElement("xml");
  6. for(int i=0; i < _items.size(); i++){
  7. XmlCursor xc = _items.get(i).newCursor();
  8. rootCursor.beginElement(_qnames.get(i));
  9. while(xc.toNextToken() == XmlCursor.TokenType.ATTR) {
  10. Node anode = xc.getDomNode();
  11. rootCursor.insertAttributeWithValue(anode.getLocalName(), anode.getNamespaceURI(), anode.getNodeValue());
  12. }
  13. xc.toStartDoc();
  14. xc.copyXmlContents(rootCursor);
  15. rootCursor.toNextToken();
  16. xc.dispose();
  17. }
  18. rootCursor.dispose();
  19. rootObject.save(out, DEFAULT_XML_OPTIONS);
  20. }

代码示例来源:origin: org.freemarker/freemarker

  1. private void outputQualifiedName(Node n, StringBuilder buf) {
  2. String nsURI = n.getNamespaceURI();
  3. if (nsURI == null || nsURI.length() == 0) {
  4. buf.append(n.getNodeName());
  5. } else {
  6. String prefix = namespacesToPrefixLookup.get(nsURI);
  7. if (prefix == null) {
  8. //REVISIT!
  9. buf.append(n.getNodeName());
  10. } else {
  11. if (prefix.length() > 0) {
  12. buf.append(prefix);
  13. buf.append(':');
  14. }
  15. buf.append(n.getLocalName());
  16. }
  17. }
  18. }

代码示例来源:origin: org.netbeans.api/org-openide-util

  1. private static void collectCDATASections(Node node, Set<String> cdataQNames) {
  2. if (node instanceof CDATASection) {
  3. Node parent = node.getParentNode();
  4. if (parent != null) {
  5. String uri = parent.getNamespaceURI();
  6. if (uri != null) {
  7. cdataQNames.add("{" + uri + "}" + parent.getNodeName()); //NOI18N
  8. } else {
  9. cdataQNames.add(parent.getNodeName());
  10. }
  11. }
  12. }
  13. NodeList children = node.getChildNodes();
  14. for(int i = 0; i < children.getLength(); i++) {
  15. collectCDATASections(children.item(i), cdataQNames);
  16. }
  17. }

代码示例来源:origin: robovm/robovm

  1. /**
  2. * Look up the index of an attribute by Namespace name.
  3. *
  4. * @param uri The Namespace URI, or the empty string if
  5. * the name has no Namespace URI.
  6. * @param localPart The attribute's local name.
  7. * @return The index of the attribute, or -1 if it does not
  8. * appear in the list.
  9. */
  10. public int getIndex(String uri, String localPart)
  11. {
  12. for(int i=m_attrs.getLength()-1;i>=0;--i)
  13. {
  14. Node a=m_attrs.item(i);
  15. String u=a.getNamespaceURI();
  16. if( (u==null ? uri==null : u.equals(uri))
  17. &&
  18. a.getLocalName().equals(localPart) )
  19. return i;
  20. }
  21. return -1;
  22. }

代码示例来源:origin: robovm/robovm

  1. values.add(node.getNodeType());
  2. values.add(node.getNodeName());
  3. values.add(node.getLocalName());
  4. values.add(node.getNamespaceURI());
  5. values.add(node.getPrefix());
  6. values.add(node.getNodeValue());
  7. for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) {
  8. values.add(child);
  9. switch (node.getNodeType()) {
  10. case DOCUMENT_TYPE_NODE:
  11. DocumentTypeImpl doctype = (DocumentTypeImpl) node;

代码示例来源:origin: xalan/xalan

  1. Node p = (root.getNodeType() == Node.ATTRIBUTE_NODE) ? ((org.w3c.dom.Attr)root).getOwnerElement() : root.getParentNode();
  2. for (; p != null; p = p.getParentNode())
  3. handle=dtm.getAttributeNode(handle,node.getNamespaceURI(),node.getLocalName());

代码示例来源:origin: com.sun.xml.parsers/jaxp-ri

  1. protected final void updateQName (Node node, QName qname){
  2. String prefix = node.getPrefix();
  3. String namespace = node.getNamespaceURI();
  4. String localName = node.getLocalName();
  5. // REVISIT: the symbols are added too often: start/endElement
  6. // and in the namespaceFixup. Should reduce number of calls to symbol table.
  7. qname.prefix = (prefix!=null && prefix.length()!=0)?fSymbolTable.addSymbol(prefix):null;
  8. qname.localpart = (localName != null)?fSymbolTable.addSymbol(localName):null;
  9. qname.rawname = fSymbolTable.addSymbol(node.getNodeName());
  10. qname.uri = (namespace != null)?fSymbolTable.addSymbol(namespace):null;
  11. }

代码示例来源:origin: haraldk/TwelveMonkeys

  1. private void parseAttributesForKnownElements(Map<String, List<Entry>> subdirs, Node desc) {
  2. // NOTE: NamedNodeMap does not have any particular order...
  3. NamedNodeMap attributes = desc.getAttributes();
  4. for (Node attr : asIterable(attributes)) {
  5. if (!XMP.ELEMENTS.contains(attr.getNamespaceURI())) {
  6. continue;
  7. }
  8. List<Entry> dir = subdirs.get(attr.getNamespaceURI());
  9. if (dir == null) {
  10. dir = new ArrayList<Entry>();
  11. subdirs.put(attr.getNamespaceURI(), dir);
  12. }
  13. dir.add(new XMPEntry(attr.getNamespaceURI() + attr.getLocalName(), attr.getLocalName(), attr.getNodeValue()));
  14. }
  15. }

代码示例来源:origin: jaxen/jaxen

  1. /**
  2. * Test if a node is an attribute. <code>xmlns</code> and
  3. * <code>xmlns:pre</code> attributes do not count as attributes
  4. * for the purposes of XPath.
  5. *
  6. * @param object the target node
  7. * @return true if the node is an attribute, false otherwise
  8. */
  9. public boolean isAttribute (Object object)
  10. {
  11. return (object instanceof Node) &&
  12. (((Node)object).getNodeType() == Node.ATTRIBUTE_NODE)
  13. && ! "http://www.w3.org/2000/xmlns/".equals(((Node) object).getNamespaceURI());
  14. }

代码示例来源:origin: org.freemarker/freemarker

  1. @Override
  2. String getQualifiedName() {
  3. String nsURI = node.getNamespaceURI();
  4. if (nsURI == null || nsURI.equals(""))
  5. return node.getNodeName();
  6. Environment env = Environment.getCurrentEnvironment();
  7. String defaultNS = env.getDefaultNS();
  8. String prefix = null;
  9. if (nsURI.equals(defaultNS)) {
  10. prefix = "D";
  11. } else {
  12. prefix = env.getPrefixForNamespace(nsURI);
  13. }
  14. if (prefix == null) {
  15. return null;
  16. }
  17. return prefix + ":" + node.getLocalName();
  18. }
  19. }

代码示例来源:origin: plutext/docx4j

  1. /**
  2. * @param sibling
  3. * @param uri
  4. * @param nodeName
  5. * @return nodes with the constraint
  6. */
  7. public static Element[] selectNodes(Node sibling, String uri, String nodeName) {
  8. List<Element> list = new ArrayList<Element>();
  9. while (sibling != null) {
  10. if (sibling.getNamespaceURI() != null && sibling.getNamespaceURI().equals(uri)
  11. && sibling.getLocalName().equals(nodeName)) {
  12. list.add((Element)sibling);
  13. }
  14. sibling = sibling.getNextSibling();
  15. }
  16. return list.toArray(new Element[list.size()]);
  17. }

代码示例来源:origin: apache/geode

  1. /**
  2. * Change the namespace URI of a <code>node</code> and its children to
  3. * <code>newNamespaceUri</code> if that node is in the given <code>oldNamespaceUri</code>
  4. * namespace URI.
  5. *
  6. *
  7. * @param node {@link Node} to change namespace URI on.
  8. * @param oldNamespaceUri old namespace URI to change from.
  9. * @param newNamespaceUri new Namespace URI to change to.
  10. * @return the modified version of the passed in node.
  11. * @since GemFire 8.1
  12. */
  13. static Node changeNamespace(final Node node, final String oldNamespaceUri,
  14. final String newNamespaceUri) throws XPathExpressionException {
  15. Node result = null;
  16. final NodeList nodes = query(node, "//*");
  17. for (int i = 0; i < nodes.getLength(); i++) {
  18. final Node element = nodes.item(i);
  19. if (element.getNamespaceURI() == null || element.getNamespaceURI().equals(oldNamespaceUri)) {
  20. Node renamed =
  21. node.getOwnerDocument().renameNode(element, newNamespaceUri, element.getNodeName());
  22. if (element == node) {
  23. result = renamed;
  24. }
  25. }
  26. }
  27. return result;
  28. }

相关文章