三种方法进行解析
下面请看代码块
1.第一种方法( * 使用dom解析xml)
//实例化DocumentBuilderFactory
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
//通过dbf获取DocumentBuilder
DocumentBuilder db=dbf.newDocumentBuilder();
//解析
Document d=db.parse(new File("students.xml"));
//获取根节点students
Element students=d.getDocumentElement();
//获取所有的student根节点
NodeList node=students.getElementsByTagName("student");
for (int i = 0; i < node.getLength(); i++) {
Element student=(Element) node.item(i);
//获取属性
String sid=student.getAttribute("sid");
String sname=student.getAttribute("sname");
//获取student下面的sage、ssex
Element ele_sage=(Element) student.getElementsByTagName("sage").item(0);
Element ele_ssex=(Element) student.getElementsByTagName("ssex").item(0);
//设置值
String sage=ele_sage.getTextContent();
String ssex=ele_ssex.getTextContent();
System.out.println(sid+" "+sname+" "+sage+" "+ssex);
}
2.第二种dom4j需要导两个jar包
//实例化SAXReader
SAXReader sax=new SAXReader();
//解读
Document d=sax.read(new File("students.xml"));
//获取根节点
Element root=d.getRootElement();
//获取所有的student根节点
List<Element> student=root.elements("student");
for (Element element : student) {
//获取属性
String sid=element.attributeValue("sid");
String sname=element.attributeValue("sname");
//子标签
Element ele_sage=element.element("sage");
Element ele_ssex=element.element("ssex");
//获值
String sage=ele_sage.getTextTrim();
String ssex=ele_ssex.getTextTrim();
System.out.println(sid+" "+sname+" "+sage+" "+ssex);
3.第三种sax
//实例化SAXParserFactory
SAXParserFactory spf=SAXParserFactory.newInstance();
//通过spf获取SAXParser
SAXParser sp=spf.newSAXParser();
//开始解析
sp.parse(new File("students.xml"), new DefaultHandler(){
//定义一个当前的标签名字
String currentTagName=null;
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
super.characters(ch, start, length);
//获取子标签
if("sage".equals(currentTagName)){
String sage=new String(ch, start, length);
System.out.println(sage);
}else if("ssex".equals(currentTagName)){
String ssex=new String(ch, start, length);
System.out.println(ssex);
}
}
@Override
public void endElement(String uri, String localName,
String qName) throws SAXException {
super.endElement(uri, localName, qName);
currentTagName=null;
}
@Override
public void startElement(String uri, String localName,
String qName, Attributes attributes)
throws SAXException {
super.startElement(uri, localName, qName, attributes);
currentTagName= qName;
if("student".equals(qName)){
//获取属性
String sid=attributes.getValue(0);
String sname=attributes.getValue(1);
System.out.println(sid+" "+sname);
}
}
});
1.第一种方法( * 使用dom解析xml)
//实例化DocumentBuilderFactory
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
//通过dbf获取DocumentBuilder
DocumentBuilder db=dbf.newDocumentBuilder();
//解析
Document d=db.parse(new File("students.xml"));
//获取根节点students
Element students=d.getDocumentElement();
//获取所有的student根节点
NodeList node=students.getElementsByTagName("student");
for (int i = 0; i < node.getLength(); i++) {
Element student=(Element) node.item(i);
//获取属性
String sid=student.getAttribute("sid");
String sname=student.getAttribute("sname");
//获取student下面的sage、ssex
Element ele_sage=(Element) student.getElementsByTagName("sage").item(0);
Element ele_ssex=(Element) student.getElementsByTagName("ssex").item(0);
//设置值
String sage=ele_sage.getTextContent();
String ssex=ele_ssex.getTextContent();
System.out.println(sid+" "+sname+" "+sage+" "+ssex);
}
2.第二种dom4j需要导两个jar包
//实例化SAXReader
SAXReader sax=new SAXReader();
//解读
Document d=sax.read(new File("students.xml"));
//获取根节点
Element root=d.getRootElement();
//获取所有的student根节点
List<Element> student=root.elements("student");
for (Element element : student) {
//获取属性
String sid=element.attributeValue("sid");
String sname=element.attributeValue("sname");
//子标签
Element ele_sage=element.element("sage");
Element ele_ssex=element.element("ssex");
//获值
String sage=ele_sage.getTextTrim();
String ssex=ele_ssex.getTextTrim();
System.out.println(sid+" "+sname+" "+sage+" "+ssex);
3.第三种sax
//实例化SAXParserFactory
SAXParserFactory spf=SAXParserFactory.newInstance();
//通过spf获取SAXParser
SAXParser sp=spf.newSAXParser();
//开始解析
sp.parse(new File("students.xml"), new DefaultHandler(){
//定义一个当前的标签名字
String currentTagName=null;
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
super.characters(ch, start, length);
//获取子标签
if("sage".equals(currentTagName)){
String sage=new String(ch, start, length);
System.out.println(sage);
}else if("ssex".equals(currentTagName)){
String ssex=new String(ch, start, length);
System.out.println(ssex);
}
}
@Override
public void endElement(String uri, String localName,
String qName) throws SAXException {
super.endElement(uri, localName, qName);
currentTagName=null;
}
@Override
public void startElement(String uri, String localName,
String qName, Attributes attributes)
throws SAXException {
super.startElement(uri, localName, qName, attributes);
currentTagName= qName;
if("student".equals(qName)){
//获取属性
String sid=attributes.getValue(0);
String sname=attributes.getValue(1);
System.out.println(sid+" "+sname);
}
}
});