<?xml version=1.0” encoding=UTF-8”?>java:
<联系方式>
<人 id=1”>
<名称>编码器
<名字>爪哇岛
<移动>054124587
<电子邮件>codeurjava@gmail.com
人>
<人员 id=2”>
<名称>编码器
<名字>C++(英语:C++)
<移动>062148795
<电子邮件>codeurcplusplus@gmail.com
人>
联系人>
import javax.xml.parsers.SAXParser;执行:
导入 javax.xml.parsers.SAXParserFactory;
导入 org.xml.sax.Attributes;
导入 org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class ReadFileXML{
public static void main(String argv[]) {
try {
//获取 sax parser
SAXParserFactory spfactory = SAXParserFactory.newInstance();
//获取parser
SAXParser saxParser = spfactory.newSAXParser();
/*所有三个方法都在 DefaltHandler 中声明 /> corp
DefaultHandler handler = new DefaultHandler() {
boolean bnom = false;
布尔值 bprefirst name = false;
布尔值 bmobile = false;
布尔值 bemail = false;
/*每当解析器遇到开始标记 '<<' */
开始标记 '' */br /> public void startElement(String uri, String localName,
String qName,Attributes attributes) throws SAXException{
if (qName.equalsIgnoreCase(name”)) {
bnom = true;
}
if (qName.equalsIgnoreCase(firstname”)) {
bfirstname = true;
}
if (qName.equalsIgnoreCase(mobile”)) {
bmobile = true;
}
if (qName.equalsIgnoreCase(email”)) {
bemail = true;
}
}
/*每当解析器遇到
结束标记 '>' */
public void endElement(String uri, String localName,
String qName) 抛出 SAXException {
if (qName.equalsIgnoreCase(name”)) {
bnom = false;
}
if (qName.equalsIgnoreCase(firstname”)) {
bfirstname = false;
}
if (qName.equalsIgnoreCase(mobile”)) {
bmobile = false;
}
if (qName.equalsIgnoreCase(email”)) {
bemail = false;
}
}
/*打印存储在 '<' 和 '>' 之间的数据 */
public void characters(char ch[], int start,
int length) throws SAXException {
if (bnom) {
System.out.println(Name: ” +
new String(ch, 开始,长度));
bnom = 假;
}
if (bprename) {
System.out.println(名字:” +
new String(ch, start, length));
b名字 = false;
}
if (bmobile) {
System.out.println(移动 : ” +
new String(ch, start, length));
bmobile = 假;
}
if (bemail) {
System.out.println(电子邮件 : ” +
new String(ch, start, length));
bemail = 假;
}
}
};
saxParser.parse(exemple.xml”, 处理程序);
} catch (异常 e) {
e.printStackTrace();
}
}
}
姓氏:coder
名字:java
手机:054124587
电子邮件:codeurjava@gmail.com
姓氏:coder
名字: c++
移动:062148795
电子邮件:codeurcplusplus@gmail.com
Please disable your ad blocker and refresh the window to use this website.