北 华 航 天 工 业 学 院
实验报告
课 程 名 称 : XML
实 验 内 容 : 基于DOM的解析器
作者所在系部: 计算机科学与工程系
作者所在专业: 计算机科学与技术
作者所在班级:
作 者 姓 名 :
作 者 学 号 :
指导教师姓名:
北华航天工业学院教务处制
实验五 基于DOM的解析器
一、实验目的
DOM(Document Object Model,文档对象模型)是W3C制定的一套规范标准,即规定了解析文件的接口
DOM规范的核心是树模型。对于解析XML文件的解析器,解析器通过读入XML文件在内存中建立一个树,也就是说XML文件的标记、标记的文本内容、实体等都会和内存中树的某个节点相对应。
1.掌握DO解析器的工作原理;
2.掌握节点的类型;
3.熟练掌握Element、Text、Document等节点的使用。
二、实验内容
使用DOM解析器读取存储学生基本信息的XML文件,练习Element、Text、Document等节点的使用。
三、实验步骤
1.使用实验二创建的XML文件,文件中包含2-3条学生(student)信息。
2.使用DOM,编写JAVA程序,读取XML文件中的数据,并显示到控制台上。
使用两种方法实现该功能:
1)使用getElementsByTagName()方法
2)使用getChildNodes()方法
3.属性值的读取使用两种
1)使用getAttribute方法
2)使用ATTR节点
//getChildNode.java
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class getChildNode {
public static void main(String args[])
{ try{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new File("Student.xml"));
Element root = document.getDocumentElement();
String rootname = root.getNodeName();
System.out.println("XML文件根节点的名字:"+rootname);
NodeList nodeList = root.getChildNodes();
int size = nodeList.getLength();
for(int k =0; k if(node.getNodeType()==Node.ELEMENT_NODE) { Element elementNode =(Element)node; String name = elementNode.getNodeName(); String id = elementNode.getAttribute("id"); String sex = elementNode.getAttribute("sex"); String content = elementNode.getTextContent(); System.out.print(name); System.out.print(" id="+id); System.out.println(" sex="+sex); System.out.println(content); } } } catch(Exception e){ System.out .println(e); } } } //getElement.java import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; 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; public class Student { public static void main(String args[]) { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder bulBuilder; try { bulBuilder = factory.newDocumentBuilder(); Document document = bulBuilder.parse(new File("Student.xml")); Element root = document.getDocumentElement(); String rootname = root.getNodeName(); System.out .println(rootname); NodeList nodeList = document.getElementsByTagName("学生"); int size = nodeList .getLength(); for(int k=0;k String name = node.getNodeName(); NamedNodeMap map = node.getAttributes(); String content = node.getTextContent(); System.out.print(name); for(int i=0;i String attName = attrNode.getName(); String attValue = attrNode.getValue(); System.out.print(" "+attName+"="+attValue+" "); } System.out .print(content); } } catch (Exception e) { e.printStackTrace();} } } 四、实验总结 1.实验中遇到的问题及解决 做getChildNode.java时,将NodeList nodeList = root.getChildNodes();写成NodeList nodeList = document.getChildNodes();输出结果: 2.本次实验学到的知识与能力:如DOM中主要用的方法及功能。 (1) getElementByTagName(String name) 返回一个NodeList对象,该对象由当前节点的Element类型子节点组成,这些子节点的名字由参数name指定。 (2) getDocumentElement()返回当前节点的Element子节点。 (3) getAttribute(String name)返回该节点中参数name指定的属性值,该属性值是此节点对应的XML标记中的属性值。 (4) getTextContent()返回当前节点及所有子孙节点中的文本内容。 (5) getChildNodes()返回一个由当前节点的所有子节点组成的NodeList对象。下载本文