以下是我要将xml文件中要获取的内容存到ArrayList集合中再取出来的代码,大家也可以不用弄到集合去,我弄到集合也是为了好玩而已,哈哈。。。
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class DOMDemo {
public static List<Student> getStudentList(){
ArrayList<Student> list = new ArrayList<Student>();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse("a.xml");
Element rootElement = document.getDocumentElement();
String schoolMaster = rootElement.getAttribute("schoolMaster");
Attr schoolName = rootElement.getAttributeNode("schoolName");
short nodeType = rootElement.getNodeType();
NamedNodeMap attributes2 = rootElement.getAttributes();
NodeList childNodes = rootElement.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
Node childNode = childNodes.item(i);
if(childNode!=null&&childNode.getNodeName().equals("student")){
String nodeName = childNode.getNodeName();
short nodeType2 = childNode.getNodeType();
NamedNodeMap attributes = childNode.getAttributes();
Node idNode = attributes.getNamedItem("id");
String idNodeName = idNode.getNodeName();
String idNodeValue = idNode.getNodeValue();
Node sexNode = attributes.getNamedItem("sex");
String sexNodeName = sexNode.getNodeName();
String sexNodeValue = sexNode.getNodeValue();
String textContent = childNode.getTextContent();
Student student = new Student(textContent, sexNodeValue, Integer.parseInt(idNodeValue));
list.add(student);
}
}
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
public static void test2(){
List<Student> studentList = getStudentList();
for (Iterator<Student> iterator = studentList.iterator(); iterator.hasNext();) {
Student student= iterator.next();
System.out.println("id:"+student.id+"\r\n"+"name:"+student.name+"\r\n"+"sex:"+student.sex+"\r\n"+"---------------------------");
}
}
public static void main(String[] args) {
test2();
}
}
class Student {
String name;
String sex;
int id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Student(String name, String sex, int id) {
this.name = name;
this.sex = sex;
this.id = id;
}
}
我的XML文件命名为a.xml,路径在项目目录下,我的xml文件内容为
<?xml version="1.0" encoding="UTF-8"?><students schoolMaster="xiaoxun" schoolName="xun">
I am content...
<student id="01" sex="女">axun</student>Hi!
<student id="02" sex="男">佳询</student>
Hello!
</students>
运行结果如下: