@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); }
/** * 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; }
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; } } };
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--; } } } }
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;
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();
/** * 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); } }
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");
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); }
/** * 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; }
/** * 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(); }
Node nextNode = pos.getFirstChild(); break; nextNode = pos.getNextSibling(); pos = pos.getParentNode();
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();
/** * 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); } }
@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)); } }
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; } }
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;
/** * 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; }
Node nextNode = pos.getFirstChild(); break; nextNode = pos.getNextSibling(); pos = pos.getParentNode();
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