下面列出了org.w3c.dom.Document#ELEMENT_NODE 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private Object getSiblingAt(Node node, int offset, boolean skipNonElement) {
Object result = null;
int indexInParent = getIndexInParent(node);
if (indexInParent != -1) {
int siblingIndex = indexInParent + offset;
NodeList siblings = node.getParentNode().getChildNodes();
// Get the sibling at the offset. In case it's out of
// bounds, return null.
while (result == null) {
if (siblingIndex >= 0 && siblingIndex < siblings.getLength()) {
Node sibling = siblings.item(siblingIndex);
if (skipNonElement && sibling.getNodeType() != Document.ELEMENT_NODE) {
// skip the non-element node
siblingIndex += (offset < 0) ? -1 : 1;
} else {
result = sibling;
break;
}
} else {
// out of bounds
break;
}
}
}
return result;
}
@SuppressWarnings("unused")
private static List<Node> getChildNodes(Node theNode, String childElName) {
LinkedList<Node> nodes = new LinkedList<Node>();
NodeList childs = theNode.getChildNodes();
for (int j = 0; j < childs.getLength(); j++) {
if (childs.item(j).getNodeType() == Document.ELEMENT_NODE && childs.item(j).getNodeName().equals(childElName)) {
nodes.add(childs.item(j));
}
}
return nodes;
}
protected static List<Node> getChildNodes(Node theNode, String childElName) {
LinkedList<Node> nodes = new LinkedList<Node>();
NodeList childs = theNode.getChildNodes();
for (int j = 0; j < childs.getLength(); j++) {
if (childs.item(j).getNodeType() == Document.ELEMENT_NODE && childs.item(j).getNodeName().equals(childElName)) {
nodes.add(childs.item(j));
}
}
return nodes;
}
static List<Node> getChildNodes(Node theNode, String childElName) {
LinkedList<Node> nodes = new LinkedList<Node>();
NodeList childs = theNode.getChildNodes();
for (int j = 0; j < childs.getLength(); j++) {
if (childs.item(j).getNodeType() == Document.ELEMENT_NODE && childs.item(j).getNodeName().equals(childElName)) {
nodes.add(childs.item(j));
}
}
return nodes;
}
/**
* Adds the nodes in actionArgs as children nodes to editNode.
*
* @param editNode The node on which children will be added.
* @param actionArgs The nodes to add.
*/
private void nodeAdd(Node editNode, NodeList actionArgs) {
// got through and add each new node to the root
for (int k = 0; k < actionArgs.getLength(); k++) {
Node n = actionArgs.item(k);
if (n.getNodeType() == Document.ELEMENT_NODE) {
editNode.appendChild(editNode.getOwnerDocument().adoptNode(n.cloneNode(true)));
}
}
}