java 使用属性获取元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6093121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 14:17:58  来源:igfitidea点击:

Getting element using attribute

javaxml

提问by RAAAAM

I am parsing Xml using Java, i want to parse element with the help of attribute value.

我正在使用 Java 解析 Xml,我想在属性值的帮助下解析元素。

For example <tag1 att="recent">Data</tag1>

例如 <tag1 att="recent">Data</tag1>

In this i want to parse tag1 data using att value. I am new to java and xml. pls guide me.

在此我想使用 att 值解析 tag1 数据。我是java和xml的新手。请指导我。

回答by Buhake Sindi

There are ways to do this. You can use either, xPath (example), DOM Document or SAX Parser (example) to retrieve attribute value and tag elements.

有办法做到这一点。您可以使用 xPath(示例)、DOM 文档或 SAX 解析器(示例)来检索属性值和标记元素。

Here's related questions:

以下是相关问题:



This is a workaround to what you requested. I would never suggest that type of "hack", instead, use SAX instead (see example link).

这是您要求的解决方法。我永远不会建议使用这种类型的“hack”,而是使用 SAX(参见示例链接)。

public static Element getElementByAttributeValue(Node rootElement, String attributeValue) {

    if (rootElement != null && rootElement.hasChildNodes()) {
        NodeList nodeList = rootElement.getChildNodes();

        for (int i = 0; i < nodeList.getLength(); i++) {
            Node subNode = nodeList.item(i);

            if (subNode.hasAttributes()) {
                NamedNodeMap nnm = subNode.getAttributes();

                for (int j = 0; j < nnm.getLength(); j++) {
                    Node attrNode = nnm.item(j);

                    if (attrNode.getNodeType == Node.ATTRIBUTE_NODE) {
                        Attr attribute = (Attr) attrNode;

                        if (attributeValue.equals(attribute.getValue()) {
                            return (Element)subNode;
                        } else {
                            return getElementByAttributeValue(subNode, attributeValue);
                        }
                    }
                }               
            }
        }
    }

    return null;
}

PS:Code comment not provided. It's given as an exercise to the reader. :)

PS:未提供代码注释。它作为练习提供给读者。:)

回答by Ramesh PVK

This is java code to get the child node with given attribute name and value. Is this what you are looking for

这是用于获取具有给定属性名称和值的子节点的 Java 代码。这是你想要的

    public static Element getNodeWithAttribute(Node root, String attrName, String attrValue)
{
    NodeList nl = root.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node n = nl.item(i);
        if (n instanceof Element) {
            Element el = (Element) n;
            if (el.getAttribute(attrName).equals(attrValue)) {
                return el;
            }else{
       el =  getNodeWithAttribute(n, attrName, attrValue); //search recursively
       if(el != null){
        return el;
       }
    }
        }
    }
    return null;
}

回答by ??? ?????' ???? ?????

It is a old question but you may use HTMLUnit

这是一个老问题,但您可以使用 HTMLUnit

 HtmlAnchor a = (HtmlAnchor)ele;
 url = a.getHrefAttribute();