3

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.

StKiller
  • 7,631
  • 10
  • 43
  • 56
RAAAAM
  • 3,378
  • 19
  • 59
  • 108

3 Answers3

4

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.

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).

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. :)

Community
  • 1
  • 1
Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
  • Thanks for your replies. i want to parse the element which is having attributes. do you know how to do this..??? i am using DOM concept – RAAAAM May 23 '11 at 05:33
  • Yes, if you have an `org.w3c.dom.Element element`, you can say, `String attribute = element.getAttribute("att");`. – Buhake Sindi May 23 '11 at 05:51
  • Thanks again, ~ String attribute = element.getAttribute("att");~ from this i only get attribute(ie)whatever i given in attribute i can get it through String, but i want to parse the element with the help of attribute. – RAAAAM May 23 '11 at 05:56
  • @HariRam, for this, you will need to use SAX Parser instead. DOM parser generates a DOM Document so getting an element and/or attribute requires you to know the name of the element and/or attribute respectively. SAX doesn't. It's an iterative parser and you implement how it should translate. – Buhake Sindi May 23 '11 at 06:46
  • Okay, but i dont know Sax parser. can you guide me for this issue. – RAAAAM May 23 '11 at 07:04
  • @HariRam, should I guide you in terms of SAX, xPath or DOM? – Buhake Sindi May 23 '11 at 09:37
  • @HariRam, I added a code (**untested**) so feel free to hack away. :) – Buhake Sindi May 23 '11 at 10:06
  • Why is this in any form a hack? – Enerccio Oct 05 '15 at 07:23
4

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;
}
Ramesh PVK
  • 15,200
  • 2
  • 46
  • 50
0

It is a old question but you may use HTMLUnit

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