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

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

本文整理了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

  1. /** If the child node doesn't exist, it is created. */
  2. private static Node getFirstChildNodeByName (Node parent, String child) {
  3. NodeList childNodes = parent.getChildNodes();
  4. for (int i = 0; i < childNodes.getLength(); i++) {
  5. if (childNodes.item(i).getNodeName().equals(child)) {
  6. return childNodes.item(i);
  7. }
  8. }
  9. Node newNode = parent.getOwnerDocument().createElement(child);
  10. if (childNodes.item(0) != null)
  11. return parent.insertBefore(newNode, childNodes.item(0));
  12. else
  13. return parent.appendChild(newNode);
  14. }

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

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

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

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

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

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

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

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

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

  1. /** If the child node doesn't exist, it is created. */
  2. private static Node getFirstChildNodeByName (Node parent, String child) {
  3. NodeList childNodes = parent.getChildNodes();
  4. for (int i = 0; i < childNodes.getLength(); i++) {
  5. if (childNodes.item(i).getNodeName().equals(child)) {
  6. return childNodes.item(i);
  7. }
  8. }
  9. Node newNode = parent.getOwnerDocument().createElement(child);
  10. if (childNodes.item(0) != null)
  11. return parent.insertBefore(newNode, childNodes.item(0));
  12. else
  13. return parent.appendChild(newNode);
  14. }

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

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

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

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

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

  1. /**
  2. * 在已有节点上创建子节点
  3. *
  4. * @param node 节点
  5. * @param tagName 标签名
  6. * @return 子节点
  7. * @since 4.0.9
  8. */
  9. public static Element appendChild(Node node, String tagName) {
  10. Document doc = (node instanceof Document) ? (Document) node : node.getOwnerDocument();
  11. Element child = doc.createElement(tagName);
  12. node.appendChild(child);
  13. return child;
  14. }

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

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

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

  1. @Override
  2. public void characters(final char[] chars, final int offset, final int length) {
  3. String data = new String(chars, offset, length);
  4. Node parent = !_parents.isEmpty() ? _parents.peek() : _node;
  5. Node last = parent.getLastChild();
  6. if ((last != null) && (last.getNodeType() == Node.TEXT_NODE)) {
  7. ((Text) last).appendData(data);
  8. } else {
  9. Text text = parent.getOwnerDocument().createTextNode(data);
  10. parent.appendChild(text);
  11. }
  12. }

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

  1. /**
  2. * 在已有节点上创建子节点
  3. *
  4. * @param node 节点
  5. * @param tagName 标签名
  6. * @return 子节点
  7. * @since 4.0.9
  8. */
  9. public static Element appendChild(Node node, String tagName) {
  10. Document doc = (node instanceof Document) ? (Document) node : node.getOwnerDocument();
  11. Element child = doc.createElement(tagName);
  12. node.appendChild(child);
  13. return child;
  14. }

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

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

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

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

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

  1. /** If the child node or attribute doesn't exist, it is created. Usage example: Node property =
  2. * getFirstChildByAttrValue(properties, "property", "name"); */
  3. private static Node getFirstChildByNameAttrValue (Node node, String childName, String attr, String value) {
  4. NodeList childNodes = node.getChildNodes();
  5. for (int i = 0; i < childNodes.getLength(); i++) {
  6. if (childNodes.item(i).getNodeName().equals(childName)) {
  7. NamedNodeMap attributes = childNodes.item(i).getAttributes();
  8. Node attribute = attributes.getNamedItem(attr);
  9. if (attribute.getNodeValue().equals(value)) return childNodes.item(i);
  10. }
  11. }
  12. Node newNode = node.getOwnerDocument().createElement(childName);
  13. NamedNodeMap attributes = newNode.getAttributes();
  14. Attr nodeAttr = node.getOwnerDocument().createAttribute(attr);
  15. nodeAttr.setNodeValue(value);
  16. attributes.setNamedItem(nodeAttr);
  17. if (childNodes.item(0) != null) {
  18. return node.insertBefore(newNode, childNodes.item(0));
  19. } else {
  20. return node.appendChild(newNode);
  21. }
  22. }

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

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

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

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

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

  1. /** If the child node or attribute doesn't exist, it is created. Usage example: Node property =
  2. * getFirstChildByAttrValue(properties, "property", "name"); */
  3. private static Node getFirstChildByNameAttrValue (Node node, String childName, String attr, String value) {
  4. NodeList childNodes = node.getChildNodes();
  5. for (int i = 0; i < childNodes.getLength(); i++) {
  6. if (childNodes.item(i).getNodeName().equals(childName)) {
  7. NamedNodeMap attributes = childNodes.item(i).getAttributes();
  8. Node attribute = attributes.getNamedItem(attr);
  9. if (attribute.getNodeValue().equals(value)) return childNodes.item(i);
  10. }
  11. }
  12. Node newNode = node.getOwnerDocument().createElement(childName);
  13. NamedNodeMap attributes = newNode.getAttributes();
  14. Attr nodeAttr = node.getOwnerDocument().createAttribute(attr);
  15. nodeAttr.setNodeValue(value);
  16. attributes.setNamedItem(nodeAttr);
  17. if (childNodes.item(0) != null) {
  18. return node.insertBefore(newNode, childNodes.item(0));
  19. } else {
  20. return node.appendChild(newNode);
  21. }
  22. }

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

  1. /**
  2. * This method returns the owner document of a particular node.
  3. * This method is necessary because it <I>always</I> returns a
  4. * {@link Document}. {@link Node#getOwnerDocument} returns <CODE>null</CODE>
  5. * if the {@link Node} is a {@link Document}.
  6. *
  7. * @param node
  8. * @return the owner document of the node
  9. */
  10. public static Document getOwnerDocument(Node node) {
  11. if (node.getNodeType() == Node.DOCUMENT_NODE) {
  12. return (Document) node;
  13. }
  14. try {
  15. return node.getOwnerDocument();
  16. } catch (NullPointerException npe) {
  17. throw new NullPointerException(I18n.translate("endorsed.jdk1.4.0")
  18. + " Original message was \""
  19. + npe.getMessage() + "\"");
  20. }
  21. }

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

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

相关文章