实验六基于JAXP实现XML数据的解析.docx
《实验六基于JAXP实现XML数据的解析.docx》由会员分享,可在线阅读,更多相关《实验六基于JAXP实现XML数据的解析.docx(10页珍藏版)》请在冰豆网上搜索。
![实验六基于JAXP实现XML数据的解析.docx](https://file1.bdocx.com/fileroot1/2022-10/29/5e81bc77-a60f-4b8c-80bf-e6e18d7d170c/5e81bc77-a60f-4b8c-80bf-e6e18d7d170c1.gif)
实验六基于JAXP实现XML数据的解析
淮海工学院计算机工程学院
实验报告书
课程名:
XML开发技术
题目:
基于JAXP实现XML数据的解析
班级:
学号:
姓名:
一.实验目的
(1)通过本次实验,进一步掌握JAXP实现XML数据解析的接口与应用。
(2)通过本次实验,掌握对XML数据进行解析的方法。
(3)通过本次实验,进一步理解DOM接口与SAX接口解析XML数据的优缺点。
二.实验要求
三、实验内容
1、实现教材中代码9-2中的代码。
2、实现教材中习题9的第2题。
(选做)
四.实验结果
packagecom.xml;
importjava.io.File;
importjava.io.IOException;
importjavax.xml.parsers.DocumentBuilder;
importjavax.xml.parsers.DocumentBuilderFactory;
importjavax.xml.parsers.ParserConfigurationException;
importorg.w3c.dom.CDATASection;
importorg.w3c.dom.Comment;
importorg.w3c.dom.Document;
importorg.w3c.dom.Element;
importorg.w3c.dom.Node;
importorg.w3c.dom.NodeList;
importorg.w3c.dom.ProcessingInstruction;
importorg.xml.sax.SAXException;
publicclassReadXMLDemo01{
publicstaticvoidmain(String[]args)throwsParserConfigurationException,SAXException,IOException{
ReadXMLDemo01demo=newReadXMLDemo01();
Documentdoc=demo.parseXML("students.xml");
if(doc!
=null)
demo.readXMLReadAndPrint(doc);
}
publicDocumentparseXML(StringxmlFileName)throwsParserConfigurationException,SAXException,IOException{
DocumentBuilderFactorydbf=DocumentBuilderFactory.newInstance();
dbf.setIgnoringElementContentWhitespace(false);
Documentdoc=null;
DocumentBuilderdb=dbf.newDocumentBuilder();
doc=db.parse(newFile(xmlFileName));
returndoc;
}
publicvoidreadXMLReadAndPrint(Documentdoc){
System.out.println("
xmlversion=\"1.0\"encoding=\"UTF-8\"?
>");
NodeListallNode=doc.getChildNodes();
for(intm=0;mNodetemp=allNode.item(m);
if(temp.getNodeType()==Node.COMMENT_NODE){
Commentcom=(Comment)temp;
System.out.println("
--"+com.getData()+"-->");
}elseif(temp.getNodeType()==Node.PROCESSING_INSTRUCTION_NODE){
ProcessingInstructionpi=(ProcessingInstruction)temp;
System.out.println("
"+pi.getTarget()+""+pi.getData()+"?
>");
}elseif(temp.getNodeType()==Node.ELEMENT_NODE){
Stringroot=temp.getNodeName();
System.out.println("<"+root+">");
NodeListnodelist=temp.getChildNodes();
if(temp!
=null){
for(intn=0;nif(nodelist.item(n).getNodeType()==Node.ELEMENT_NODE){
Elemente=(Element)nodelist.item(n);
System.out.println("<"+e.getNodeName()+">");
NodenodeText=e.getFirstChild();
if(nodeText.getNodeType()==Node.CDATA_SECTION_NODE){
CDATASectioncdata=(CDATASection)nodeText;
System.out.println("
[CDATA["+cdata.getTextContent()+"]]>");
}else{
System.out.println(nodeText.getTextContent());
}
System.out.println(""+e.getNodeName()+">");
}elseif(temp.getNodeType()==Node.DOCUMENT_POSITION_DISCONNECTED){
System.out.println();
}
}
System.out.println(""+root+">");
}
}
}
}
}
2.Xml文档
xmlversion="1.0"encoding="UTF-8"?
>
--学生王宏Java成绩单-->
xsl-stylesheettype="text/xsl"href="xmlxslt.xslt"?
>
王宏
96
[CDATA[喜爱文学作品《三国演义》]]>
packagecom.xml;
importjava.io.File;
importjava.io.IOException;
importjavax.xml.parsers.DocumentBuilder;
importjavax.xml.parsers.DocumentBuilderFactory;
importjavax.xml.parsers.ParserConfigurationException;
importorg.w3c.dom.CDATASection;
importorg.w3c.dom.Comment;
importorg.w3c.dom.Document;
importorg.w3c.dom.Element;
importorg.w3c.dom.Node;
importorg.w3c.dom.NodeList;
importorg.w3c.dom.ProcessingInstruction;
importorg.xml.sax.SAXException;
publicclassReadXMLDemo01{
publicstaticvoidmain(String[]args)throwsParserConfigurationException,SAXException,IOException{
ReadXMLDemo01demo=newReadXMLDemo01();
Documentdoc=demo.parseXML("students.xml");
if(doc!
=null)
demo.readXMLReadAndPrint(doc);
}
publicDocumentparseXML(StringxmlFileName)throwsParserConfigurationException,SAXException,IOException{
DocumentBuilderFactorydbf=DocumentBuilderFactory.newInstance();
dbf.setIgnoringElementContentWhitespace(false);
Documentdoc=null;
DocumentBuilderdb=dbf.newDocumentBuilder();
doc=db.parse(newFile(xmlFileName));
returndoc;
}
publicvoidreadXMLReadAndPrint(Documentdoc){
System.out.println("
xmlversion=\"1.0\"encoding=\"UTF-8\"?
>");
NodeListallNode=doc.getChildNodes();
for(intm=0;mNodetemp=allNode.item(m);
if(temp.getNodeType()==Node.COMMENT_NODE){
Commentcom=(Comment)temp;
System.out.println("
--"+com.getData()+"-->");
}elseif(temp.getNodeType()==Node.PROCESSING_INSTRUCTION_NODE){
ProcessingInstructionpi=(ProcessingInstruction)temp;
System.out.println("
"+pi.getTarget()+""+pi.getData()+"?
>");
}elseif(temp.getNodeType()==Node.ELEMENT_NODE){
Stringroot=temp.getNodeName();
System.out.println("<