ImageVerifierCode 换一换
格式:DOCX , 页数:19 ,大小:19.04KB ,
资源ID:7282419      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/7282419.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(XML基础教程2版第第4章DOM解析器代码.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

XML基础教程2版第第4章DOM解析器代码.docx

1、XML基础教程2版第第4章DOM解析器代码第4章 DOM解析器4.1 DOM解析器例子1example4_1.xml 张三 李四 一等奖学金 JAXPOne.javaimport org.w3c.dom.*; import javax.xml.parsers.*;import java.io.*;public class JAXPOne public static void main(String args) try DocumentBuilderFactory factory= DocumentBuilderFactory. newInstance(); DocumentBuilder do

2、mPaser=factory.newDocumentBuilder(); Document document=domPaser.parse(new File(example4_1.xml) ; Element root=document.getDocumentElement(); String rootName=root.getNodeName(); System.out.println(XML文件根节点的名字:+rootName); NodeList nodelist=root.getElementsByTagName(姓名); int size=nodelist.getLength();

3、for(int k=0;ksize;k+) Node node=nodelist.item(k); String name=node.getNodeName(); String content=node.getTextContent(); System.out.print(name); System.out.println(:+content); catch(Exception e) System.out.println(e); 4.2 节点的类型例子2example4_2.xml 张三 25岁 3190元/月 李四 35岁 4320元/月 JAXPTwo.javaimport org.w3c

4、.dom.*; import javax.xml.parsers.*;import java.io.*;public class JAXPTwo public static void main(String args) try DocumentBuilderFactory factory= DocumentBuilderFactory.newInstance(); DocumentBuilder domParser= factory.newDocumentBuilder(); Document document=domParser.parse(new File(example6_2.xml)

5、; NodeList nodeList=document.getChildNodes(); output(nodeList); catch(Exception e) System.out.println(e); public static void output(NodeList nodeList) /output是一个递归方法 int size=nodeList.getLength(); for(int k=0;ksize;k+) Node node=nodeList.item(k); if(node.getNodeType()=Node.TEXT_NODE) Text textNode=(

6、Text)node; String content=textNode.getWholeText(); System.out.print(content); if(node.getNodeType()=Node.ELEMENT_NODE) Element elementNode=(Element)node; String name=elementNode.getNodeName(); System.out.print(name+:); NodeList nodes=elementNode.getChildNodes(); output(nodes); /递归调用 4.4 Element节点例子3

7、example4_3.xml 电视机 雅格尔西装 东北大米 JAXPThree.javaimport org.w3c.dom.*; import javax.xml.parsers.*;import java.io.*;public class JAXPThree public static void main(String args) try DocumentBuilderFactory factory= DocumentBuilderFactory. newInstance(); DocumentBuilder domPaser=factory.newDocumentBuilder();

8、Document document=domPaser.parse(new File(example6_3.xml) ; Element root=document.getDocumentElement() ; NodeList nodeList=root.getChildNodes(); int size=nodeList.getLength(); for(int k=0;ksize;k+) Node node=nodeList.item(k); if(node.getNodeType()=Node.ELEMENT_NODE) Element elementNode=(Element)node

9、; String name=elementNode.getNodeName(); String id=elementNode.getAttribute(分类); String content=elementNode.getTextContent(); System.out.print(name); System.out.print(+id+); System.out.println(:+content); catch(Exception e) 4.5 Text节点例子4example4_4.xml 张大山 大连市 0411-123456 刘翠花 北京市 010-654321 JAXPFour.

10、javaimport org.w3c.dom.*; import javax.xml.parsers.*;import java.io.*;public class JAXPFour public static void main(String args) GiveData give=new GiveData(); try DocumentBuilderFactory factory= DocumentBuilderFactory.newInstance(); DocumentBuilder domPaser=factory.newDocumentBuilder(); Document doc

11、ument=domPaser.parse(new File(example4_4.xml) ; Element root=document.getDocumentElement() ; NodeList nodeList=root.getChildNodes(); give.output(nodeList); System.out.println(一共有+give.m+个Text节点); catch(Exception e) class GiveData int m=0; public void output(NodeList nodeList) /这是一个递归方法 int size=node

12、List.getLength(); for(int k=0;ksize;k+) Node node=nodeList.item(k); if(node.getNodeType()=Node.TEXT_NODE) Text textNode=(Text)node; String content=textNode.getWholeText(); m+; System.out.print(content); if(node.getNodeType()=Node.ELEMENT_NODE) Element elementNode=(Element)node; String name=elementNo

13、de.getNodeName(); System.out.print(name+:); NodeList nodes=elementNode.getChildNodes(); output(nodes); 例子5example4_5.xml 海尔电视机 5238 星海电视机 3660 长虹电视机 5285 JAXPFive.javaimport org.w3c.dom.*; import javax.xml.parsers.*;import java.io.*;public class JAXPFive public static void main(String args) GiveData

14、 give=new GiveData(); try DocumentBuilderFactory factory= DocumentBuilderFactory.newInstance(); DocumentBuilder domPaser=factory.newDocumentBuilder(); Document document=domPaser.parse(new File(example4_5.xml) ; NodeList nodeList=document.getChildNodes(); give.output(nodeList); System.out.printf(平均价格

15、:%5.2f%s,give.average/give.m,give.mess); catch(Exception e) System.out.println(e); class GiveData double average=0,m=0; String mess; public void output(NodeList nodeList) int size=nodeList.getLength(); for(int k=0;k0) System.out.print(name+(+id+):); mess=id; NodeList nodes=elementNode.getChildNodes(

16、); output(nodes); 4.6 Attr节点例子6Example4_6.xml 张三 清华大学 土木工程 张三 北京大学 计算数学 JAXPSix.javaimport org.w3c.dom.*; import javax.xml.parsers.*;import java.io.*;public class JAXPSix public static void main(String args) GiveData give=new GiveData(); try DocumentBuilderFactory factory= DocumentBuilderFactory.new

17、Instance(); DocumentBuilder domPaser=factory.newDocumentBuilder(); Document document=domPaser.parse(new File(example4_6.xml) ; Element root=document.getDocumentElement() ; NodeList nodeList=root.getChildNodes(); give.output(nodeList); catch(Exception e) System.out.println(e); class GiveData public v

18、oid output(NodeList nodeList) int size=nodeList.getLength(); for(int k=0;ksize;k+) Node node=nodeList.item(k); if(node.getNodeType()=Node.TEXT_NODE) Text textNode=(Text)node; String content=textNode.getWholeText(); System.out.print(content); if(node.getNodeType()=Node.ELEMENT_NODE) Element elementNo

19、de=(Element)node; String name=elementNode.getNodeName(); System.out.print(name+:); NamedNodeMap map=elementNode.getAttributes(); for(int m=0;mmap.getLength();m+) Attr attrNode=(Attr)map.item(m); String attName=attrNode.getName(); String attValue=attrNode.getValue(); System.out.print(+attName+:+attVa

20、lue+); NodeList nodes=elementNode.getChildNodes(); output(nodes); 4.7 DocumentType节点例子7time.dtd example4_7.xml T259 18:38 K1257 23:12 JAXPSeven.javaimport org.w3c.dom.*; import javax.xml.parsers.*;import java.io.*;public class JAXPSeven public static void main(String args) try DocumentBuilderFactory

21、 factory=DocumentBuilderFactory.newInstance(); DocumentBuilder domParser=factory.newDocumentBuilder(); Document document=domParser.parse(new File(example4_7.xml) ; DocumentType doctype=document.getDoctype(); String DTDName=doctype.getName(); System.out.println(DTD名字:+DTDName); String publicId=doctyp

22、e.getPublicId(); System.out.println(public标识:+publicId); String systemId=doctype.getSystemId(); System.out.println(system标识:+systemId); String internalDTD =doctype.getInternalSubset(); System.out.println(内部DTD:+internalDTD); catch(Exception e) 4.8 处理空白例子8bookList.dtd Java程序设计 清华大学出版社 高等数学 高等教育出版社 ex

23、ample4_8.xml Java程序设计 清华大学出版社 高等数学 高等教育出版社 JAXPEight.javaimport org.w3c.dom.*; import javax.xml.parsers.*;import java.io.*;public class JAXPEight public static void main(String args) GiveData give=new GiveData(); try DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); factory.setIgnoringElementContentWhitespace(true); /忽略缩进

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1