Xml解析器(java用的)

Dom解析器

package com.dongdong.gogo;

import java.io.File;
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.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class XMlclass {
    public static void main(String[] args) {
        File f1 = new File("E:\\tianqi.txt");
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();// 定义一个dom工厂
        try {
            DocumentBuilder db = dbf.newDocumentBuilder();// 顶一个解析器
            Document doc = db.parse(f1);// 建立一个树
            NodeList weathers = doc.getElementsByTagName("Weather");// 找树中的一节点
            for (int i = 0; i < weathers.getLength(); i++) {
                Node weather = weathers.item(i);// 树中的子节点
             NamedNodeMap map = weather.getAttributes();//获取<weather name="天气">里面的name的属性
             if(map!=null)
             System.out.println(map.getNamedItem("name"));
                for (Node node = weather.getFirstChild(); node != null; node = node.getNextSibling()) {//输出便利weather的子节点的内容
                    if (node.getFirstChild() != null) {//考虑这个能否遍历标签里面的标签
                        System.out.println(node.getNodeName());//getNodeName给这个标签的名字
                        System.out.println(node.getFirstChild().getNodeValue());//getNodeValue标签子标签的内容
                        System.out.println();
                    }
                }
            }

        } 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.dong.xml;

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 TestSax {
public static void main(String[] args) {
    File f1=new File("E:\\tianqi.txt");
    SAXParserFactory sax=SAXParserFactory.newInstance();//先定义一个解析工厂 
    try {
        SAXParser parse=sax.newSAXParser();//在定义一个解析器
        MyHandle handle=new MyHandle();//解析树
        parse.parse(f1, handle);

    } 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();
    }

}
}
package com.dong.xml;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class MyHandle extends DefaultHandler{

    @Override
    public void characters(char[] arg0, int arg1, int arg2) throws SAXException {
        // TODO Auto-generated method stub
        super.characters(arg0, arg1, arg2);
        System.out.println("标签内容"+new String(arg0,arg1,arg2));

    }

    @Override
    public void endElement(String arg0, String arg1, String arg2) throws SAXException {
        // TODO Auto-generated method stub
        super.endElement(arg0, arg1, arg2);
        System.out.println("结尾标签"+arg2);
    }

    @Override
    public void endDocument() throws SAXException {
        // TODO Auto-generated method stub
        super.endDocument();
    }

    @Override
    public void startDocument() throws SAXException {
        // TODO Auto-generated method stub
        super.startDocument();
    }

    @Override
    public void startElement(String arg0, String arg1, String arg2, Attributes arg3) throws SAXException {
        // TODO Auto-generated method stub
        super.startElement(arg0, arg1, arg2, arg3);
        System.out.println("开始标签"+arg2);
        if(arg3!=null){
            System.out.println(arg3.getValue("name"));
        }
    }

}

Xml文件

<Profiles>
<Weather name="天气">
<city><center>北京</center></city>
<status1>多云</status1>
<status2>雷阵雨</status2>
<figure1>duoyun</figure1>
<figure2>leizhenyu</figure2>
<direction1>无持续风向</direction1>
<direction2>无持续风向</direction2>
<power1>≤3</power1>
<power2>≤3</power2>
<temperature1>32</temperature1>
<temperature2>21</temperature2>
<ssd>8</ssd>
<tgd1>28</tgd1>
<tgd2>28</tgd2>
<zwx>1</zwx>
<ktk>3</ktk>
<pollution>3</pollution>
<xcz>5</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>洗车后当日有降水、大风或沙尘天气,不适宜洗车</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-07-23</savedate_weather>
<savedate_life>2015-07-23</savedate_life>
<savedate_zhishu>2015-07-23</savedate_zhishu>
<udatetime>2015-07-23 08:10:12</udatetime>
</Weather>
</Profiles>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值