1、 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.getNode
2、Name(); System.out.println(XML文件根节点的名字:+rootName); NodeList nodelist=root.getElementsByTagName(姓名); int size=nodelist.getLength(); for(int k=0;k雇员 张三 25岁工资 3190元/月/雇员 李四 35岁 4320元/月JAXPTwo.javapublic class JAXPTwo try DocumentBuilderFactory factory= DocumentBuilderFactory.newInstance(); DocumentBuil
3、der domParser= factory.newDocumentBuilder(); Document document=domParser.parse(new File(example6_2.xml NodeList nodeList=document.getChildNodes(); output(nodeList); public static void output(NodeList nodeList) /output是一个递归方法 int size=nodeList.getLength(); Node node=nodeList.item(k); if(node.getNodeT
4、ype()=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(); out
5、put(nodes); /递归调用4.4 Element节点例子3example4_3.xml商品列表商品名称 分类=家电 电视机 /商品名称服装 雅格尔西装 食品 东北大米 /商品列表JAXPThree.javapublic class JAXPThreeexample6_3.xml Element root=document.getDocumentElement() ; NodeList nodeList=root.getChildNodes(); String id=elementNode.getAttribute(分类 String content=elementNode.getTex
6、tContent(); System.out.print(+id+): catch(Exception e)4.5 Text节点例子4example4_4.xml同学录 张大山居住城市大连市联系电话0411-123456 刘翠花北京市010-654321JAXPFour.javapublic class JAXPFour GiveData give=new GiveData();example4_4.xml give.output(nodeList);一共有+give.m+个Text节点class GiveData int m=0; public void output(NodeList no
7、deList) /这是一个递归方法 m+;例子5example4_5.xml电视机列表名称 海尔电视机价格 单位=元/台5238/名称 星海电视机3660 长虹电视机5285JAXPFive.javapublic class JAXPFiveexample4_5.xml System.out.printf(平均价格:%5.2f%s,give.average/give.m,give.mess); double average=0,m=0; String mess; public void output(NodeList nodeList) Element parent=(Element)text
8、Node.getParentNode(); boolean boo=(parent.getNodeName().equals(价格 if(boo=true) content=textNode.getWholeText(); average=average+Double.parseDouble(content.trim();单位 if(id.length()0): mess=id;4.6 Attr节点例子6Example4_6.xml应聘者列表姓名 学历=研究生 张三毕业学校 类型=工科清华大学专业 土木工程 本科理科北京大学 计算数学 JAXPSix.javapublic class JAXP
9、Sixexample4_6.xml NamedNodeMap map=elementNode.getAttributes(); for(int m=0;mELEMENT 客车 (车次,开车时间)ELEMENT 车次 (#PCDATA)ELEMENT 开车时间 (#PCDATA)ATTLIST 客车 类别 CDATA #REQUIREDexample4_7.xmlDOCTYPE 北京站始发列车时刻表 PUBLIC -/ISO985/China/ForXML/Ch time.dtd北京站始发列车时刻表客车 类别=特快车次 T259 开车时间 18:38 /客车普快 K1257 23:12 JAXP
10、Seven.javapublic class JAXPSeven try DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); DocumentBuilder domParser=factory.newDocumentBuilder();example4_7.xml DocumentType doctype=document.getDoctype(); String DTDName=doctype.getName();DTD名字:+DTDName); String publicId=doctype.getPub
11、licId();public标识:+publicId); String systemId=doctype.getSystemId();system标识:+systemId); String internalDTD =doctype.getInternalSubset();内部DTD:+internalDTD);4.8 处理空白例子8bookList.dtdDOCTYPE 图书列表 SYSTEM bookList.dtd图书列表book书名Java程序设计出版社清华大学出版社/book高等数学高等教育出版社example4_8.xmlJAXPEight.javapublic class JAXPEight factory.setIgnoringElementContentWhitespace(true); /忽略缩进