1> Dom
XML(Extensible Markup Language 可扩展标记语言)
<1>DOM:Document Object Model(文档对象模型)
DOM的特性:定义一组 Java 接口,基于对象,与语言和平台无关
将 XML 文档表示为树,在内存中解析和存储 XML 文档,允许随机访问文档的不同部
分
使用 DOM 解析 XML 文档:
获得一个新 DocumentBuilderFactory 实例
使用 DocumentBuilderFactory 构建 DocumentBuilder
使用 DocumentBuilder的 parse( ) 方法解析文件
将已解析的文档存储在 Document 对象中
使用 getElementsByTagName( ) 方法获得元素
DOM的缺点:须将整个文档存储在内存中
代码如下:try {
//获取文件路径
//String filePath = request.getSession().getServletContext().getRealPath("/WEB-INF/xmldata/staticdata.xml");
//构造File对象
File f = new File("staticdata.xml");
//获取文档解析器工厂对象
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
//通过解析器工厂生成文档解析器对象
DocumentBuilder builder = factory.newDocumentBuilder();
//解析文件并返回Document对象
Document doc = builder.parse(f);
//获取XML文件中所有的"attr"节点列表
NodeList nl = doc.getElementsByTagName("attr");
Node attrcode = nl.item(0).getAttributes().getNamedItem("code");
Node attrname = nl.item(0).getAttributes().getNamedItem("name");
System.out.println(attrcode.getNodeValue()+","+attrname.getNodeValue());
System.out.println();
NodeList items = doc.getElementsByTagName("item");
for(int i=0;i<items.getLength();i++){
System.out.print("code:");
String code = items.item(i).getAttributes().getNamedItem("code").getNodeValue();
String name = items.item(i).getAttributes().getNamedItem("name").getNodeValue();
System.out.print(code);
System.out.println(",name:"+name);
}
} catch (Exception e) {
e.printStackTrace();
}
2> Sax(由于实际项目中很少使用,只作了解)
使用 SAX 解析 XML 文档的步骤:
创建 SAXParserFactory 的实例
创建 SAXParser 的实例
创建 SAXParserHandler 类
使用 parse() 方法解析 XML 文档
SAX 的缺点:
不能对文档进行随机访问
只读
只遍历文档一次
实例代码如下:
Stack tags=new Stack();
String name;
String address;
String tel;
String fax;
String email;
public void endDocument() throws SAXException {
System.out.println("------Parse End--------");
}
public void startDocument() throws SAXException {
System.out.println("------Parse Begin--------");
}
public void startElement(String p0, String p1, String p2, Attributes p3) throws SAXException {
tags.push(p2);
}
public void endElement(String p0, String p1, String p2) throws SAXException {
tags.pop();
if (p2.equals("PERSON")) printout();
}
public void characters(char[] p0, int p1, int p2) throws SAXException {
String tag=(String) tags.peek();
if (tag.equals("NAME")) name=new String(p0,p1,p2);
else if (tag.equals("ADDRESS")) address=new String(p0,p1,p2);
else if (tag.equals("TEL")) tel=new String(p0,p1,p2);
else if (tag.equals("FAX")) fax=new String(p0,p1,p2);
else if (tag.equals("EMAIL")) email=new String(p0,p1,p2);
}
private void printout(){
System.out.print("Name: ");
System.out.println(name);
System.out.print("Address: ");
System.out.println(address);
System.out.print("Tel: ");
System.out.println(tel);
System.out.print("Fax: ");
System.out.println(fax);
System.out.print("Email: ");
System.out.println(email);
System.out.println();
}
static public void main(String[] args) {
String filename = "candidate.xml";
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser saxParser=null;
try {
saxParser = spf.newSAXParser();
} catch (Exception e) {
System.out.println(e);
System.exit(1);
}
try {
saxParser.parse(new File(filename),new SaxTest());
} catch (SAXException se) {
System.out.println(se.getMessage());
System.exit(1);