XML解析
XML解析器分别为SAX解析器、DOM解析器和PULL解析器。
11.txt
<?xml version="1.0" encoding="UTF-8"?>
<!-- published at 2015-08-03 15:41:42 -->
<Profiles>
<Weather>
<city>
北京
</city>
<city>上海</city>
<city>广州</city>
<city>深圳</city>
<city>天津</city>
<status1>阴</status1>
<status2>多云</status2>
<figure1>yin</figure1>
<figure2>duoyun</figure2>
<direction1>无持续风向</direction1>
<direction2>无持续风向</direction2>
<power1>≤3</power1>
<power2>≤3</power2>
<temperature1>30</temperature1>
<temperature2>23</temperature2>
<ssd>7</ssd>
<tgd1>27</tgd1>
<tgd2>27</tgd2>
<zwx>1</zwx>
<ktk>3</ktk>
<pollution>3</pollution>
<xcz>4</xcz>
<zho></zho>
<diy></diy>
<fas></fas>
<chy>1</chy>
<zho_shuoming>暂无</zho_shuoming>
<diy_shuoming>暂无</diy_shuoming>
<fas_shuoming>暂无</fas_shuoming>
<chy_shuoming>短袖衫、短裙、短裤、薄型T恤衫、敞领短袖棉衫</chy_shuoming>
<pollution_l>轻度</pollution_l>
<zwx_l>最弱</zwx_l>
<ssd_l>偏热</ssd_l>
<fas_l>暂无</fas_l>
<zho_l>暂无</zho_l>
<chy_l>薄短袖类</chy_l>
<ktk_l>较适宜开启(制冷)</ktk_l>
<xcz_l>不太适宜</xcz_l>
<diy_l>暂无</diy_l>
<pollution_s>对空气污染物扩散无明显影响</pollution_s>
<zwx_s>紫外线最弱</zwx_s>
<ssd_s>天气偏热,适当调整穿衣后,仍可达到比较舒适的程度。</ssd_s>
<ktk_s>比较适宜开启空调</ktk_s>
<xcz_s>洗车后未来1-2天内有降水、大风或沙尘天气,不太适宜洗车</xcz_s>
<gm>1</gm>
<gm_l>低发期</gm_l>
<gm_s>环境温度较高,要提防长时间在空调环境中引发的空调病;</gm_s>
<yd>5</yd>
<yd_l>不适宜</yd_l>
<yd_s>天气炎热,不适宜户外运动;</yd_s>
<savedate_weather>2015-08-03</savedate_weather>
<savedate_life>2015-08-03</savedate_life>
<savedate_zhishu>2015-08-03</savedate_zhishu>
<udatetime>2015-08-03 08:21:36</udatetime>
</Weather>
</Profiles>
package com.lingzhuo;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class Test2 {
public static void main(String[] args) {
DocumentBuilderFactory factory= DocumentBuilderFactory.newInstance();//创建dom解析器的工厂
try {
DocumentBuilder builder=factory.newDocumentBuilder();//使用解析器工厂创建解析器
Document document =builder.parse("d://11.txt");//使用解析器解析文档生成倒树的document
NodeList list=document.getElementsByTagName("Weather");//得到文档中的所有的Weather 标签
Node node=list.item(0); //得到第一个Weather
Node child=node.getFirstChild(); //得到Weather 标签的第一个子标签city
Node next=child.getNextSibling(); //得到city的下一个标签
while(next!=null){ //判断没有走到最后一个标签
if(next.getNodeType()==Node.ELEMENT_NODE){ //判断节点类型是ELEMENT_NODE
Node content=next.getFirstChild(); //得到该节点的第一个子节点不能为空
if(content!=null){
System.out.println(next.getFirstChild().getNodeValue().trim());//String中有一个方法trim()去掉前后空格
}
}
next=next.getNextSibling(); //得到下一个标签
}
} 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.lingzhuo;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class MySAXHander extends DefaultHandler{
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
super.characters(ch, start, length);
System.out.println(new String(ch,start,length));
}
@Override
public void endDocument() throws SAXException {
super.endDocument();
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
super.endElement(uri, localName, qName);
System.out.println(localName+qName);
}
@Override
public void startDocument() throws SAXException {
super.startDocument();
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
super.startElement(uri, localName, qName, attributes);
System.out.println(localName+qName);
}
}
package com.lingzhuo;
import java.io.File;
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 Test3 {
public static void main(String[] args) {
SAXParserFactory factory=SAXParserFactory.newInstance();
try {
SAXParser parser=factory.newSAXParser();
MySAXHander hander=new MySAXHander();
parser.parse(new File("d://11.txt"), hander);
} catch (ParserConfigurationException | SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}