XML作用:不同应用之间的通信和数据共享
Dom遍历法:对内存消耗大,容易内存溢出
SAX方法:事件驱动模式,缺点 不易操作,很难同时访问多处不同数据,对内存消耗不大,速度快
jdom方法:
dom4j方法:优秀比jdom优秀 Dom遍历法:全部加载内容到内存
DocumentBuilderFactory d=DocumentBuilderFactory.newInstance();
try {
DocumentBuilder bu=d.newDocumentBuilder();
Document dom=bu.parse("pom.xml");
NodeList nl=dom.getElementsByTagName("dependency");
System.out.println(nl.getLength());
for(int i=0;i<nl.getLength();i++) {
Node node=nl.item(i);
NodeList nnm=node.getChildNodes(); for(int k=0;k<nnm.getLength();k++) {
Node node1=nnm.item(k);
if(node1.getNodeType()==Node.ELEMENT_NODE) {
String tt=node1.getNodeName();
String tt1=node1.getTextContent();
System.out.println(tt+":"+tt1+"\n");
} }
}
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} SAX方法 两个步骤:
package com.xml; import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler; public class SAXhandler extends DefaultHandler {
int index=0;
public void startElement(String uri, String localName,String qName,Attributes attributes) throws SAXException {
super.startElement(uri, localName, qName, attributes);
if(qName=="project") {
index++;
System.out.println("开始遍历第"+index+"gejiedian");
//String va=attributes.getValue("id");//已知属性名称
int aa=attributes.getLength();
for(int i=0;i<aa;i++) {
String name=attributes.getQName(i);
String va=attributes.getValue(name);
System.out.println(name+":"+va);
}
}else if(qName.equals("dependency")) {
System.out.print("节点名称:"+qName);
} }
public void endElement(String uri, String localName, String qName) throws SAXException {
super.endElement(uri, localName, qName);
if(qName.equals("project")) {
System.out.println("end遍历第"+index+"gejiedian");
} }
public void startDocument() throws SAXException {
super.startDocument();
System.out.println("解析开始");
}
public void endDocument() throws SAXException {
super.endDocument();
System.out.println("解析结束");
} public void characters(char[]ch, int start, int length) throws SAXException {
super.characters(ch, start, length);
String value=new String(ch, start, length);
if(!value.trim().equals("")) {
System.out.println(value);
} }
} import java.io.IOException; import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory; import org.xml.sax.SAXException; public class SAX { public static void main(String[] args) {
// TODO Auto-generated method stub
SAXParserFactory sf=SAXParserFactory.newInstance();
try {
SAXParser sp=sf.newSAXParser();
SAXhandler sh=new SAXhandler();
sp.parse("pom.xml", sh); } catch (ParserConfigurationException | SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } } jdom方法:需要额外jar包 import java.io.File; import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder; public class JDOM { public static void main(String[] args) throws JDOMException, IOException {
SAXBuilder sb=new SAXBuilder();
Document Document = sb.build(new File("pom1.xml"));
Element rootelement=Document.getRootElement();
List<Element> listele=rootelement.getChildren();
List<Object> lo=new ArrayList<Object>(); for( Element ele:listele) {
obj o=new obj();
System.out.println("开始"+(listele.indexOf(ele)+1)+"个");
List<Attribute> attrs=ele.getAttributes();
for(Attribute attr:attrs) {
String attrname=attr.getName();
String attrvalue=attr.getValue();
System.out.println(attrname+":"+attrvalue);
}
List<Element> list_sun= ele.getChildren();
for( Element ele1:list_sun) { if(ele1.getName().equals("groupId") ) {
o.setGroupId(ele1.getValue());
}else if(ele1.getName().equals("artifactId")) {
o.setArtifactId(ele1.getValue());
}
}
lo.add(o);
o=null; } System.out.println(lo); } } dom4j方法:
import java.io.File;
import java.util.Iterator;
import java.util.List; import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader; public class dom4j { public static void main(String[] args) throws DocumentException {
SAXReader sr=new SAXReader();
Document Document = sr.read(new File("pom1.xml"));
Element root=Document.getRootElement();
Iterator it=root.elementIterator();
while(it.hasNext()) {
Element ele=(Element) it.next();
List<Attribute> list=ele.attributes();
for(Attribute attr:list) {
System.out.println(attr.getName()+":"+attr.getValue());
} Iterator suneles=ele.elementIterator();
while(suneles.hasNext()) {
Element ele1=(Element) suneles.next(); System.out.println(ele1.getName()+":"+ele1.getStringValue());
} }
} }

  

最新文章

  1. Git on Windows 一些问题
  2. C#中Cookie的概述及应用
  3. 用c#开发微信 (4) 基于Senparc.Weixin框架的接收事件推送处理 (源码下载)
  4. [codevs1380]没有上司的舞会([BZOJ2060][Usaco2010 Nov]Visiting Cows 拜访奶牛)
  5. 检测到有潜在危险的 Request.Form 值。 说明: ASP.NET 在请求中检测到包含潜在危险的数据
  6. win10 IIS10 HTTP 错误 404.2 - Not Found
  7. 在Mac OS X中使用VIM开发STM32(1)
  8. Android(java)学习笔记180:Android MediaPlayer 播放prepareAsync called in state 8解决办法
  9. 从PHP程序员到RAW开发~
  10. 同时运行ecstore1.2与ecstore2.0的解决方案
  11. 关于 insufficient memory case 4 的解决记录
  12. mysql 导入数据库问题
  13. OpenCV入门之寻找图像的凸包(convex hull)
  14. 苹果电脑利用wget总是会出现无法建立 SSL 连接的问题
  15. 2017-2018-2 20155203《网络对抗技术》Exp6 信息搜集与漏洞扫描
  16. Ubuntu12.04下搭建Java环境
  17. MySQL外键使用及说明详解
  18. 使用Navicat for Oracle工具连接oracle
  19. PyQt5系列教程(四)信号和槽
  20. CDOJ ABCDE dp(前缀和优化)

热门文章

  1. What size do you use for varchar(MAX) in your parameter declaration?
  2. RHEL6.1 安装 Oracle10gr2 (图文、解析)
  3. js的内部特性--属性
  4. 第六周实验总结&amp;学习总结
  5. 20140922 tcpip3次握手 分段 分页 spooling 位示图
  6. window下eclipse搭建hadoop环境
  7. redis 入门之列表
  8. nginx匹配以XXX结尾的
  9. 使用WdatePicker时间插件简单的控制页面上两个时间选择的前后范围
  10. 第九章 Service