dom4j-java-如何获取root中具有特定元素名称的所有元素(父元素或子元素)

时间:2022-10-27 11:41:41

I want to get the number of elements in an xml, which have specific name eg. name="while")

我想获取xml中的元素数量,这些元素具有特定的名称,例如。名称=“同时”)

The problem is that if I use the following code- I only get top level elements that have this name--

问题是,如果我使用以下代码 - 我只获得具有此名称的*元素 -

       for ( Iterator i = root.elementIterator( "while" ); i.hasNext(); ) {
            Element foo = (Element) i.next();

But any lower level "while" element is not part of the iterator...

但任何较低级别的“while”元素都不是迭代器的一部分......

What is the most efficient way of obtaining all elements (whether top level or lower level) that have name="while"? Do I have to parse through all elements in document for this purpose?

获得name =“while”的所有元素(无论是*还是低级)的最有效方法是什么?为此,我是否必须解析文档中的所有元素?

3 个解决方案

#1


1  

You can use XPath for that using //while or the name() function and a wildcard node *: //*[name() = 'while']

您可以使用// while来使用// while或name()函数和通配符节点*:// * [name()='while']

List list = document.selectNodes("//*[name() = 'while']"); // or "//while"
int numberOfNodes = list.size();
for (Iterator iter = list.iterator(); iter.hasNext(); ) {
    // do something
}

#2


0  

Element Iterator works only for next level in xPath. To be able to parse all XML and get all Element you have to use some recursion. For example next code will return list of all nodes with "name"

Element Iterator仅适用于xPath中的下一级。为了能够解析所有XML并获取所有Element,您必须使用一些递归。例如,下一个代码将返回具有“名称”的所有节点的列表

public List<Element> getElementsByName(String name, Element parent, List<Element> elementList)
{
    if (elementList == null)
        elementList = new ArrayList<Element>();

    for ( Iterator i = parent.elementIterator(); i.hasNext(); ) {
        Element current = (Element) i.next();
        if (current.getName().equalsIgnoreCase(name))
        {
            elementList.add(current);
        }

        getElementsByName(name, current, elementList);
    }
    return elementList;
}

#3


0  

This regex worked for me:

这个正则表达式对我有用:

document.selectNodes(".//*")

#1


1  

You can use XPath for that using //while or the name() function and a wildcard node *: //*[name() = 'while']

您可以使用// while来使用// while或name()函数和通配符节点*:// * [name()='while']

List list = document.selectNodes("//*[name() = 'while']"); // or "//while"
int numberOfNodes = list.size();
for (Iterator iter = list.iterator(); iter.hasNext(); ) {
    // do something
}

#2


0  

Element Iterator works only for next level in xPath. To be able to parse all XML and get all Element you have to use some recursion. For example next code will return list of all nodes with "name"

Element Iterator仅适用于xPath中的下一级。为了能够解析所有XML并获取所有Element,您必须使用一些递归。例如,下一个代码将返回具有“名称”的所有节点的列表

public List<Element> getElementsByName(String name, Element parent, List<Element> elementList)
{
    if (elementList == null)
        elementList = new ArrayList<Element>();

    for ( Iterator i = parent.elementIterator(); i.hasNext(); ) {
        Element current = (Element) i.next();
        if (current.getName().equalsIgnoreCase(name))
        {
            elementList.add(current);
        }

        getElementsByName(name, current, elementList);
    }
    return elementList;
}

#3


0  

This regex worked for me:

这个正则表达式对我有用:

document.selectNodes(".//*")