如果你可以使用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();
}
}
}