Если вы можете использовать SAX-парсер, чем это легко, ваш ContentHandler
public class CH extends DefaultHandler
{
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
{
if (qName.equals("child2"))
{
// here you go do what you need here with the attributes
}
}
}
передает его парсеру, и все готово
как
import org.xml.sax.*;
public class TestParse {
public static void main(String[] args) {
try {
XMLReader parser =
org.xml.sax.helpers.XMLReaderFactory.createXMLReader();
// Create a new instance and register it with the parser
ContentHandler contentHandler = new CH();
parser.setContentHandler(contentHandler);
parser.parse("foo.xml"); // see javadoc you can give it a string or stream
} catch (Exception e) {
e.printStackTrace();
}
}
}