客户端与服务端常见的数据交换格式有xml ,json,html
移动端常用http+json
webservice采用html+xml
json
使用fastjson解析json
public static final Object parse(String text); // 把JSON文本parse为JSONObject或者JSONArray
public static final JSONObject parseObject(String text); // 把JSON文本parse成JSONObject
public static final T parseObject(String text, Class clazz); // 把JSON文本parse为JavaBean
public static final JSONArray parseArray(String text); // 把JSON文本parse成JSONArray
public static final List parseArray(String text, Class clazz); //把JSON文本parse成JavaBean集合
public static final String toJSONString(Object object); // 将JavaBean序列化为JSON文本
public static final String toJSONString(Object object, boolean prettyFormat); // 将JavaBean序列化为带格式的JSON文本
public static final Object toJSON(Object javaObject); 将JavaBean转换为JSONObject或者JSONArray。
xml格式
解析xml一般三种:dom4j sax
dom4j解析xml
package com.xml;
import com.sun.xml.internal.ws.util.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.OutputStreamWriter;
import java.util.List;
/**
* xml解析方式, Dom4j、Sax、Pull
* dom4j不适合大文件的解析,因为它是一下子将文件加载到内存中,所以有可能出现内存溢出,
* sax是基于事件来对xml进行解析的,所以他可以解析大文件的xml,
*/
public class App {
/**
* 创建节点
*/
public static void add(String fileName) {
Document document = DocumentHelper.createDocument();
//根节点
Element beans = document.addElement("beans");
beans.addComment("zhu shi");
//命名空间
beans.addNamespace("context", "http://www.springframework.org/schema/context");
beans.addNamespace("bean", "http://www.springframework.org/schema/bean");
beans.addNamespace("jdbc", "http://www.springframework.org/schema/jsbc");
beans.addNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
beans.addAttribute("xsi:schemaLocation", "http://www.springframework.org/schema/beans \n " +
" http://www.springframework.org/schema/beans/spring-beans.xsd \n " +
" http://www.springframework.org/schema/context \n " +
" http://www.springframework.org/schema/context/spring-context.xsd\n " +
" http://www.springframework.org/schema/aop \n " +
" http://www.springframework.org/schema/aop/spring-aop.xsd");
//节点 <context:component-scan base-package="com.paopao"/>
beans.addElement("context:component")
.addAttribute("basepackage", "com.paopao");
//bean节点
beans.addElement("bean")
.addAttribute("id", "UserService")
.addAttribute("class", "com.demo.service");
//property
beans.addElement("bean")
.addAttribute("id", "userController")
.addAttribute("class", "com.demo.controller")
.addElement("property")
.addAttribute("name", "destroy")
.addAttribute("name", "init").setText("init");
//子节点
Element bean = beans.addElement("bean")
.addAttribute("id", "user")
.addAttribute("class", "com.demo.pojo");
bean.addElement("property")
.addAttribute("name", "username")
.addAttribute("value", "xiaoming");
bean.addElement("property")
.addAttribute("name", "password")
.addAttribute("value", "123456");
try {
OutputFormat outputFormat = OutputFormat.createPrettyPrint();
outputFormat.setEncoding("UTF-8");
XMLWriter xmlWriter = new XMLWriter(new FileWriter(new File(fileName)), outputFormat);
xmlWriter.write(document);
xmlWriter.close();
} catch (Exception e) {
e.printStackTrace();
}
return;
}
/**
* 解析xml文件
*/
public static void xml(String fileName) {
SAXReader reader = new SAXReader();
try {
Document document = reader.read(new File(fileName));
Element beans = document.getRootElement();
//获取根目录下的子元素
List<Element> list = beans.elements();
for (Element e : list) {
if ("bean".equals(e.getName())) {
String beanName = e.attributeValue("id");
String beanPackage = e.attributeValue("class");
List<Element> element = e.elements();
if (!element.equals(null)) {
for (Element ele : element) {
String name = ele.attributeValue("name");
String value = ele.attributeValue("value");
String text = ele.getText();
System.out.println(name+value+text);
}
}
System.out.println(beanPackage + "." + beanName);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
App.add("d:/file-mianshi/b.xml");
App.xml("d:/file-mianshi/b.xml");
}
}
Java反射
动态获取这个类的所有信息。包括java对象的属性,方法,构造函数等
反射获取类
Class.forName("com.demo.pojo.User");
User.class;
(new User()).getClass();
实例化类(构造对象)
无参构造
Class<?> clazz = Class.forName("...")
Object user = clazz.newInstance();
有参构造
Class<?> clazz = Class.forName("...")
Constructor<?>constructor = forName.getConstructor(String.class, String.class);
User user = (User) constructor.newInstance("name", "xiaoming");
反射创建api
getDeclaredMethods []
getReturnType()
getParameterTypes()
getDeclaredFields()
setAccessible //允许访问私有成员
springioc的简单实现
读取xml文件
装配bean
反射赋值
新建xml文档
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="student" class="com.xml.pojo.Student">
<property name = "name" value="网瘾少年"></property>
</bean>
<bean id="teacher" class="com.xml.pojo.Teacher">
<constructor-arg name="name" value="雷电法王"></constructor-arg>
<constructor-arg name="student" ref="student"></constructor-arg>
</bean>
<bean id="play" class="com.xml.service.Play">
<property name="teacher" ref="teacher"></property>
<property name="student" ref="student"></property>
</bean>
</beans>
解析xml
package com.xml;
import com.xml.service.Play;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.beans.ConstructorProperties;
import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author cailonghao
*/
public class Cxml {
/**
* 拿到装载bean的实例
*/
public static Map xml(String file) {
//存放bean
Map<String, Class<?>> map1 = new HashMap<>();
//存放节点
Map<String, Object> map2 = new HashMap<>();
//存放对象
Map<String, Object> map3 = new HashMap<>();
SAXReader reader = new SAXReader();
try {
//获取根节点并遍历子节点
Document document = reader.read(new File(file));
Element beans = document.getRootElement();
List<Element> list = beans.elements();
for (Element element : list) {
String beanName = element.attributeValue("id");
String beanPackage = element.attributeValue("class");
Class<?> clazz = Class.forName(beanPackage);
map1.put(beanName, clazz);
map2.put(beanName, element.elements());
}
//拿到没引用的bean
for (Map.Entry<String, Object> entry : map2.entrySet()) {
String beanName = entry.getKey();
List<Element> elements = (List<Element>) entry.getValue();
for (Element element : elements) {
if ("property".equals(element.getName())) {
String name = element.attributeValue("name");
Object value = element.attributeValue("ref");
if (value == null) {
value = element.attributeValue("value");
Object object = map1.get(beanName).newInstance();
Field field = object.getClass().getDeclaredField(name);
field.setAccessible(true);
field.set(object, value);
map3.put(beanName, object);
}
}
}
}
;
//拿有构造的
for (Map.Entry<String, Object> entry : map2.entrySet()) {
String beanName = entry.getKey();
List<Element> elements = (List<Element>) entry.getValue();
List<Object> ls = new ArrayList<>();
int i = 1;
for (Element element : elements) {
if ("constructor-arg".equals(element.getName())) {
String name = element.attributeValue("name");
Object value = element.attributeValue("value");
if (value == null) {
value = map3.get(element.attributeValue("ref"));
}
ls.add(0, beanName);
ls.add(i, value);
i++;
}
}
if (!(ls.isEmpty())) {
Class<?> clazz = map1.get(ls.get(0));
Constructor constructor = clazz.getConstructor(ls.get(1).getClass(), ls.get(2).getClass());
Object object = constructor.newInstance(ls.get(1), ls.get(2));
map3.put((String) ls.get(0), object);
}
}
;
//拿有引用bean的
for (Map.Entry<String, Object> entry : map2.entrySet()) {
String beanName = entry.getKey();
List<Element> elements = (List<Element>) entry.getValue();
for (Element element : elements) {
if ("property".equals(element.getName())) {
if (!(map3.containsKey(beanName))) {
String name = element.attributeValue("name");
Object value = element.attributeValue("value");
if (value == null) {
value = map3.get(element.attributeValue("ref"));
}
Object object = map1.get(beanName).newInstance();
Field field = object.getClass().getDeclaredField(name);
field.setAccessible(true);
field.set(object, value);
map3.put(beanName, object);
}
}
}
}
} catch (Exception e) {
//todo
e.printStackTrace();
} finally {
return map3;
}
}
public static void main(String[] args) {
Map map = Cxml.xml("d:/file-mianshi/c.xml");
System.out.println(map)
}
}