欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

xml文档的解析

程序员文章站 2022-05-30 10:21:38
...

 

<?xml version="1.0" encoding="utf-8" ?>
<web-app>
    <servlet>
        <servlet-name>login</servlet-name>
        <servlet-class>rui.server.httpserver02.servlet.LoginServlet</servlet-class>
    </servlet>
    <mapping>
        <name>login</name>
        <url-pattern>/log</url-pattern>
        <url-pattern>/login</url-pattern>
        <url-pattern>/g</url-pattern>
    </mapping>
</web-app>
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;
import java.util.List;

public class test1 {
    public static void main(String[] args) throws IOException, ParserConfigurationException, SAXException {
       //获取解析工厂
        SAXParserFactory factory=SAXParserFactory.newInstance();
        //从工厂获取解析器
        SAXParser parser=factory.newSAXParser();
        //加载文档 注册处理器
        //编写处理器
        PersonHandler handler=new PersonHandler();
        parser.parse(Thread.currentThread().getContextClassLoader().getResourceAsStream("rui/server/httpserver02/xml/web.xml"),handler);
        List<Person> personList=handler.getList();
        for (Person person:personList
             ) {
            System.out.println("姓名:"+person.getName()+",年龄:"+person.getAge());

        }


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

import java.util.ArrayList;
import java.util.List;

public class PersonHandler extends DefaultHandler {
    public List<Person> getList() {
        return list;
    }

    private List<Person> list=null;
    private Person person=null;
    private String tag=null;
    @Override
    public void startDocument() throws SAXException {
        super.startDocument();
        list=new ArrayList<>();
    }
    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        super.startElement(uri, localName, qName, attributes);
        System.out.println(qName);
        tag=qName;
        if(qName.equals("person")){
            person=new Person();
        }
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        super.characters(ch, start, length);
        String info=new String(ch,start,length).trim();
        if(tag!=null&&tag.equals("name")){
            person.setName(info);
        }else if (tag!=null&&tag.equals("age")){
            Integer age=Integer.valueOf(info);
            person.setAge(age);
        }

    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        super.endElement(uri, localName, qName);
        System.out.println(qName);
        if(qName.equals("person")){
            list.add(person);
        }
        tag=null;
    }
    @Override
    public void endDocument() throws SAXException {
        super.endDocument();
        System.out.println("文件读取结束");

    }


}

 

相关标签: xml