需要apache.xerces,自己去下
import org.apache.xerces.parsers.DOMParser;
import org.xml.sax.SAXException;
import java.io.IOException;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.NamedNodeMap;public class DOMExample2{
public static void main(String args[]) throws IOException, SAXException{
DOMParser parser = new DOMParser();
parser.parse("test.xml");
Document dom = parser.getDocument();
NodeList nlist= dom.getElementsByTagName("LOCAL ");
for (int i=0;i<nlist.getLength();i++){
Node aNode = nlist.item(i);
System.out.println(aNode.getFirstChild().getNodeValue());
NamedNodeMap attributes = aNodde.getAttributes();
for (int a=0;a<attributes.getLength();a++){
Node theAttribute = attributes.item(a);
System.out.println(theAttribute.getNodeName() + " "+ theAttribute.getNodeValue());
}
}
}}
对。
Document doc = sb.build(new FileInputStream(filePath));
Element root = doc.getRootElement();
String name=root.getAttributeValue("name");