XML
简介
可扩展标记语言
特性:
- 平台无关性
- 自我描述性
为什么学习XML?
- 网络数据传输
- 数据存储
- 配置文件
XML文件
.xml文件是保存xml数据的一种方式。
xml解析
引入Dom4j包之后
本地文件解析
package com.java.demo;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
/**
* 本地xml文档解析
*/
public class Demo1 {
public static void main(String[] args) throws IOException, DocumentException {
// 1. 获取输入流
FileInputStream fis = new FileInputStream("demo1.xml");
// 2. 创建xml读取对象
SAXReader sr = new SAXReader();
// 3. 读取并得到文档对象
Document doc = sr.read(fis);
// 4. 通过文档获取根元素
Element root = doc.getRootElement();
// 解析元素
System.out.println(root.getName());
Element book = root.element("book");
Element name = book.element("name");
System.out.println(name.getText());
List<Element> list = root.elements();
for(int i=0;i<list.size();i++){
Element book1 = list.get(i);
System.out.println(book1.attributeValue("id"));
System.out.println(book1.elementText("name"));
System.out.println(book1.elementText("info"));
}
fis.close();
}
}
网页文件解析
package com.java.demo;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
/**
* 网页xml文档解析
*/
public class Demo2 {
public static void main(String[] args) throws IOException, DocumentException {
String phone = "15223113816";
// 1. 获取xml资源的输入流
URL url = new URL("http://apis.juhe.cn/mobile/get? phone="+phone+"&dtype=xml&key=9f3923e8f87f1ea50ed4ec8c39cc9253");
URLConnection con = url.openConnection();
InputStream is = con.getInputStream();
// 2. 创建一个xml读取对象
SAXReader sr = new SAXReader();
// 3. 通过读取对象读取xml, 并返回文档对象
Document doc = sr.read(is);
// 4. 获取根节点
Element root = doc.getRootElement();
// 5. 解析内容
Element result = root.element("result");
String code = root.elementText("resultcode");
if("200".equals(code)){
String province = result.elementText("province");
String city = result.elementText("city");
if(city.equals(province)){
System.out.println(phone+"的号码归属地为:"+city);
}else{
System.out.println(phone+"的号码归属地为:"+province+city);
}
}
}
}
通过xPath+文档对象, 查找所有的name节点
package com.java.demo;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
/**
* 本地xml文档解析, xPath查找节点
*/
public class Demo3 {
public static void main(String[] args) throws IOException, DocumentException {
// 1. 获取输入流
FileInputStream fis = new FileInputStream("demo1.xml");
// 2. 创建xml读取对象
SAXReader sr = new SAXReader();
// 3. 读取并得到文档对象
Document doc = sr.read(fis);
// 4. 通过xPath+文档对象, 查找所有的name节点
List<Node> list = doc.selectNodes("//book[@id=1001]//name");
for (Node node : list) {
System.out.println(node.getName()+":"+node.getText());
}
fis.close();
}
}
生成XML
package com.java.demo;
import jdk.internal.util.xml.XMLStreamException;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.XMLWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
public class Demo5 {
public static void main(String[] args) throws IOException, XMLStreamException {
// 1. 通过文档帮助器生成文档
Document doc = DocumentHelper.createDocument();
// 2. 创建根节点
Element books = doc.addElement("books");
// 3. 丰富子节点
for(int i=0;i<100;i++){
Element book = books.addElement("book");
Element name = book.addElement("name");
Element info = book.addElement("info");
book.addAttribute("id", 100+i+"");
name.setText("种苹果"+i);
info.setText("种苹果的故事"+i);
}
// 4. 创建一个文件输出流
FileOutputStream fos = new FileOutputStream("demo5.xml");
// 5. 转换为xml文件输出流
XMLWriter xw = new XMLWriter(fos);
// 6. 写出文档
xw.write(doc);
// 7. 释放资源
xw.close();
System.out.println("执行完毕");
}
}
XStream
package com.java.demo;
import com.thoughtworks.xstream.XStream;
import java.util.Objects;
public class Demo6 {
public static void main(String[] args) {
Person p = new Person();
p.setAge("18");
p.setName("张三");
XStream xs = new XStream();
xs.alias("Person", Person.class);
String xml = xs.toXML(p);
System.out.println(xml);
}
static class Person{
private String name;
private String age;
public Person(String name, String age) {
this.name = name;
this.age = age;
}
public Person() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return Objects.equals(name, person.name) && Objects.equals(age, person.age);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age='" + age + '\'' +
'}';
}
}
}
Json 解析
Gson
package com.java.demo1;
import com.google.gson.Gson;
public class Demo1 {
public static void main(String[] args) {
Gson g = new Gson();
Book b = new Book(1, "苹果","种苹果");
String s = g.toJson(b);
System.out.println(s);
}
}
package com.java.demo1;
import com.google.gson.Gson;
public class Demo2 {
public static void main(String[] args) {
// {"id":1,"name":"苹果","info":"种苹果"}
Gson g = new Gson();
Book book = g.fromJson("{\"id\":1,\"name\":\"苹果\",\"info\":\"种苹果\"}", Book.class);
System.out.println(book);
}
}
FastJson
package com.java.demo1;
import com.alibaba.fastjson.JSON;
public class Demo3 {
public static void main(String[] args) {
Book b = new Book(2, "test", "test");
String s = JSON.toJSONString(b);
System.out.println(s);
}
}
package com.java.demo1;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
public class Demo4 {
public static void main(String[] args) {
// {"id":2,"info":"test","name":"test"}
Book b = JSON.parseObject("{\"id\":2,\"info\":\"test\",\"name\":\"test\"}", Book.class);
System.out.println(b);
}
}
package com.java.demo1;
import com.alibaba.fastjson.JSON;
import java.util.List;
public class Demo5 {
public static void main(String[] args) {
// ["1","2","3"]
List<String> strings = JSON.parseArray("[\"1\",\"2\",\"3\"]", String.class);
System.out.println(strings.toString());
}
}