English 中文(简体)
How do I check if a node has no siblings?
原标题:
  • 时间:2009-11-09 18:50:35
  •  标签:
  • java
  • xml

I have a org.w3c.dom.Node object.

I would like to see if it has any other siblings.

Here s what I have tried:

Node sibling = node.getNextSibling();
if(sibling == null)
    return true;
else
    return false;

HOWEVER, for some reason (possibly due to identation or line spaces in the source XML) I am not getting the expected result.

[Also

 node.getParentNode().getChildNodes().getLength() 

is giving a value higher higher than I would expect.]

I welcome your suggestions to improve this code.

EDIT

As suggested below it seems that blank nodes are thwarting my attempts to count the siblings.

The xml looks something like this:

  <a>
        <b>
              <c>I have this node:</c>
              <c>I want to know how many of these there are.</c>
              <c>This is another sibling.</c>
        </b>
        <b>
        </b>

  </a>

Starting from my node (the first <c></c> above), how do I find out the number of other siblings?

最佳回答

When you get child nodes from another node, you receive all direct children. This will include Element nodes, Text nodes, and Comment nodes. Most often, you ll only care about Element nodes. So you can check the result of getChildNodes to see if any are ELEMENT_NODE.

An example function for what you wanted to do:

public function isSingleChild(Node node) {
    boolean singleChild = true;
    NodeList siblings = node.getParentNode().getChildNodes();
    for(int i = 0, int len = siblings.getLength(); i < len; i++) {
        if(siblings.item(i).getNodeType() == Node.ELEMENT_NODE) {
            singleChild = false;
            break;
        }
    }

    return singleChild;
}

Just to see what each node type is like:

<div>
    <!-- Comment Node -->
    <p>Element node</p>
    a text node
</div>

Getting the div s childNodes would return 3 nodes, a comment node containing "Comment Node", an Element node of "P", and a Text node of "a text node".

问题回答

you could count the number of childnodes in the items parent after filtering out the whitespace nodes. (which you probably don t want, but are also probably messing up your expected result).

I can t put it in actual java real quick because i m not familiar enough with it, but it should be pretty straightforward.

I m not a Java guy, but in C# one way to test an element to see if it has siblings is to use an XPath query:

bool hasSiblings = elm.SelectNodes("../*").Count == 1;

The * selector finds only elements, which keeps you from having to remember that "node" can mean element, text node, processing instruction, or comment. (I d estimate that in 90% of cases, when someone talking about XML uses the word "node" they really mean "element".)

Thanks to all for your input.

Here is the code that I used in the end, it is based on seanmonstar s code.

public Boolean isSingleChild(Node node)
{
    int elementNodes = 0;
    NodeList siblings = node.getParentNode().getChildNodes();
    for(int x = 0 ; x < siblings.getLength(); x++)
    {
        if(siblings.item(x).getNodeType() == Node.ELEMENT_NODE)
        {
            elementNodes++;
        }
    }
    if(elementNodes == 1)
         return true;
    else
        return false;
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签