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

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

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

@Override
public int jjtGetChildIndex() {
  org.w3c.dom.Node parent = node.getParentNode();
  NodeList childNodes = parent.getChildNodes();
  for (int i = 0; i < childNodes.getLength(); i++) {
    if (node == childNodes.item(i)) {
      return i;
    }
  }
  throw new IllegalStateException("This node is not a child of its parent: " + node);
}

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

/**
 * Get the XPath-model parent of a node.  This version takes advantage
 * of the DOM Level 2 Attr.ownerElement() method; the base version we
 * would otherwise inherit is prepared to fall back on exhaustively
 * walking the document to find an Attr's parent.
 *
 * @param node Node to be examined
 *
 * @return the DOM parent of the input node, if there is one, or the
 * ownerElement if the input node is an Attr, or null if the node is
 * a Document, a DocumentFragment, or an orphan.
 */
public static Node getParentOfNode(Node node)
{
    Node parent=node.getParentNode();
    if(parent==null && (Node.ATTRIBUTE_NODE == node.getNodeType()) )
     parent=((Attr) node).getOwnerElement();
    return parent;
}

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

protected Node getNextNode (Node node) {
    if (node == null) {
      return null;
    }
    else {
      Node n = node.getFirstChild();
      if (n == null) n = node.getNextSibling();
      if (n == null) return getFirstNode(node.getParentNode());
      else return n;
    }
  }
};

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

private void removeEmptyNodes( NodeList nodes ) {
  for ( int i = 0; i < nodes.getLength(); i++ ) {
   Node node = nodes.item( i );

   // Process the tree bottom-up
   if ( node.hasChildNodes() ) {
    removeEmptyNodes( node.getChildNodes() );
   }

   boolean nodeIsEmpty =
     node.getNodeType() == Node.ELEMENT_NODE && !node.hasAttributes() && !node.hasChildNodes()
       && node.getTextContent().length() == 0;

   if ( nodeIsEmpty ) {
    // We shifted elements left, do not increment counter
    node.getParentNode().removeChild( node );
    i--;
   }
  }
 }
}

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

Node p=n.getNextSibling();
if(p==null)
    for(n=n.getParentNode();
        n!=null && ENTITY_REFERENCE_NODE == n.getNodeType();
        n=n.getParentNode())
        p=n.getNextSibling();
        if(p!=null)
            break;
while(n!=null && ENTITY_REFERENCE_NODE == n.getNodeType())
        n=n.getFirstChild();
    else
        n=n.getNextSibling();
    int ntype=n.getNodeType();
    if(TEXT_NODE != ntype && CDATA_SECTION_NODE != ntype)
        n=null;

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

while (childA.getParentNode() != a)
  childA = childA.getParentNode();
Node childZ = last;
while (childZ.getParentNode() != a)
  childZ = childZ.getParentNode();
Document doc = (a.getNodeType() == Node.DOCUMENT_NODE) ? (Document) a : a.getOwnerDocument();
Element newElement = doc.createElementNS(doc.getDocumentElement().getNamespaceURI(), newElementName);
Node helper;
while (c != null && c != childZ) {
  helper = c.getNextSibling();

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

/**
 * Analogous to the Node.insertBefore() method, insert a newNode after a refNode.
 * 
 * @param newNode
 *            new node
 * @param refNode
 *            ref node
 * @throws DOMException
 *             DOMException
 */
public static void insertAfter(Node newNode, Node refNode) throws DOMException {
  Node parent = refNode.getParentNode();
  Node next = refNode.getNextSibling();
  if (next == null) {
    parent.appendChild(newNode);
  } else {
    parent.insertBefore(newNode, next);
  }
}

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

myDoc = builder.parse(is);
  NodeList nl = myDoc.getElementsByTagName("url");
  for (int i = 0; i < nl.getLength(); i++) {
    Node nd = nl.item(i);
    if(nd instanceof Element) {
      Element el = (Element) nd;
      for (int j = 0; j < nodes.getLength(); j++) {
        Node node = nodes.item(j);
        if (node instanceof Text) {
          String dt = ((Text)node).getData();
          dt = new String(b);
          urlList.add(dt);
          urlNodes.add((Element) el.getParentNode());
    kid = kid.getNextSibling();
try {
  Element ar = (Element) myDoc.getDocumentElement().getFirstChild().getFirstChild();
  byte b[] = getElBytes(ar, "data");

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

XPathFactory xpathFactory = XPathFactory.newInstance();
// XPath to find empty text nodes.
XPathExpression xpathExp = xpathFactory.newXPath().compile(
    "//text()[normalize-space(.) = '']");  
NodeList emptyTextNodes = (NodeList) 
    xpathExp.evaluate(doc, XPathConstants.NODESET);

// Remove each empty text node from document.
for (int i = 0; i < emptyTextNodes.getLength(); i++) {
  Node emptyTextNode = emptyTextNodes.item(i);
  emptyTextNode.getParentNode().removeChild(emptyTextNode);
}

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

/**
 * Method getStrFromNode
 *
 * @param xpathnode
 * @return the string for the node.
 */
public static String getStrFromNode(Node xpathnode) {
  if (xpathnode.getNodeType() == Node.TEXT_NODE) {
    // we iterate over all siblings of the context node because eventually,
    // the text is "polluted" with pi's or comments
    StringBuilder sb = new StringBuilder();
    for (Node currentSibling = xpathnode.getParentNode().getFirstChild();
      currentSibling != null;
      currentSibling = currentSibling.getNextSibling()) {
      if (currentSibling.getNodeType() == Node.TEXT_NODE) {
        sb.append(((Text) currentSibling).getData());
      }
    }
    return sb.toString();
  } else if (xpathnode.getNodeType() == Node.ATTRIBUTE_NODE) {
    return xpathnode.getNodeValue();
  } else if (xpathnode.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) {
    return xpathnode.getNodeValue();
  }
  return null;
}

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

/** 
 * Return the XPath parent of the supplied DOM node.
 * XPath has slightly different definition of parent than DOM does.
 * In particular, the parent of an attribute is not null.
 * 
 * @param child the child node
 * 
 * @return the parent of the specified node; or null if
 *     the node does not have a parent
 */
public Object getParentNode(Object child) {
  Node node = (Node) child;
  if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
    return ((Attr) node).getOwnerElement();
  }
  return node.getParentNode();
}

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

Node nextNode = pos.getFirstChild();
  break;
 nextNode = pos.getNextSibling();
  pos = pos.getParentNode();

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

while (childA.getParentNode() != a)
  childA = childA.getParentNode();
Node childZ = last;
while (childZ.getParentNode() != a)
  childZ = childZ.getParentNode();
Document doc = (a.getNodeType() == Node.DOCUMENT_NODE) ? (Document) a : a.getOwnerDocument();
Element newElement = doc.createElementNS(doc.getDocumentElement().getNamespaceURI(), newElementName);
Node helper;
while (c != null && c != childZ) {
  helper = c.getNextSibling();

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

/**
 * Analogous to the Node.insertBefore() method, insert a newNode after a refNode.
 * 
 * @param newNode
 *            new node
 * @param refNode
 *            ref node
 * @throws DOMException
 *             DOMException
 */
public static void insertAfter(Node newNode, Node refNode) throws DOMException {
  Node parent = refNode.getParentNode();
  Node next = refNode.getNextSibling();
  if (next == null) {
    parent.appendChild(newNode);
  } else {
    parent.insertBefore(newNode, next);
  }
}

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

@Override
  public void postSign(Document document)
  throws MarshalException {
    // check for XAdES-BES
    NodeList nl = document.getElementsByTagNameNS(XADES_132_NS, "QualifyingProperties");
    if (nl.getLength() != 1) {
      throw new MarshalException("no XAdES-BES extension present");
    }

    QualifyingPropertiesType qualProps;
    try {
      qualProps = QualifyingPropertiesType.Factory.parse(nl.item(0), DEFAULT_XML_OPTIONS);
    } catch (XmlException e) {
      throw new MarshalException(e);
    }
    
    // create basic XML container structure
    UnsignedPropertiesType unsignedProps = qualProps.getUnsignedProperties();
    if (unsignedProps == null) {
      unsignedProps = qualProps.addNewUnsignedProperties();
    }
    UnsignedSignaturePropertiesType unsignedSigProps = unsignedProps.getUnsignedSignatureProperties();
    if (unsignedSigProps == null) {
      /* unsignedSigProps = */ unsignedProps.addNewUnsignedSignatureProperties();
    }
    
    Node n = document.importNode(qualProps.getDomNode().getFirstChild(), true);
    nl.item(0).getParentNode().replaceChild(n, nl.item(0));
  }
}

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

protected List<Element> getChildNodesByTagName(Element element, String tagName) {
    List<Element> list = new ArrayList<Element>();
    NodeList nList = element.getElementsByTagName(tagName);
    if(nList.getLength() > 0) {
      for (int i = 0; i < nList.getLength(); i++) {
        Node node = nList.item(i);
        if(node.getParentNode().isSameNode(element) && node instanceof Element)
          list.add((Element) node);
      }
    }
    return list;
  }
}

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

Node p=n.getNextSibling();
if(p==null)
    for(n=n.getParentNode();
        n!=null && ENTITY_REFERENCE_NODE == n.getNodeType();
        n=n.getParentNode())
        p=n.getNextSibling();
        if(p!=null)
            break;
while(n!=null && ENTITY_REFERENCE_NODE == n.getNodeType())
        n=n.getFirstChild();
    else
        n=n.getNextSibling();
    int ntype=n.getNodeType();
    if(TEXT_NODE != ntype && CDATA_SECTION_NODE != ntype)
        n=null;

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

/**
 * Get the XPath-model parent of a node.  This version takes advantage
 * of the DOM Level 2 Attr.ownerElement() method; the base version we
 * would otherwise inherit is prepared to fall back on exhaustively
 * walking the document to find an Attr's parent.
 *
 * @param node Node to be examined
 *
 * @return the DOM parent of the input node, if there is one, or the
 * ownerElement if the input node is an Attr, or null if the node is
 * a Document, a DocumentFragment, or an orphan.
 */
public static Node getParentOfNode(Node node)
{
    Node parent=node.getParentNode();
    if(parent==null && (Node.ATTRIBUTE_NODE == node.getNodeType()) )
     parent=((Attr) node).getOwnerElement();
    return parent;
}

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

Node nextNode = pos.getFirstChild();
  break;
 nextNode = pos.getNextSibling();
  pos = pos.getParentNode();

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

private Node getNextLogicalSibling(Node n) {
  Node next = n.getNextSibling();
  // If "n" has no following sibling and its parent is an entity reference node we 
  // need to continue the search through the following siblings of the entity 
  // reference as these are logically siblings of the given node.
  if (next == null) {
    Node parent = n.getParentNode();
    while (parent != null && parent.getNodeType() == Node.ENTITY_REFERENCE_NODE) {
      next = parent.getNextSibling();
      if (next != null) {
        break;
      }
      parent = parent.getParentNode();
    }
  }
  return next;
} // getNextLogicalSibling(Node):Node

相关文章