博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
xml数据解析和生成
阅读量:5303 次
发布时间:2019-06-14

本文共 13194 字,大约阅读时间需要 43 分钟。

java中xml的解析方式有许多,有java自带的DOM、SAX,android中的PULL,其它的还有DOM4J、JDOM等。

本文简要讲述DOM、SAX、PULL三种方式。

1.DOM方法

缺点:此方法会将所有数据都读取到内存中,内存消耗大,数据量太大容易造成OOM,而且此方法的效率较低,所以不建议在移动开发中使用。

优点:以树形的结构访问,容易理解,编码简单,可随机访问所需要的内容。

2.SAX方法:

从开头顺序读取直至结尾,读取和处理同步。

缺点:编码难度较大

优点:解析快,占用内存小,适合在移动设备上使用

3.PULL方法:

此方法与SAX方法类似,适合在移动设备中使用,android系统内部默认使用此方法。

与SAX的区别是,此方法能随时结束解析,而SAX不能。


 

先展示一下接下来要解析的xml数据和对应的类:

book.xml

冰与火之歌
乔治马丁
2014
89
安徒生童话
2004
77
English
View Code

Book.java

public class Book {    private String id;    private String name;    private String author;    private String year;    private String price;    private String language;    public String getId() {        return id;    }    public void setId(String id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getAuthor() {        return author;    }    public void setAuthor(String author) {        this.author = author;    }    public String getYear() {        return year;    }    public void setYear(String year) {        this.year = year;    }    public String getPrice() {        return price;    }    public void setPrice(String price) {        this.price = price;    }    public String getLanguage() {        return language;    }    public void setLanguage(String language) {        this.language = language;    }    @Override    public String toString() {        return "Book{" +                "id='" + id + '\'' +                ", name='" + name + '\'' +                ", author='" + author + '\'' +                ", year='" + year + '\'' +                ", price='" + price + '\'' +                ", language='" + language + '\'' +                '}';    }}
View Code

1、PULL

解析:

public static ArrayList
getBooks(InputStream is) throws Exception { ArrayList
books = null; Book book = null; // 创建一个xml解析的工厂 XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); // 获得xml解析类的引用 XmlPullParser parser = factory.newPullParser(); parser.setInput(is, "UTF-8"); // 获得事件的类型 int eventType = parser.getEventType(); while (eventType != XmlPullParser.END_DOCUMENT) { switch (eventType) { case XmlPullParser.START_DOCUMENT: books = new ArrayList<>(); break; case XmlPullParser.START_TAG: if ("book".equals(parser.getName())) { book = new Book(); // 取出属性值 String id = parser.getAttributeValue(0); book.setId(id); } else if ("name".equals(parser.getName())) { String name = parser.nextText();// 获取该节点的内容 Show.log("name:"+name); book.setName(name); } else if ("author".equals(parser.getName())) { String author = parser.nextText(); book.setAuthor(author); }else if("year".equals(parser.getName())){ String year=parser.nextText(); book.setYear(year); }else if ("price".equals(parser.getName())){ String price=parser.nextText(); book.setPrice(price); }else if("language".equals(parser.getName())){ String language=parser.nextText(); book.setLanguage(language); } break; case XmlPullParser.END_TAG: if ("book".equals(parser.getName())) { books.add(book); book = null; } break; } eventType = parser.next(); } return books; }

 

生成:

public static void save(List
books, OutputStream out) throws Exception { XmlSerializer serializer = Xml.newSerializer(); serializer.setOutput(out, "UTF-8"); serializer.startDocument("UTF-8", true); serializer.startTag(null, "bookstore"); for (Book book : books) { Show.log(book.toString()); serializer.startTag(null, "book"); serializer.attribute(null, "id", book.getId() + ""); serializer.startTag(null, "name"); serializer.text(book.getName()); serializer.endTag(null, "name"); serializer.startTag(null, "author"); serializer.text(book.getAuthor() + ""); serializer.endTag(null, "author"); serializer.startTag(null,"year"); serializer.text(book.getYear()); serializer.endTag(null,"year"); serializer.startTag(null,"language"); serializer.text(book.getLanguage()+""); serializer.endTag(null,"language"); serializer.endTag(null, "book"); } serializer.endTag(null, "bookstore"); serializer.endDocument(); }

 


 

 

2、DOM

解析:

public static void domXmlParser() {        ArrayList
bookLists = new ArrayList
(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); try { DocumentBuilder db = dbf.newDocumentBuilder(); Document document = db.parse("book.xml"); NodeList bookList = document.getElementsByTagName("book"); for (int i = 0; i < bookList.getLength(); i++) { Node book = bookList.item(i); Book bookEntity = new Book(); NamedNodeMap attrs = book.getAttributes(); for (int j = 0; j < attrs.getLength(); j++) { Node attr = attrs.item(j); if (attr.getNodeName().equals("id")) { bookEntity.setId(attr.getNodeValue()); } } NodeList childNodes = book.getChildNodes(); for (int k = 0; k < childNodes.getLength(); k++) { if (childNodes.item(k).getNodeType() == Node.ELEMENT_NODE) { String name = childNodes.item(k).getNodeName(); String value = childNodes.item(k).getFirstChild().getNodeValue(); if (name.equals("name")) { bookEntity.setName(value); } else if (name.equals("author")) { bookEntity.setAuthor(value); } else if (name.equals("year")) { bookEntity.setYear(value); } else if (name.equals("price")) { bookEntity.setPrice(value); } else if (name.equals("language")) { bookEntity.setLanguage(value); } } } bookLists.add(bookEntity); bookEntity = null; } } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }

 

生成:

public void DOMCreateXML() {        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();        DocumentBuilder db = null;        try {            db = dbf.newDocumentBuilder();        } catch (ParserConfigurationException e) {            e.printStackTrace();        }        Document document = db.newDocument();        document.setXmlStandalone(true);        Element bookstore = document.createElement("bookStore");        Element book = document.createElement("book");        Element name = document.createElement("name");        name.setTextContent("小王子");        book.appendChild(name);        book.setAttribute("id", "1");        bookstore.appendChild(book);        document.appendChild(bookstore);        TransformerFactory tff = TransformerFactory.newInstance();        try {            Transformer tf = tff.newTransformer();            tf.setOutputProperty(OutputKeys.INDENT, "yes");            tf.transform(new DOMSource(document), new StreamResult(new File(                    "books1.xml")));        } catch (TransformerConfigurationException e) {            e.printStackTrace();        } catch (TransformerException e) {            e.printStackTrace();        }    }

 

3、SAX

 解析:

public class SAXParserHandler extends DefaultHandler {    String value = null;    Book book = null;    private ArrayList
bookList = new ArrayList
(); public ArrayList
getBookList() { return bookList; } int bookIndex = 0; /** * 用来标识解析开始 */ @Override public void startDocument() throws SAXException { super.startDocument(); } /** * 用来标识解析结束 */ @Override public void endDocument() throws SAXException { super.endDocument(); } /** * 解析xml元素 */ @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { super.startElement(uri, localName, qName, attributes); if (qName.equals("book")) { bookIndex++; book = new Book(); String value = attributes.getValue("id"); int num = attributes.getLength(); for(int i = 0; i < num; i++){ if (attributes.getQName(i).equals("id")) { book.setId(attributes.getValue(i)); } } } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { super.endElement(uri, localName, qName); if (qName.equals("book")) { bookList.add(book); book = null; } else if (qName.equals("name")) { book.setName(value); } else if (qName.equals("author")) { book.setAuthor(value); } else if (qName.equals("year")) { book.setYear(value); } else if (qName.equals("price")) { book.setPrice(value); } else if (qName.equals("language")) { book.setLanguage(value); } } @Override public void characters(char[] ch, int start, int length) throws SAXException { // TODO Auto-generated method stub super.characters(ch, start, length); String temp = new String(ch, start, length); if (!temp.trim().equals("")) { value = temp; } }}
public static void saxXmlParser(){        SAXParserFactory factory = SAXParserFactory.newInstance();        try {            SAXParser parser = factory.newSAXParser();            SAXParserHandler handler = new SAXParserHandler();            parser.parse("book.xml", handler);        } catch (ParserConfigurationException e) {            e.printStackTrace();        } catch (SAXException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }    }

生成:

public void SAXCreateXML() {        SAXTransformerFactory tff = (SAXTransformerFactory) SAXTransformerFactory                .newInstance();        try {            TransformerHandler handler = tff.newTransformerHandler();            Transformer tr = handler.getTransformer();            tr.setOutputProperty(OutputKeys.ENCODING, "UTF-8");            tr.setOutputProperty(OutputKeys.INDENT, "yes");            File f = new File("books2.xml");            if (!f.exists()) {                f.createNewFile();            }            Result result = new StreamResult(new FileOutputStream(f));            handler.setResult(result);            handler.startDocument();            AttributesImpl attr = new AttributesImpl();            handler.startElement("", "", "bookstore", attr);            attr.clear();            attr.addAttribute("", "", "id", "", "1");            handler.startElement("", "", "book", attr);            attr.clear();            handler.startElement("", "", "name", attr);            handler.characters("小王子".toCharArray(), 0, "小王子".length());            handler.endElement("", "", "name");                    handler.endElement("", "", "book");            handler.endElement("", "", "bookstore");            handler.endDocument();        } catch (TransformerConfigurationException e) {            e.printStackTrace();        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        } catch (SAXException e) {            e.printStackTrace();        }    }

 

转载于:https://www.cnblogs.com/zhisuoyu/p/5491642.html

你可能感兴趣的文章
思达BI软件Style Intelligence实例教程—房地产分析
查看>>
Unity 3D 如何修改新建脚本中的 C# 默认创建的 Script 脚本格式
查看>>
Unity 5.4 测试版本新特性---因吹丝停
查看>>
7.5 文件操作
查看>>
DFS-hdu-2821-Pusher
查看>>
Spring事务管理的三种方式
查看>>
MyEclipse中将普通Java项目convert(转化)为Maven项目
查看>>
node js 安装.node-gyp/8.9.4 权限 无法访问
查看>>
Java_正则表达式
查看>>
Linux内核分析——第二周学习笔记
查看>>
windows基本命令
查看>>
Qt图片显示效率的比较(转)
查看>>
VMware中CentOS设置静态IP
查看>>
剑指Offer_编程题_7
查看>>
js 变量大小写
查看>>
Linux系统的启动原理
查看>>
JDesktopPane JInternalFrames
查看>>
错误The request sent by the client was syntactically incorrect ()的解决
查看>>
Java基础知识学习(九)
查看>>
redis在windows下总是报错,就是下面的错误,这是哪里出错了
查看>>