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

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

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

Node.getParentNode介绍

[英]The parent of this node. All nodes, except Attr, Document, DocumentFragment, Entity, and Notation may have a parent. However, if a node has just been created and not yet added to the tree, or if it has been removed from the tree, this is null.
[中]此节点的父节点。除AttrDocumentDocumentFragmentEntityNotation之外的所有节点都可能有父节点。但是,如果节点刚刚创建,尚未添加到树中,或者已从树中删除,则此值为[$5$]。

代码示例

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

  1. @Override
  2. public int jjtGetChildIndex() {
  3. org.w3c.dom.Node parent = node.getParentNode();
  4. NodeList childNodes = parent.getChildNodes();
  5. for (int i = 0; i < childNodes.getLength(); i++) {
  6. if (node == childNodes.item(i)) {
  7. return i;
  8. }
  9. }
  10. throw new IllegalStateException("This node is not a child of its parent: " + node);
  11. }

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

  1. /**
  2. * Get the XPath-model parent of a node. This version takes advantage
  3. * of the DOM Level 2 Attr.ownerElement() method; the base version we
  4. * would otherwise inherit is prepared to fall back on exhaustively
  5. * walking the document to find an Attr's parent.
  6. *
  7. * @param node Node to be examined
  8. *
  9. * @return the DOM parent of the input node, if there is one, or the
  10. * ownerElement if the input node is an Attr, or null if the node is
  11. * a Document, a DocumentFragment, or an orphan.
  12. */
  13. public static Node getParentOfNode(Node node)
  14. {
  15. Node parent=node.getParentNode();
  16. if(parent==null && (Node.ATTRIBUTE_NODE == node.getNodeType()) )
  17. parent=((Attr) node).getOwnerElement();
  18. return parent;
  19. }

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

  1. protected Node getNextNode (Node node) {
  2. if (node == null) {
  3. return null;
  4. }
  5. else {
  6. Node n = node.getFirstChild();
  7. if (n == null) n = node.getNextSibling();
  8. if (n == null) return getFirstNode(node.getParentNode());
  9. else return n;
  10. }
  11. }
  12. };

代码示例来源:origin: pentaho/pentaho-kettle

  1. private void removeEmptyNodes( NodeList nodes ) {
  2. for ( int i = 0; i < nodes.getLength(); i++ ) {
  3. Node node = nodes.item( i );
  4. // Process the tree bottom-up
  5. if ( node.hasChildNodes() ) {
  6. removeEmptyNodes( node.getChildNodes() );
  7. }
  8. boolean nodeIsEmpty =
  9. node.getNodeType() == Node.ELEMENT_NODE && !node.hasAttributes() && !node.hasChildNodes()
  10. && node.getTextContent().length() == 0;
  11. if ( nodeIsEmpty ) {
  12. // We shifted elements left, do not increment counter
  13. node.getParentNode().removeChild( node );
  14. i--;
  15. }
  16. }
  17. }
  18. }

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

  1. Node p=n.getNextSibling();
  2. if(p==null)
  3. for(n=n.getParentNode();
  4. n!=null && ENTITY_REFERENCE_NODE == n.getNodeType();
  5. n=n.getParentNode())
  6. p=n.getNextSibling();
  7. if(p!=null)
  8. break;
  9. while(n!=null && ENTITY_REFERENCE_NODE == n.getNodeType())
  10. n=n.getFirstChild();
  11. else
  12. n=n.getNextSibling();
  13. int ntype=n.getNodeType();
  14. if(TEXT_NODE != ntype && CDATA_SECTION_NODE != ntype)
  15. n=null;

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

  1. while (childA.getParentNode() != a)
  2. childA = childA.getParentNode();
  3. Node childZ = last;
  4. while (childZ.getParentNode() != a)
  5. childZ = childZ.getParentNode();
  6. Document doc = (a.getNodeType() == Node.DOCUMENT_NODE) ? (Document) a : a.getOwnerDocument();
  7. Element newElement = doc.createElementNS(doc.getDocumentElement().getNamespaceURI(), newElementName);
  8. Node helper;
  9. while (c != null && c != childZ) {
  10. helper = c.getNextSibling();

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

  1. /**
  2. * Analogous to the Node.insertBefore() method, insert a newNode after a refNode.
  3. *
  4. * @param newNode
  5. * new node
  6. * @param refNode
  7. * ref node
  8. * @throws DOMException
  9. * DOMException
  10. */
  11. public static void insertAfter(Node newNode, Node refNode) throws DOMException {
  12. Node parent = refNode.getParentNode();
  13. Node next = refNode.getNextSibling();
  14. if (next == null) {
  15. parent.appendChild(newNode);
  16. } else {
  17. parent.insertBefore(newNode, next);
  18. }
  19. }

代码示例来源:origin: stackoverflow.com

  1. myDoc = builder.parse(is);
  2. NodeList nl = myDoc.getElementsByTagName("url");
  3. for (int i = 0; i < nl.getLength(); i++) {
  4. Node nd = nl.item(i);
  5. if(nd instanceof Element) {
  6. Element el = (Element) nd;
  7. for (int j = 0; j < nodes.getLength(); j++) {
  8. Node node = nodes.item(j);
  9. if (node instanceof Text) {
  10. String dt = ((Text)node).getData();
  11. dt = new String(b);
  12. urlList.add(dt);
  13. urlNodes.add((Element) el.getParentNode());
  14. kid = kid.getNextSibling();
  15. try {
  16. Element ar = (Element) myDoc.getDocumentElement().getFirstChild().getFirstChild();
  17. byte b[] = getElBytes(ar, "data");

代码示例来源:origin: stackoverflow.com

  1. XPathFactory xpathFactory = XPathFactory.newInstance();
  2. // XPath to find empty text nodes.
  3. XPathExpression xpathExp = xpathFactory.newXPath().compile(
  4. "//text()[normalize-space(.) = '']");
  5. NodeList emptyTextNodes = (NodeList)
  6. xpathExp.evaluate(doc, XPathConstants.NODESET);
  7. // Remove each empty text node from document.
  8. for (int i = 0; i < emptyTextNodes.getLength(); i++) {
  9. Node emptyTextNode = emptyTextNodes.item(i);
  10. emptyTextNode.getParentNode().removeChild(emptyTextNode);
  11. }

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

  1. /**
  2. * Method getStrFromNode
  3. *
  4. * @param xpathnode
  5. * @return the string for the node.
  6. */
  7. public static String getStrFromNode(Node xpathnode) {
  8. if (xpathnode.getNodeType() == Node.TEXT_NODE) {
  9. // we iterate over all siblings of the context node because eventually,
  10. // the text is "polluted" with pi's or comments
  11. StringBuilder sb = new StringBuilder();
  12. for (Node currentSibling = xpathnode.getParentNode().getFirstChild();
  13. currentSibling != null;
  14. currentSibling = currentSibling.getNextSibling()) {
  15. if (currentSibling.getNodeType() == Node.TEXT_NODE) {
  16. sb.append(((Text) currentSibling).getData());
  17. }
  18. }
  19. return sb.toString();
  20. } else if (xpathnode.getNodeType() == Node.ATTRIBUTE_NODE) {
  21. return xpathnode.getNodeValue();
  22. } else if (xpathnode.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) {
  23. return xpathnode.getNodeValue();
  24. }
  25. return null;
  26. }

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

  1. /**
  2. * Return the XPath parent of the supplied DOM node.
  3. * XPath has slightly different definition of parent than DOM does.
  4. * In particular, the parent of an attribute is not null.
  5. *
  6. * @param child the child node
  7. *
  8. * @return the parent of the specified node; or null if
  9. * the node does not have a parent
  10. */
  11. public Object getParentNode(Object child) {
  12. Node node = (Node) child;
  13. if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
  14. return ((Attr) node).getOwnerElement();
  15. }
  16. return node.getParentNode();
  17. }

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

  1. Node nextNode = pos.getFirstChild();
  2. break;
  3. nextNode = pos.getNextSibling();
  4. pos = pos.getParentNode();

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

  1. while (childA.getParentNode() != a)
  2. childA = childA.getParentNode();
  3. Node childZ = last;
  4. while (childZ.getParentNode() != a)
  5. childZ = childZ.getParentNode();
  6. Document doc = (a.getNodeType() == Node.DOCUMENT_NODE) ? (Document) a : a.getOwnerDocument();
  7. Element newElement = doc.createElementNS(doc.getDocumentElement().getNamespaceURI(), newElementName);
  8. Node helper;
  9. while (c != null && c != childZ) {
  10. helper = c.getNextSibling();

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

  1. /**
  2. * Analogous to the Node.insertBefore() method, insert a newNode after a refNode.
  3. *
  4. * @param newNode
  5. * new node
  6. * @param refNode
  7. * ref node
  8. * @throws DOMException
  9. * DOMException
  10. */
  11. public static void insertAfter(Node newNode, Node refNode) throws DOMException {
  12. Node parent = refNode.getParentNode();
  13. Node next = refNode.getNextSibling();
  14. if (next == null) {
  15. parent.appendChild(newNode);
  16. } else {
  17. parent.insertBefore(newNode, next);
  18. }
  19. }

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

  1. @Override
  2. public void postSign(Document document)
  3. throws MarshalException {
  4. // check for XAdES-BES
  5. NodeList nl = document.getElementsByTagNameNS(XADES_132_NS, "QualifyingProperties");
  6. if (nl.getLength() != 1) {
  7. throw new MarshalException("no XAdES-BES extension present");
  8. }
  9. QualifyingPropertiesType qualProps;
  10. try {
  11. qualProps = QualifyingPropertiesType.Factory.parse(nl.item(0), DEFAULT_XML_OPTIONS);
  12. } catch (XmlException e) {
  13. throw new MarshalException(e);
  14. }
  15. // create basic XML container structure
  16. UnsignedPropertiesType unsignedProps = qualProps.getUnsignedProperties();
  17. if (unsignedProps == null) {
  18. unsignedProps = qualProps.addNewUnsignedProperties();
  19. }
  20. UnsignedSignaturePropertiesType unsignedSigProps = unsignedProps.getUnsignedSignatureProperties();
  21. if (unsignedSigProps == null) {
  22. /* unsignedSigProps = */ unsignedProps.addNewUnsignedSignatureProperties();
  23. }
  24. Node n = document.importNode(qualProps.getDomNode().getFirstChild(), true);
  25. nl.item(0).getParentNode().replaceChild(n, nl.item(0));
  26. }
  27. }

代码示例来源:origin: nutzam/nutz

  1. protected List<Element> getChildNodesByTagName(Element element, String tagName) {
  2. List<Element> list = new ArrayList<Element>();
  3. NodeList nList = element.getElementsByTagName(tagName);
  4. if(nList.getLength() > 0) {
  5. for (int i = 0; i < nList.getLength(); i++) {
  6. Node node = nList.item(i);
  7. if(node.getParentNode().isSameNode(element) && node instanceof Element)
  8. list.add((Element) node);
  9. }
  10. }
  11. return list;
  12. }
  13. }

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

  1. Node p=n.getNextSibling();
  2. if(p==null)
  3. for(n=n.getParentNode();
  4. n!=null && ENTITY_REFERENCE_NODE == n.getNodeType();
  5. n=n.getParentNode())
  6. p=n.getNextSibling();
  7. if(p!=null)
  8. break;
  9. while(n!=null && ENTITY_REFERENCE_NODE == n.getNodeType())
  10. n=n.getFirstChild();
  11. else
  12. n=n.getNextSibling();
  13. int ntype=n.getNodeType();
  14. if(TEXT_NODE != ntype && CDATA_SECTION_NODE != ntype)
  15. n=null;

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

  1. /**
  2. * Get the XPath-model parent of a node. This version takes advantage
  3. * of the DOM Level 2 Attr.ownerElement() method; the base version we
  4. * would otherwise inherit is prepared to fall back on exhaustively
  5. * walking the document to find an Attr's parent.
  6. *
  7. * @param node Node to be examined
  8. *
  9. * @return the DOM parent of the input node, if there is one, or the
  10. * ownerElement if the input node is an Attr, or null if the node is
  11. * a Document, a DocumentFragment, or an orphan.
  12. */
  13. public static Node getParentOfNode(Node node)
  14. {
  15. Node parent=node.getParentNode();
  16. if(parent==null && (Node.ATTRIBUTE_NODE == node.getNodeType()) )
  17. parent=((Attr) node).getOwnerElement();
  18. return parent;
  19. }

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

  1. Node nextNode = pos.getFirstChild();
  2. break;
  3. nextNode = pos.getNextSibling();
  4. pos = pos.getParentNode();

代码示例来源:origin: com.rackspace.apache/xerces2-xsd11

  1. private Node getNextLogicalSibling(Node n) {
  2. Node next = n.getNextSibling();
  3. // If "n" has no following sibling and its parent is an entity reference node we
  4. // need to continue the search through the following siblings of the entity
  5. // reference as these are logically siblings of the given node.
  6. if (next == null) {
  7. Node parent = n.getParentNode();
  8. while (parent != null && parent.getNodeType() == Node.ENTITY_REFERENCE_NODE) {
  9. next = parent.getNextSibling();
  10. if (next != null) {
  11. break;
  12. }
  13. parent = parent.getParentNode();
  14. }
  15. }
  16. return next;
  17. } // getNextLogicalSibling(Node):Node

相关文章