1. XML文档声明
<?xml version="1.0" encoding="UTF-8"?>2. 标记 ( 元素 / 标签 / 节点)
2. 标记 ( 元素 / 标签 / 节点) XML文档,由一个个的标记组成.
语法:
开始标记(开放标记): <标记名称>
结束标记(闭合标记): </标记名称>
标记名称: 自定义名称,必须遵循以下命名规则:
1.名称可以含字母、数字以及其他的字符
2.名称不能以数字或者标点符号开始
3.名称不能以字符 “xml”(或者 XML、Xml)开始
4.名称不能包含空格,不能包含冒号(:)
5.名称区分大小写
标记内容: 开始标记与结束标记之间 ,是标记的内容.
例如 ,: <name>郑阿彬</name>
3. 一个XML文档中, 必须有且且仅允许有一个根标记.
4. 标记可以嵌套, 但是不允许交叉.
5. 标记的层级称呼 (子标记, 父标记 , 兄弟标记, 后代标记 ,祖先标记)
6. 标记名称 允许重复
7. 标记除了开始和结束 , 还有属性.
8. 注释
注释不能写在文档文档声明前
注释不能嵌套注释
格式: 注释开始: <!--
注释结束: -->
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book id="1001">
<name>金苹果</name>
<info>锄禾日当午,苹果很辛苦</info>
</book>
<book id="1002">
<name>银苹果</name>
<info>一二三四五,我们吃的好辛苦</info>
</book>
</books>
XML解析
package com.kkb.demo1;
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;
public class Demo1 {
public static void main(String[] args) throws IOException, DocumentException {
//1. 获取输入流
FileInputStream fis = new FileInputStream("F://Demo.xml");
//2. 创建XML读取对象
SAXReader sr = new SAXReader();
//3. 读取并得到文档对象
Document doc = sr.read(fis);
//4. 通过文档获取根元素
Element root = doc.getRootElement();
//5. 开始解析元素
System.out.println(root.getName());
//操作一波
//Element book = root.element("book");
//Element name = book.element("name");
//System.out.println(name.getText());
List<Element> es = root.elements();
for (int i =0;i<es.size();i++){
Element book = es.get(i);
System.out.println(book.attributeValue("id"));
System.out.println(book.elementText("name"));
System.out.println(book.elementText("info"));
System.out.println("------------------------");
}
fis.close();
}
}
package com.kkb.demo1;
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;
public class Demo2 {
public static void main(String[] args) throws IOException, DocumentException {
//1. 获取到XML自愿的输入流
String phone = "13697933361";
URL url = new URL("http://apis.juhe.cn/mobile/get?phone="+phone+"&dtype=xml&key=9f3923e8f87f1ea50ed4ec8c39cc9253");
URLConnection conn = url.openConnection();
InputStream is = conn.getInputStream();
//2. 创建一个XML读取对象
SAXReader sr = new SAXReader();
//3. 通过读取对象 读取XML数据,并返回文档对象
Document doc = sr.read(is);
//4. 获取根节点
Element root = doc.getRootElement();
//5. 解析内容
String code = root.elementText("resultcode");
if ("200".equals(code)){
Element result= root.element("result");
String proy = result.elementText("province");
String city = result.elementText("city");
if (proy.equals(city)){
System.out.println("手机号码归属地为:"+city);
}else {
System.out.println("手机号码归属地为:"+proy+city);
}
}else {
System.out.println("请输入正确的手机号码");
}
}
}
XPATH解析
package com.kkb.demo1;
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;
public class Demo3 {
public static void main(String[] args) throws IOException, DocumentException {
//1. 获取输入流
FileInputStream fis = new FileInputStream("F://Demo.xml");
//2. 创建XML读取对象
SAXReader sr = new SAXReader();
//3. 读取并得到文档对象
Document doc = sr.read(fis);
//4. 通过文档对象+xpath ,查找所有与的name节点
//查多个
List<Node> nams = doc.selectNodes("//book//name");
for (int i=0;i<nams.size();i++){
System.out.println(nams.get(i).getName());
System.out.println(nams.get(i).getText());
}
//查一个
Node n = doc.selectSingleNode("//book[@id=1002]//name");
System.out.println(n.getName()+" "+n.getText());
fis.close();
}
}
XML生成
package com.kkb.demo1;
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 Demo {
public static void main(String[] args) throws IOException {
//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");
name.setText(i+"小红帽");
Element info = book.addElement("info");
info.setText(i+"被狼外婆吃掉的心路经历");
book.addAttribute("id",100+i+"");
}
//4. 创建一个文件输出流
FileOutputStream fos = new FileOutputStream("f://books.xml");
//5. 将输出流转换为XML输出流
XMLWriter xw = new XMLWriter(fos);
//6. 写出文档
xw.write(doc);
//7. 释放资源
xw.close();
System.out.println("执行完毕");
}
}
package com.kkb.demo1;
import com.thoughtworks.xstream.XStream;
public class Demo6 {
public static void main(String[] args) {
Person p = new Person("张三","十八");
//XStream使用
//1. 创建XStream对象
XStream x = new XStream();
//2. 修改某个类型生成的根节点(可选的,默认的包名.类名)
x.alias("person",Person.class);
//3. 传入对象,开始生成
String xml = x.toXML(p);
System.out.println(xml);
}
static class Person{
private String name;
private String age;
public Person() {
}
public Person(String name, String age) {
this.name = name;
this.age = age;
}
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;
}
}
}
JSON
Gson
将对象转换成字符串
package com.kkb.deno2;
import com.google.gson.Gson;
public class Demo2 {
public static void main(String[] args) {
//1. 创建Gson对象
Gson g = new Gson();
//2. 转换
Book b = new Book("111","静苹果","种植苹果真辛苦");
String s = g.toJson(b);
System.out.println(s);
}
}
将JSON字符串转换为对象
package com.kkb.deno2;
import com.google.gson.Gson;
public class Demo1 {
public static void main(String[] args) {
//1. 创建Gson对象
Gson g = new Gson();
//2. 转换 : {"id":"111","name":"静苹果","info":"种植苹果真辛苦"}
Book b = g.fromJson("{\"id\":\"111\",\"name\":\"金苹果\",\"info\":\"种植苹果真辛苦\"}",Book.class);
System.out.println(b.getId());
}
}
数组
package com.kkb.deno2;
import com.google.gson.Gson;
import java.util.HashMap;
import java.util.List;
public class Demo3 {
public static void main(String[] args) {
//1. 创建Gson对象
Gson g = new Gson();
//2. 转换 : "100",{"id":"100","name":"金苹果","info ":"狼来了木有","oage":["车河","大佬","没有"]}
HashMap hashMap = g.fromJson("{\"id\":\"100\",\"name\":\"金苹果\",\"info \":\"狼来了木有\",\"page\":[\"车河\",\"大佬\",\"没有\"]}", HashMap.class);
List page = (List) hashMap.get("page");
System.out.println(page.get(1));
}
}
FastJson
将对象转换为JSON字符串
package com.kkb.deno2;
import com.alibaba.fastjson.JSON;
public class Demo4 {
public static void main(String[] args) {
Book book = new Book("1003", "我的", "没想到,没下次");
//1. 转换
String json = JSON.toJSONString(book);
System.out.println(json);
}
}
将JSON字符串转换为对象
package com.kkb.deno2;
import com.alibaba.fastjson.JSON;
public class Demo5 {
public static void main(String[] args) {
//1.转换 : {"id":"1003","info":"没想到,没下次","name":"我的"}
Book book = JSON.parseObject("{\"id\":\"1003\",\"info\":\"没想到,没下次\",\"name\":\"我的\"}", Book.class);
System.out.println(book.getId());
}
}