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

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

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

Node.getOwnerDocument介绍

[英]The Document object associated with this node. This is also the Document object used to create new nodes. When this node is a Document or a DocumentType which is not used with any Document yet, this is null.
[中]与此节点关联的Document对象。这也是用于创建新节点的Document对象。当此节点为DocumentDocumentType且尚未与任何Document一起使用时,此节点为null

代码示例

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

/** If the child node doesn't exist, it is created. */
private static Node getFirstChildNodeByName (Node parent, String child) {
  NodeList childNodes = parent.getChildNodes();
  for (int i = 0; i < childNodes.getLength(); i++) {
    if (childNodes.item(i).getNodeName().equals(child)) {
      return childNodes.item(i);
    }
  }
  Node newNode = parent.getOwnerDocument().createElement(child);
  if (childNodes.item(0) != null)
    return parent.insertBefore(newNode, childNodes.item(0));
  else
    return parent.appendChild(newNode);
}

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

/**
 * Get the root node of the document tree, regardless of
 * whether or not the node passed in is a document node.
 * <p>
 * TODO: This doesn't handle DocumentFragments or "orphaned" subtrees
 * -- it's currently returning ownerDocument even when the tree is
 * not actually part of the main Document tree. We should either
 * rewrite the description to say that it finds the Document node,
 * or change the code to walk up the ancestor chain.
 *
 * @param n Node to be examined
 *
 * @return the Document node. Note that this is not the correct answer
 * if n was (or was a child of) a DocumentFragment or an orphaned node,
 * as can arise if the DOM has been edited rather than being generated
 * by a parser.
 */
public Node getRootNode(Node n)
{
 int nt = n.getNodeType();
 return ( (Node.DOCUMENT_NODE == nt) || (Node.DOCUMENT_FRAGMENT_NODE == nt) ) 
     ? n : n.getOwnerDocument();
}

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

static class LoggingErrorListener implements ErrorListener {
    // See http://www.cafeconleche.org/slides/sd2003west/xmlandjava/346.html
  
  boolean strict;
    public LoggingErrorListener(boolean strict) {
  }
  
  public void warning(TransformerException exception) {
      log.warn(exception.getMessage(), exception);
      // Don't throw an exception and stop the processor
   // just for a warning; but do log the problem
  }
  
  public void error(TransformerException exception)
   throws TransformerException {
   
    log.error(exception.getMessage(), exception);
   
    // XSLT is not as draconian as XML. There are numerous errors
   // which the processor may but does not have to recover from; 
   // e.g. multiple templates that match a node with the same
   // priority. If I do not want to allow that,  I'd throw this 
   // exception here.
    if (strict) {
      throw exception;
    }

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

/**
 * Get the root node of the document tree, regardless of
 * whether or not the node passed in is a document node.
 * <p>
 * TODO: This doesn't handle DocumentFragments or "orphaned" subtrees
 * -- it's currently returning ownerDocument even when the tree is
 * not actually part of the main Document tree. We should either
 * rewrite the description to say that it finds the Document node,
 * or change the code to walk up the ancestor chain.
 *
 * @param n Node to be examined
 *
 * @return the Document node. Note that this is not the correct answer
 * if n was (or was a child of) a DocumentFragment or an orphaned node,
 * as can arise if the DOM has been edited rather than being generated
 * by a parser.
 */
public Node getRootNode(Node n)
{
 int nt = n.getNodeType();
 return ( (Node.DOCUMENT_NODE == nt) || (Node.DOCUMENT_FRAGMENT_NODE == nt) ) 
     ? n : n.getOwnerDocument();
}

代码示例来源:origin: org.apache.tuscany.sca/tuscany-databinding

public void setChild(Node wrapper, int i, ElementInfo childElement, Object value) {
  Node node = (Node)value;
  if (node.getNodeType() == Node.DOCUMENT_NODE) {
    node = ((Document)node).getDocumentElement();
  }
  wrapper.appendChild(wrapper.getOwnerDocument().importNode(node, true));
}

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

/** If the child node doesn't exist, it is created. */
private static Node getFirstChildNodeByName (Node parent, String child) {
  NodeList childNodes = parent.getChildNodes();
  for (int i = 0; i < childNodes.getLength(); i++) {
    if (childNodes.item(i).getNodeName().equals(child)) {
      return childNodes.item(i);
    }
  }
  Node newNode = parent.getOwnerDocument().createElement(child);
  if (childNodes.item(0) != null)
    return parent.insertBefore(newNode, childNodes.item(0));
  else
    return parent.appendChild(newNode);
}

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

public static TreeWalker createTreeWalker(Node root, String... tagNames) {
  return createTreeWalker(root.getNodeType() == Node.DOCUMENT_NODE ? (Document) root : root.getOwnerDocument(), root,
      tagNames);
}

代码示例来源:origin: org.apache.tuscany.sca/tuscany-base-runtime

public void setChild(Node wrapper, int i, ElementInfo childElement, Object value) {
  Node node = (Node)value;
  if (node.getNodeType() == Node.DOCUMENT_NODE) {
    node = ((Document)node).getDocumentElement();
  }
  wrapper.appendChild(wrapper.getOwnerDocument().importNode(node, true));
}

代码示例来源:origin: looly/hutool

/**
 * 在已有节点上创建子节点
 * 
 * @param node 节点
 * @param tagName 标签名
 * @return 子节点
 * @since 4.0.9
 */
public static Element appendChild(Node node, String tagName) {
  Document doc = (node instanceof Document) ? (Document) node : node.getOwnerDocument();
  Element child = doc.createElement(tagName);
  node.appendChild(child);
  return child;
}

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

public static NodeIterator createNodeIterator(Node root, String... tagNames) {
  return createNodeIterator(root.getNodeType() == Node.DOCUMENT_NODE ? (Document) root : root.getOwnerDocument(), root,
      tagNames);
}

代码示例来源:origin: org.codehaus.castor/castor-xml

@Override
public void characters(final char[] chars, final int offset, final int length) {
 String data = new String(chars, offset, length);
 Node parent = !_parents.isEmpty() ? _parents.peek() : _node;
 Node last = parent.getLastChild();
 if ((last != null) && (last.getNodeType() == Node.TEXT_NODE)) {
  ((Text) last).appendData(data);
 } else {
  Text text = parent.getOwnerDocument().createTextNode(data);
  parent.appendChild(text);
 }
}

代码示例来源:origin: looly/hutool

/**
 * 在已有节点上创建子节点
 * 
 * @param node 节点
 * @param tagName 标签名
 * @return 子节点
 * @since 4.0.9
 */
public static Element appendChild(Node node, String tagName) {
  Document doc = (node instanceof Document) ? (Document) node : node.getOwnerDocument();
  Element child = doc.createElement(tagName);
  node.appendChild(child);
  return child;
}

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

public static NodeIterator createNodeIterator(Node root, String... tagNames) {
  return createNodeIterator(root.getNodeType() == Node.DOCUMENT_NODE ? (Document) root : root.getOwnerDocument(), root,
      tagNames);
}

代码示例来源:origin: org.codehaus.castor/com.springsource.org.castor

public void characters(final char[] chars, final int offset, final int length) {
  String data = new String(chars, offset, length);
  Node parent = (_parents.size() > 0) ? (Node) _parents.peek() : _document;
  Node last = parent.getLastChild();
  if ((last != null) && (last.getNodeType() == Node.TEXT_NODE)) {
    ((Text)last).appendData(data);
  } else {
    Text text = parent.getOwnerDocument().createTextNode(data);
    parent.appendChild(text);
  }
}

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

/** If the child node or attribute doesn't exist, it is created. Usage example: Node property =
 * getFirstChildByAttrValue(properties, "property", "name"); */
private static Node getFirstChildByNameAttrValue (Node node, String childName, String attr, String value) {
  NodeList childNodes = node.getChildNodes();
  for (int i = 0; i < childNodes.getLength(); i++) {
    if (childNodes.item(i).getNodeName().equals(childName)) {
      NamedNodeMap attributes = childNodes.item(i).getAttributes();
      Node attribute = attributes.getNamedItem(attr);
      if (attribute.getNodeValue().equals(value)) return childNodes.item(i);
    }
  }
  Node newNode = node.getOwnerDocument().createElement(childName);
  NamedNodeMap attributes = newNode.getAttributes();
  Attr nodeAttr = node.getOwnerDocument().createAttribute(attr);
  nodeAttr.setNodeValue(value);
  attributes.setNamedItem(nodeAttr);
  if (childNodes.item(0) != null) {
    return node.insertBefore(newNode, childNodes.item(0));
  } else {
    return node.appendChild(newNode);
  }
}

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

public static TreeWalker createTreeWalker(Node root, String... tagNames) {
  return createTreeWalker(root.getNodeType() == Node.DOCUMENT_NODE ? (Document) root : root.getOwnerDocument(), root,
      tagNames);
}

代码示例来源:origin: org.springframework.ws/spring-oxm

protected final void marshalDomNode(Object graph, Node node) throws XmlMappingException {
  Document document = node.getNodeType() == Node.DOCUMENT_NODE ? (Document) node : node.getOwnerDocument();
  Node xmlBeansNode = ((XmlObject) graph).newDomNode(getXmlOptions());
  NodeList xmlBeansChildNodes = xmlBeansNode.getChildNodes();
  for (int i = 0; i < xmlBeansChildNodes.getLength(); i++) {
    Node xmlBeansChildNode = xmlBeansChildNodes.item(i);
    Node importedNode = document.importNode(xmlBeansChildNode, true);
    node.appendChild(importedNode);
  }
}

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

/** If the child node or attribute doesn't exist, it is created. Usage example: Node property =
 * getFirstChildByAttrValue(properties, "property", "name"); */
private static Node getFirstChildByNameAttrValue (Node node, String childName, String attr, String value) {
  NodeList childNodes = node.getChildNodes();
  for (int i = 0; i < childNodes.getLength(); i++) {
    if (childNodes.item(i).getNodeName().equals(childName)) {
      NamedNodeMap attributes = childNodes.item(i).getAttributes();
      Node attribute = attributes.getNamedItem(attr);
      if (attribute.getNodeValue().equals(value)) return childNodes.item(i);
    }
  }
  Node newNode = node.getOwnerDocument().createElement(childName);
  NamedNodeMap attributes = newNode.getAttributes();
  Attr nodeAttr = node.getOwnerDocument().createAttribute(attr);
  nodeAttr.setNodeValue(value);
  attributes.setNamedItem(nodeAttr);
  if (childNodes.item(0) != null) {
    return node.insertBefore(newNode, childNodes.item(0));
  } else {
    return node.appendChild(newNode);
  }
}

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

/**
 * This method returns the owner document of a particular node.
 * This method is necessary because it <I>always</I> returns a
 * {@link Document}. {@link Node#getOwnerDocument} returns <CODE>null</CODE>
 * if the {@link Node} is a {@link Document}.
 *
 * @param node
 * @return the owner document of the node
 */
public static Document getOwnerDocument(Node node) {
  if (node.getNodeType() == Node.DOCUMENT_NODE) {
    return (Document) node;
  } 
  try {
    return node.getOwnerDocument();
  } catch (NullPointerException npe) {
    throw new NullPointerException(I18n.translate("endorsed.jdk1.4.0")
                    + " Original message was \""
                    + npe.getMessage() + "\"");
  }
}

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

public static Element appendChildElement(Node node, String childName) {
  if (node == null)
    throw new NullPointerException("Received null node");
  return (Element) node.appendChild(createElement(node.getOwnerDocument(), childName));
}

相关文章