Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

For example <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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
169 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

    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;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...