java操作xml和json

java操作xml和json

1. XML

1.1 XML简介

XML, 可扩展标记语言(extensible markup language), 是一种数据表示格式, 可以藐视非常复制的数据结构, 用于传输和存储数据

特点:

  • 纯文本, 默认UTF-8编码
  • 可嵌套, 适合表示结构化数据
  • 一般以文件形式, 如:book.xml

1.2 Dom

java解析XML:

  • Document: 代表整个XML文档
  • Element: 元素
  • Attribute: 属性
  • Text: 文本内容
  • Comment: 注释

Java Dom核心API:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse("/Users/lingjing/IdeaProjects/hadooptest/pom.xml");
Element root = doc.getDocumentElement();

java DOM API:

  • 将XML解析为DOM
  • 可在内存中完整标识XML数据结构
  • 解析速度慢, 内存占用大

Dom示例

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;

public class DOMTest {

    static final String XML_URL = "http://rss.sina.com.cn/tech/internet/home28.xml";

    public static void main(String[] args) throws Exception {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse("/Users/lingjing/IdeaProjects/hadooptest/pom.xml");
        printNode(doc, 0);
    }

    static void printNode(Node n, int indent) {
        String tab = "";
        for (int i = 0; i < indent; i++) {
//            System.out.print(' ');
            tab += "\t";
        }
        System.out.print(tab);
        switch (n.getNodeType()) {
            case Node.DOCUMENT_NODE:
                System.out.println("Document: " + n.getNodeName());
                break;
            case Node.ELEMENT_NODE:
                System.out.println("Element: " + n.getNodeName());
                break;
            case Node.TEXT_NODE:
                System.out.println("Text: " + n.getNodeName() + " = " + n.getNodeValue());
                break;
            case Node.ATTRIBUTE_NODE:
                System.out.println("Attr: " + n.getNodeName() + " = " + n.getNodeValue());
                break;
            case Node.CDATA_SECTION_NODE:
                System.out.println("CDATA: " + n.getNodeName() + " = " + n.getNodeValue());
                break;
            case Node.COMMENT_NODE:
                System.out.println("Comment: " + n.getNodeName() + " = " + n.getNodeValue());
                break;
            default:
                System.out.println("NodeType: " + n.getNodeType() + ", NodeName: " + n.getNodeName());
        }
        for (Node child = n.getFirstChild(); child != null; child = child.getNextSibling()) {
            printNode(child, indent + 1);
        }
    }
}

1.3 SAX

SAX: Simple Api for XML, 基于事件的API

SAX解析会触发一系列事件:

  • startDocument
  • startElement
  • Characters: 文本
  • endElement
  • endDocument

SAX解析XML:

  • 一种流式解析XML的API
  • 通过事件触发, 速度快
  • 调用方通过回调方法获取数据

SAX示例:

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

public class SAXSample {

    static final String XML_URL = "http://rss.sina.com.cn/tech/internet/home28.xml";

    public static void main(String[] args) throws Exception {
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser saxParser = spf.newSAXParser();
        saxParser.parse("/Users/lingjing/IdeaProjects/hadooptest/pom.xml", new MyHandler());
    }
}



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

public class MyHandler extends DefaultHandler {

    void print(Object... objs) {
        for (Object obj : objs) {
            System.out.print(obj);
            System.out.print(" ");
        }
        System.out.println();
    }

    @Override
    public void startDocument() throws SAXException {
        print("start document");
    }

    @Override
    public void endDocument() throws SAXException {
        print("end document");
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        print("start element:", localName, qName);
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        print("end element:", localName, qName);
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        print("characters:", new String(ch, start, length));
    }

    @Override
    public void error(SAXParseException e) throws SAXException {
        print("error:", e);
    }

}

1.4 Jackson

使用Jackson:

  • 开源XML读写工具
  • 可在XML和JavaBean之间相互转换
  • API接口简单
  • 可定制序列化

maven依赖地址:

<dependency>
			<groupId>com.fasterxml.jackson.dataformat</groupId>
			<artifactId>jackson-dataformat-xml</artifactId>
			<version>2.9.0</version>
</dependency>
<dependency>
			<groupId>org.codehaus.woodstox</groupId>
			<artifactId>woodstox-core-asl</artifactId>
			<version>4.4.1</version>
</dependency>

读取XML:把xml变成java对象

XmlMapper mapper = new XmlMapper();
Book book = mapper.readValue(xml, Book.class);

写入XML:把java对象变成xml文件, 并返回字符串

XmlMapper mapper = new XmlMapper();
String xml = mapper.writeValueAsString(book);

2. fastjson

maven配置

<dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.46</version>
</dependency>

代码测试:

import lombok.Data;

@Data
public class User {
    private String name;
    private int age;
}


public class TestUser {

    public static void main(String[] args) {
        User user = new User();
        user.setAge(20);
        user.setName("ajing");
        // 把User对象变成json字符串
        String jstr = JSON.toJSONString(user);
        System.out.println(jstr);
      	// 把字符串变成User对象
        User u1 = JSON.parseObject(jstr, User.class);
        System.out.println(u1 instanceof  User);
        System.out.println(u1.getAge());
        System.out.println(u1.getName());
    }
}
posted @ 2019-06-06 14:05  阿晶  阅读(935)  评论(0编辑  收藏  举报