资源描述
淮海工学院计算机工程学院
实验报告书
课 程 名: 《XML开发技术》
题 目: DOM解析
学 号:
姓 名:
评语:
成绩: 指导教师:
批阅时间: 年 月 日
《XML开发技术》实验报告
一、实验目的与要求
DOM(Document Object Model,文档对象模型)是W3C制定的一套规范标准,即规定了解析文件的接口
DOM规范的核心是树模型。对于解析XML文件的解析器,解析器通过读入XML文件在内存中建立一个树,也就是说XML文件的标记、标记的文本内容、实体等都会和内存中树的某个节点相对应。
1.掌握DO解析器的工作原理;
2.掌握节点的类型;
3.熟练掌握Element、Text、Document等节点的使用。
二、实验内容或题目
使用DOM解析器读取存储CD信息的XML文件,练习Element、Text、Document等节点的使用。
三、实验步骤与源程序
1.使用以前创建的XML文件,文件中包含多条CD信息、图书信息等。
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<size;k++)
{ Node node = nodeList.item(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<size;k++)
{ Node node =nodeList.item(k);
String name = node.getNodeName();
NamedNodeMap map = node.getAttributes();
String content = node.getTextContent();
System.out.print(name);
for(int i=0;i<map.getLength();i++)
{ Attr attrNode = (Attr)map.item(i);
String attName = attrNode.getName();
String attValue = attrNode.getValue();
System.out.print(" "+attName+"="+attValue+" "); }
System.out .print(content); }
} catch (Exception e) {
e.printStackTrace();} }
4. 阅读并调试如下代码,要求读懂程序。
<html>
<head>
<script type="text/javascript" src="/example/xdom/loadxmldoc.js"></script>
</head>
<body>
<script type="text/javascript">
xmlDoc=loadXMLDoc("/example/xdom/books.xml"); /*加载该xml文件*/
document.write(xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue); /* 输出第一个title标记 的第一个孩子节点的值*/
document.write("<br />");
/*输出换行*/
document.write(xmlDoc.getElementsByTagName("author")[0].childNodes[0].nodeValue);
/*输出author标记的第一个孩子节点的值*/
document.write("<br />");
document.write(xmlDoc.getElementsByTagName("year")[0].childNodes[0].nodeValue);
/*输出第一个year标记的第一个孩子节点的值*/
</script>
</body>
</html>
对程序加在注释,了解程序获取对象的方法。
四、测试数据与实验结果
1
2
五、结果分析与实验体会
本次实验,我知道了dom的一般用法,知道如何通过相应的方法访问xml文档中的数据,知道了如何通过编程实现这一功能。DOM是一种标准,并被广泛的实现,同时也内置到其他标准中。作为标准,它对数据的处理与编程语言无关。
5
展开阅读全文