JAVA基础--XML解析

来源:互联网 发布:好scratch编程 编辑:程序博客网 时间:2024/06/06 03:41

XML的解析方式有很多,光开源的就有十多种:如Xerces、JDOM、DOM4J、XOM、JiBX、KXML、XMLBeans、jConfig、XStream、XJR等。

但是最常用的还是sax、dom、pull、dom4j 

本文演示dom解析和sax解析。


DOM解析一

import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import org.w3c.dom.Document;import org.w3c.dom.NamedNodeMap;import org.w3c.dom.Node;import org.w3c.dom.NodeList;public class Test {public static void main(String[] args) throws Exception {//获取解析权限。DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();DocumentBuilder db = dbf.newDocumentBuilder();Document doc = db.parse("C:\\Users\\Administrator\\Desktop\\Person.xml");//获取解析标签名字为Emlpoyee的类容,装入nodeList里。NodeList nodeList = doc.getElementsByTagName("Employee");//遍历标签里面的类容。for(int i=0;i<nodeList.getLength();i++){//取出nodelist里面的类容。Node node = nodeList.item(i);//得到标签里面的属性,并装入集合中。NamedNodeMap map = node.getAttributes();//遍历属性里面的类容。for(int j=0;j<map.getLength();j++){//取出对应下标的属性,并装入a中。Node a = map.item(j);//获取属性的名字。String name = a.getNodeName();//获取属性的值。String value = a.getNodeValue();System.out.println("节点名称:"+name + "  节点值:" +value);}}}}

DOM解析二

import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import org.w3c.dom.Document;import org.w3c.dom.NamedNodeMap;import org.w3c.dom.Node;import org.w3c.dom.NodeList;public class Test {public static void main(String[] args) throws Exception {//创建DocumentBuilderFactory对象。DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();//创建DocumentBuilder对象。DocumentBuilder db = dbf.newDocumentBuilder();//创建Document对象,并获取解析地址。Document doc = db.parse("C:\\Users\\Administrator\\Desktop\\Person.xml");//获取标签为Employee的标签内容,并装入NodeList里面。NodeList nodeList = doc.getElementsByTagName("Employee");//遍历所有标签的内容。for(int i=0;i<nodeList.getLength();i++){//获取单个标签,并装入node里面。Node node = nodeList.item(i);//获取标签的子节点。并所有内容存入NodeList里面。NodeList nodeChild = node.getChildNodes();//遍历子节点内容。for(int j=0;j<nodeChild.getLength();j++){//获取单个子节点的内容。Node child = nodeChild.item(j);//获取子节点的名字。String childName = child.getNodeName();//获取子节点的文本内容。String childText = child.getTextContent();//本个xml里面一个标签含有九个子节点,5个空白,4个真实节点//此处的判断作用是去除空白的节点,不输出。if(child.getNodeType() == Node.ELEMENT_NODE){System.out.println("标签名:"+childName +"  内容:"+ childText);}}}}}
SAX解析

import javax.xml.parsers.SAXParser;import javax.xml.parsers.SAXParserFactory;import org.xml.sax.Attributes;import org.xml.sax.SAXException;import org.xml.sax.helpers.DefaultHandler;public class Test {//sax解析适合解析大文件xmlpublic static void main(String[] args) throws Exception {//创建SAXParserFactory对象。SAXParserFactory spf = SAXParserFactory.newInstance();//创建SAXParser对象SAXParser sp = spf.newSAXParser();//创建DefaultHandler的子类对象,并重写里面的方法。MyDefaultHandler my = new MyDefaultHandler();//调用解析文件,并解析。sp.parse("C:\\Users\\Administrator\\Desktop\\Person.xml", my);}}class MyDefaultHandler extends DefaultHandler{//开始解析文件。@Overridepublic void startDocument() throws SAXException {// TODO Auto-generated method stubsuper.startDocument();System.out.println("开始解析。。。");}//开始解析元素。@Overridepublic void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {// TODO Auto-generated method stubsuper.startElement(uri, localName, qName, attributes);System.out.print(qName+"   ");for(int i=0;i<attributes.getLength();i++){String name = attributes.getQName(i);String value = attributes.getValue(i);System.out.println("   标签名:"+name + "  标签值:"+ value);}}//元素结束符。@Overridepublic void endElement(String uri, String localName, String qName) throws SAXException {// TODO Auto-generated method stubsuper.endElement(uri, localName, qName);System.out.print(qName+"  ");}//获取节点的内容。@Overridepublic void characters(char[] ch, int start, int length) throws SAXException {// TODO Auto-generated method stubsuper.characters(ch, start, length);String str = new String(ch,start,length);if(!str.isEmpty()){System.out.print(str+"   ");}}//结束文件解析@Overridepublic void endDocument() throws SAXException {// TODO Auto-generated method stubsuper.endDocument();System.out.println("结束解析。。。");}}
解析的XML文件,可以任意
<?xml version="1.0" encoding="UTF-8"?><Person><Employee id="1"><name>Tom</name><age>20</age><sex>male</sex></Employee><Employee id="2"><name>Jery</name><age>18</age><sex>female</sex></Employee><Manager id="3"><name>coco</name><age>28</age><sex>female</sex></Manager></Person>

DOM解析进阶(任意深度)

public class Test {public static int c1 = 0;public static void main(String[] args) throws Exception {//简历xml解析DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();DocumentBuilder db = dbf.newDocumentBuilder();//注意,此处路径不能使用中文名。Document doc = db.parse("C:\\Users\\Administrator\\Desktop\\Test.xml");//得到标签rootNodeList nodeList = doc.getElementsByTagName("root");// 他的长度为1//循环遍历标签中的所有子节点。for (int i = 0; i < nodeList.getLength(); i++) {Node node = nodeList.item(i);NodeList nodeChild = node.getChildNodes();//获取标签的名字。System.out.println("<" + node.getNodeName() + ">");//递归遍历所有的子节点。GetNodeChildAll(nodeChild, 0);System.out.println("</" + node.getNodeName() + ">");}}private static void GetNodeChildAll(NodeList nodeChild, int start) {for (int j = 0; j < nodeChild.getLength(); j++) {Node child = nodeChild.item(j);//孩子节点不为空文本节点。if (child.getNodeType() == Node.ELEMENT_NODE) {//获取孩子节点名字String childName = child.getNodeName();//获取孩子节点的所有文本。String childText = child.getTextContent();//以换行符切割String[] str = childText.split("\n");System.out.print("<" + childName + ">");if (childText != null)System.out.print(str[0]);//将n传入下次递归。。。NodeList n = child.getChildNodes();if (n.getLength() > 0){GetNodeChildAll(n, start+1);}System.out.println("</" + childName + ">");}}}}
采用SAX解析的方式解析XML,且将解析内容转存到本地文件中。

public class Test {public static void main(String[] args) throws Exception {SAXParserFactory spf = SAXParserFactory.newInstance();SAXParser sp = spf.newSAXParser();MyDefaultHandler my = new MyDefaultHandler();sp.parse("C:\\Users\\Administrator\\Desktop\\Test.xml", my);File file = new File("F:\\B.txt");try {Writer out = new FileWriter(file);BufferedWriter out2 = new BufferedWriter(out);out2.write(my.str);out2.newLine();out2.flush();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}class MyDefaultHandler extends DefaultHandler{public static String str = "";@Overridepublic void startDocument() throws SAXException {// TODO Auto-generated method stubsuper.startDocument();System.out.println("开始解析。。。");}@Overridepublic void endDocument() throws SAXException {// TODO Auto-generated method stubsuper.endDocument();System.out.println("结束解析。。。");}@Overridepublic void endElement(String uri, String localName, String qName) throws SAXException {// TODO Auto-generated method stubsuper.endElement(uri, localName, qName);if(!qName.isEmpty()){System.out.println("   "+qName);this.str+="   "+qName+"\r\n";}}@Overridepublic void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {// TODO Auto-generated method stubsuper.startElement(uri, localName, qName, attributes);System.out.print("  "+qName);this.str+="  "+qName;}@Overridepublic void characters(char[] ch, int start, int length) throws SAXException {// TODO Auto-generated method stubsuper.characters(ch, start, length);String str = new String(ch, start, length);if(!str.isEmpty()){System.out.print(" "+str);this.str += " "+str;}}}