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