XML解析方式:
DOM解析
SAX解析
XML解析工具
DOM解析原理:
1)JAXP (oracle-Sun公司官方)
2)JDOM工具(非官方)
3)Dom4J工具(非官方)
三大框架(默认读取xml的工具就是Dom4j)
.......
SAX解析原理:
1)Sax解析工具(oracle-sun公司官方)
DOM解析原理:
xml解析器一次性把整个xml文档加载进内存,然后在内存中构建一颗Document的对象树,通过Document对象,得到树上的节点对象,通过节点对象访问(操作)到xml文档的内容。 xml对象document对象: 1.1创建一个xml解析器对象 SAXReader reader =new SAXReader(); 1.2读取xml文档,返回Document对象 Document doc = reader.read(new File("./src/contacts.xml")); System.out.print(doc);
读取标签
1、getRootElement获取根元素对象 Element root = xmlDoc.getRootElement(); 2、getName获取根元素名称 String elementName = root.getName(); 3、element通过元素名称获取元素对象 指定名称的第一个元素 Element element =root.element(“元素名称"); 4、nodeIterator: 得到当前节点下的所有子节点对象(不包含孙以下的节点) 得到根节点元素: SAXReader reader = new SAXReader(); Document doc = reader.read(new File("./src/contacts.xml")); Iterator<Node> it = doc.nodeIterator(); while(it.hasNext()){//判断是否有下一个元素 Node node = it.next();//取出元素 String name = node.getName();//得到节点名称 System.out.println(name);//输出contactList } //得到根节点下的元素(不包括孙元素) if(node instanceof Element){ Element elem = (Element)node; Iterator<Node> it2 = elem.nodeIterator(); while(it2.hasNext()){ Node n2 = it2.next(); System.out.println(n2.getName()); } } 注意:每个标签跟一个null是因为空格和换行也是属于Element但是为null
5、Iterator<Element>Element.elementIterator("标签名");指定名称的所有子标签
根标签下指点的名字是contact的所有contact标签 Element rootElem = doc.getRootElement(); Iterator<Element> it = rootElem.elementIterator("contact"); 得到所有属性对象,返回迭代器 Iterator<Attribute> it = contactElem.attributeIterator(); while(it.hasNext()){ Attribute attr = it.next(); System.out.println(attr.getName()+"="+attr.getValue()); } while(it.hasNext()){ Element elem = it.next(); System.out.println(elem.getName()); }
6、elements获取所有直接子元素对象集合
获取根节点下的所有子节点 List<Element> list = rootElem.elements(); //遍历List的方法 //1)传统for循环 2)增强for循环 3)迭代器 for(int i=0;i<list.size();i++){ Element e = list.get(i); System.out.println(e.getName()); } for(Element e:list){ System.out.println(e.getName()); } Iterator<Element> it = list.iterator(); //ctrl+2 松开 l while(it.hasNext()){ Element elem = it.next(); System.out.println(elem.getName()); }
获取属性
Element contactElem = doc.getRootElement().element("contact"); 通过属性名获取元素属性对象 Attribute attribute = element.attribute(“属性名称"); Attribute idAttr = contactElem.attribute("id"); //getName: 属性名称 getValue:属性值 System.out.println(idAttr.getName() +"=" + idAttr.getValue()); 或是:Attribute idAttr = contactElem.attribute(0); System.out.println(idAttr.getName() +"=" + idAttr.getValue()); 通过元素对象 获得属性值:String value = element.attributeValue(“属性名称”); String idValue = contactElem.attributeValue("id"); System.out.println(idValue); 获取所有属性对象集合attributes List attributeList = root.attributes(); List<Attribute> list = contactElem.attributes(); //遍历属性 for (Attribute attr : list) { System.out.println(attr.getName()+"="+attr.getValue()); } 得到所有属性对象,返回迭代器 Iterator<Attribute> it = contactElem.attributeIterator(); while(it.hasNext()){ Attribute attr = it.next(); System.out.println(attr.getName()+"="+attr.getValue()); }
获取文本
String content = doc.getRootElement().getText();
System.out.println(content);
//获取文本(先获取标签,再获取标签上的文本)
Element nameELem =
doc.getRootElement().element("contact").element("name");
//1. 得到文本
String text = nameELem.getText();
System.out.println(text);
//2. 得到指定子标签名的文本内容
String text2 =
doc.getRootElement().element("contact").elementText("phone");
System.out.println(text2);
读取一个完整的xml标签 public void test() throws Exception{ //读取xml文档 SAXReader reader = new SAXReader(); Document doc = reader.read(new File("./src/contacts.xml")); //读取根标签 Element rootELem = doc.getRootElement(); StringBuffer sb = new StringBuffer(); getChildNodes(rootELem,sb); System.out.println(sb.toString()); } /** * 获取当前标签的所有子标签 */ private void getChildNodes(Element elem,StringBuffer sb){ //System.out.println(elem.getName()); //开始标签 sb.append("<"+elem.getName()); //得到标签的属性列表 List<Attribute> attrs = elem.attributes(); if(attrs!=null){ for (Attribute attr : attrs) { //System.out.println(attr.getName()+"="+attr.getValue()); sb.append(" "+attr.getName()+"=\""+attr.getValue()+"\""); } } sb.append(">"); //得到文本 //String content = elem.getText(); //System.out.println(content); Iterator<Node> it = elem.nodeIterator(); while(it.hasNext()){ Node node = it.next(); //标签 if(node instanceof Element){ Element el = (Element)node; getChildNodes(el,sb); } //文本 if(node instanceof Text){ Text text = (Text)node; sb.append(text.getText()); } } //结束标签 sb.append("</"+elem.getName()+">"); }
XML
<?xml version="1.0" encoding="gbk"?>
<contactList>
<contact id="001">
<name>张三</name>
<age>20</age>
<phone>1216461316</phone>
<email>zhangsan@qq.con</email>
<qq>2451356331</qq>
</contact>
<contact id="002">
<name>李四</name>
<age>23</age>
<phone>122341316</phone>
<email>lisi@qq.con</email>
<qq>23424331</qq>
<app>101010</app>
</contact>
<abc></abc>
</contactList>
腾不出时间思考的人,迟早会腾出时间来后悔;腾不出时间学习的人,迟早会腾出时间来伤悲。