写一个简单的例子,对于您的这种情况
import java.util.Hashtable;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import java.io.*;
public class test {
public static void main(String[] args) {
Hashtable values = new Hashtable();
String fileName = "e:/test.xml";
DocumentBuilderFactory dbf =do
cumentBuilderFactory.newInstance();
dbf.setValidating(false);
dbf.setIgnoringComments(true);
DocumentBuilder db = null;
try {
db = dbf.newDocumentBuilder();
} catch (Exception e) {
e.printStackTrace();
}
Documentdo
c = null;
try {
doc = db.parse(new InputSource(new FileInputStream(fileName)));
} catch (Exception e) {
e.printStackTrace();
}
NodeList nl =do
c.getDocumentElement().getChildNodes();
for (int i=0 ;
i<nl.getLength() ;
i++) {
Node n = nl.item(i);
if (n.getLocalName()!=null)
if (n.getFirstChild() != null) values.put(n.getLocalName(), n.getFirstChild().getNodeValue());
else
values.put(n.getLocalName(), "");
}
System.out.println(values.get("Name"));
System.out.println(values.get("Price"));
}
}