XML
Posted zuiaimiusi
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了XML相关的知识,希望对你有一定的参考价值。
未完待续!~
dom4j解析
需要添加第三方jar包 可以去https://dom4j.github.io/下载
stus.xml
<?xml version="1.0" encoding="UTF-8"?> <stus> <stu> <name>w</name> <college>qsing</college> <tele>1365</tele> </stu> <stu> <name>x</name> <college>zju</college> <tele>1893</tele> </stu> </stus>
a.java
import org.dom4j.io.SAXReader; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import java.util.Iterator; public class a public static void main(String[] args) try //创建解析器 SAXReader reader = new SAXReader(); //通过解析器的read方法将配置文件读取到内存中,生成一个Document[org.dom4j]对象树 Document document=reader.read("conf/stus.xml"); //获取根节点 Element root=document.getRootElement(); //遍历根节点 for(Iterator<Element>rootIter=root.elementIterator();rootIter.hasNext();) Element studentElt=rootIter.next(); for (Iterator<Element>innerIter=studentElt.elementIterator();innerIter.hasNext();) Element innerElt=innerIter.next(); String innerValue=innerElt.getStringValue(); System.out.println(innerValue); System.out.println("-----------------------"); catch (Exception e) e.printStackTrace();
sax解析 不需要外部导入jar包
优点:
无需将整个文档加载到内存中,所以内存消耗少,适合解析特别大的XML文件
1.创建解析工厂:通过newInstance()方法获取
SAXParserFactory saxParseFactory=SAXParseFactory.newInstance();
2.创建解析器
SAXParser saxParser=saxParserFactory.newSAXParser();
执行parse方法,需要传递两个参数:
1.XML文件路径
2.事件处理器
当把事件处理器传递进来,相当于在方法里面绑定了事件处理器,该事件是自动执行
执行事件处理器的三个方法:
startElement(String url,String localName,String qName,Attributes attributes);
characters(char[] ch,int start,int length);
endElement(String url,String localName,String qName);
qName 把标签名称返回
attributes 返回标签中的属性对象
import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import java.util.Iterator; public class a public static void main(String[] args) //创建解析器工厂 SAXParserFactory saxParserFactory=SAXParserFactory.newInstance(); //创建解析器 try SAXParser saxParser=saxParserFactory.newSAXParser(); //通过解析器的parse方法 saxParser.parse("conf/stus.xml",new MyDefaultHandler()); catch (Exception e) e.printStackTrace(); class MyDefaultHandler extends DefaultHandler @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException System.out.print("<"+qName+">"); @Override public void characters(char[] ch, int start, int length) throws SAXException System.out.print(new String(ch,start,length)); @Override public void endElement(String uri, String localName, String qName) throws SAXException System.out.print("</"+qName+">");
打印出来的效果:
<stus> <stu> <name>wxy</name> <college>qsing</college> <tele>1365</tele> </stu> <stu> <name>x</name> <college>zju</college> <tele>1893</tele> </stu> </stus>
使用dom4j和xpath解析XML文件
需要下载jaxen.jar 下载地址
http://www.java2s.com/Code/Jar/j/Downloadjaxen11beta11jar.htm
import org.dom4j.io.SAXReader; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; public class a public static void main(String[]args) try SAXReader reader=new SAXReader(); Document document=reader.read("conf/stus.xml"); //name节点元素的路径:stus->stu->name //name节点元素的xpath路径:/stus/stu/name //name节点元素的xpath路径:stus//name //name节点元素的xpath路径://name Element driverNameElt=(Element)document.selectSingleNode("/stus/stu/name"); //获取driverNameElt节点元素对象的文本内容 String driverName=driverNameElt.getStringValue(); System.out.print(driverName);//wxy Element urlElt=(Element)document.selectSingleNode("stus//name"); String url=urlElt.getText(); System.out.print(url);//wxy catch (Exception e) e.printStackTrace();
获取结点元素中的值
sys-config.xml
<?xml version="1.0" encoding="UTF-8"?> <server> <service> <connector port="8080"></connector> </service> </server>
a.java
方法1
import org.dom4j.Attribute; import org.dom4j.io.SAXReader; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; public class a public static void main(String[] args) throws DocumentException SAXReader saxReader=new SAXReader(); Document document=saxReader.read("conf/sys-config.xml"); Element connectorElt=(Element)document.selectSingleNode("//connector"); //获取connectorElt节点元素对象的port属性对象 Attribute portAttr=connectorElt.attribute("port"); //获取portAttr属性对象的值 String port=portAttr.getStringValue(); System.out.print(port);//8080
方法2
import org.dom4j.io.SAXReader; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; public class a public static void main(String[] args) throws DocumentException SAXReader saxReader=new SAXReader(); Document document=saxReader.read("conf/sys-config.xml"); Element connectorElt=(Element)document.selectSingleNode("//connector"); String portValue=connectorElt.attributeValue("port"); System.out.print(portValue);//8080
以上是关于XML的主要内容,如果未能解决你的问题,请参考以下文章