




已阅读5页,还剩11页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第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 domPaser=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(); 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.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) ; 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=(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节点例子3example4_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(); 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; 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.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 document=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=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(); m+; 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); 例子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 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(平均价格:%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(); 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.newInstance(); 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 void 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 elementNode=(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+:+attValue+); 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 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=doctype.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程序设计 清华大学出版社 高等数学 高等教育出版社 example4_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); /忽略缩进空白。 DocumentBuilder domPaser= factory. newDocumentBuilder(); Document document=domPaser.parse(new File(example4_8.xml) ; NodeList nodeList=document.getChildNodes(); give.output(nodeList); System.out.printf(n一共有%d个Text节点,give.m); catch(Exception e) class GiveData int m=0; public void 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(); m+; System.out.println(content); if(node.getNodeType()=Node.ELEMENT_NODE) Element elementNode=(Element)node; String name=elementNode.getNodeName(); NodeList nodes=elementNode.getChildNodes(); output(nodes); 4.10 使用DOM生成XML文件例子9example4_9.xml 建设银行 8:30 中国银行 9:30 JAXPNine.javaimport javax.xml.transform.*;import javax.xml.transform.stream.*;import javax.xml.transform.dom.*;import org.w3c.dom.*; import javax.xml.parsers.*;import java.io.*;public class JAXPNine public static void main(String args) ModifyNode modify=new ModifyNode(); try DocumentBuilderFactory factory= DocumentBuilderFactory.newInstance(); DocumentBuilder domParser= factory.newDocumentBuilder(); Document document=domParser.parse(new File(example4_9.xml) ; Element root=document.getDocumentElement() ; NodeList nodeList=root.getChildNodes(); modify.modifyNode(nodeList,document); TransformerFactory transFactory=TransformerFactory.newInstance(); Transformer transformer=transFactory.newTransformer(); DOMSource domSource=new DOMSource(document); File file=new File(newXML.xml); FileOutputStream out=new FileOutputStream(file); StreamResult xmlResult=new StreamResult(out); transformer.transform(domSource,xmlResult); out.close(); catch(Exception e) System.out.println(e); class ModifyNode int m=0; Document document; public void modifyNode(NodeList nodeList,Document document) this.document=document; 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; String name=elementNode.getNodeName(); if(name.equals(银行) Node textN=document.createTextNode(18:30); Node elementN=document.createElement(关门时间); elementN.appendChild(textN); elementNode.appendChild(elementN); NodeList nodes=elementNode.getChildNodes(); modifyNode(nodes,document); newXML.xml 建设银行 8:30 18:30 中国银行 9:30 18:30 例子10JAXPTen.javaimport javax.xml.transform.*;import javax.xml.transform.stream.*;import javax.xml.transform.dom.*;import org.w3c.dom.*; import javax.xml.parsers.*;import java.io.*;public class JAXPTen public static void main(String args) try String studentName=辛如学,纲尚学,楚进校; String studentNumber=201001,201002,201003; DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); DocumentBuilder domPaser=factory.newDocumentBuilder(); Document document=domPaser.newDocument(); docu
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 网络规划设计师课外知识补充试题及答案
- 西医临床考试前必看内容介绍试题及答案
- 脑卒中病人测试题及答案
- 药物溶出与吸收机制的研究试题及答案
- 药品合法注册程序试题及答案
- 电工取证考试题及答案
- 宗门大赛笔试题及答案
- 督察组考试试题及答案
- 考试科目划分与重点初级药师试题及答案
- 激光技术工程师在行业中的价值试题及答案
- 中医医疗技术手册2013普及版
- 景区人员管理制度
- 采矿学课程设计-潘三煤矿1
- MOOC 空中机器人-浙江大学 中国大学慕课答案
- 供电所年度培训计划
- 北师大版数学五年级(下册)长方体(二) 练习四
- DB35T 2082-2022 人民防空疏散基地建设基本要求
- 再生铝商业计划书
- 江苏省苏州市2022-2023学年二年级下学期语文期中调研试卷(含答案)
- 肺结核的治疗原则和居家护理
- 角磨机切割作业的应急预案
评论
0/150
提交评论