Java DOM:如何获取多少个子元素

时间:2022-10-27 11:37:22

I have an XML Document:

我有一个XML文档:

<entities xmlns="urn:yahoo:cap">
    <entity score="0.988">
        <text end="4" endchar="4" start="0" startchar="0">Messi</text>
        <wiki_url>http://en.wikipedia.com/wiki/Lionel_Messi</wiki_url>
        <types>
            <type region="us">/person</type>
        </types>
    </entity>
</entities>

I have a TreeMap<String,String> data which stores the getTextContent() for both the "text" and "wiki_url" element. Some "entity"s will only have the "text" element (no "wiki_url") so i need a way of finding out when there is only the text element as the child and when there is a "wiki_url". I could use document.getElementByTag("text") & document.getElementByTag("wiki_url") but then I would lose the relationship between the text and the url.

我有一个TreeMap 数据,它存储“text”和“wiki_url”元素的getTextContent()。一些“实体”将只有“text”元素(没有“wiki_url”)所以我需要一种方法来找出当只有文本元素作为孩子时以及何时有“wiki_url”。我可以使用document.getElementByTag(“text”)&document.getElementByTag(“wiki_url”),但之后我将失去文本和url之间的关系。 ,string>

I'm trying to get the amount of elements within the "entity" element by using:

我试图通过使用以下方法获取“entity”元素中的元素数量:

NodeList entities = document.getElementsByTagName("entity"); //List of all the entity nodes
int nchild; //Number of children
System.out.println("Number of entities: "+ entities.getLength()); //Prints 1 as expected
nchild=entities.item(0).getChildNodes().getLength(); //Returns 7

However as shows above this returns 7 (which I don't understand, surely its 3 or 4 if you include the grandchild) I was then going to use the number of children to cycle through them all to check if getNodeName().equals("wiki_url") and save it to data if correct.

然而,正如上面的显示,这返回7(我不明白,如果你包括孙子肯定是3或4)我然后将使用子数来循环遍历它们以检查是否getNodeName()。equals( “wiki_url”)并将其保存到数据中,如果正确的话。

Why is it that i am getting the number of children as 7 when I can only count 3 children and 1 grandchild?

为什么当我只能算上3个孩子和1个孙子时,我的孩子数量为7?

1 个解决方案

#1


3  

The white-spaces following > of <entity score="0.988"> also count for nodes, similarly end of line chararcter between the tags are also parsed to nodes. If you are interested in a particular node with a name, add a helper method like below and call wherever you want.

之后的白色空间也计入节点,类似地,标签之间的行尾符号也被解析为节点。如果您对具有名称的特定节点感兴趣,请添加如下所示的辅助方法并在任何位置调用。

Node getChild(final NodeList list, final String name)
    {
        for (int i = 0; i < list.getLength(); i++)
        {
            final Node node = list.item(i);
            if (name.equals(node.getNodeName()))
            {
                return node;
            }
        }
        return null;
    }

and call

final NodeList childNodes = entities.item(0).getChildNodes();
final Node textNode = getChild(childNodes, "text");
final Node wikiUrlNode = getChild(childNodes, "wiki_url");

Normally when working with DOM, comeup with helper methods like above to simplify main processing logic.

通常在使用DOM时,请使用上面的辅助方法来简化主处理逻辑。

#1


3  

The white-spaces following > of <entity score="0.988"> also count for nodes, similarly end of line chararcter between the tags are also parsed to nodes. If you are interested in a particular node with a name, add a helper method like below and call wherever you want.

之后的白色空间也计入节点,类似地,标签之间的行尾符号也被解析为节点。如果您对具有名称的特定节点感兴趣,请添加如下所示的辅助方法并在任何位置调用。

Node getChild(final NodeList list, final String name)
    {
        for (int i = 0; i < list.getLength(); i++)
        {
            final Node node = list.item(i);
            if (name.equals(node.getNodeName()))
            {
                return node;
            }
        }
        return null;
    }

and call

final NodeList childNodes = entities.item(0).getChildNodes();
final Node textNode = getChild(childNodes, "text");
final Node wikiUrlNode = getChild(childNodes, "wiki_url");

Normally when working with DOM, comeup with helper methods like above to simplify main processing logic.

通常在使用DOM时,请使用上面的辅助方法来简化主处理逻辑。