java高级工程师-----JSON和XML的使用
来源:互联网 发布:sql截掉字符串 编辑:程序博客网 时间:2024/06/01 16:04
- JSON和XML的比较
- JSON的理解
- JSON的基本使用
1)//转换成为JSONObject对象 JSONObject jsonObj = new JSONObject(jsonStr);2)//json转换为字符串数据public static void main(String[] args) throws JSONException {
//创建JSONObject对象
JSONObject json = new JSONObject();
//向json中添加数据
json.put("username", "wanglihong");
json.put("height", 12.5);
json.put("age", 24);
//创建JSONArray数组,并将json添加到数组
JSONArray array = new JSONArray();
array.put(json);
//转换为字符串
String jsonStr = array.toString();
System.out.println(jsonStr);
}
3)对集合的转换
public static void main(String[] args)throws JSONException{
//初始化ArrayList集合并添加数据
List<String> list = new ArrayList<String>();
list.add("username");
list.add("age");
list.add("sex");
//初始化HashMap集合并添加数组
Map map = new HashMap();
map.put("bookname", "CSS3实战");
map.put("price", 69.0);
//初始化JSONArray对象,并添加数据
JSONArray array = new JSONArray();
array.put(list);
array.put(map);
//生成的JSON字符串为:[["username","age","sex"],{"price":69,"bookname":"CSS3实战"}]
}
- XML的理解
- XML的作用区域
- XML的基本使用
解析xml的API :DOM(文档对象模型,核心是节点,可以上下导航,数据保存在内存中) -------------------用得比较多
SAX(顺序读取文件,不能上下读取,事件驱动,用得比较少)
通过w3c的包DOM解析xml的代码:
import org.w3c.dom.Document
public class DomHandler{
public void read(String fileName) throws Exception{
DocumentBuliderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
Document document = builder.parse(this.getClass().getResourceAsStream("/"+fileName));//到这一步整个xml内容已经被保存在内存中了
//读取根节点
Element root=document.getDocumentElement();
//获取指定节点的所有所有节点
NodeList nodeList=root.getElementsByTagName("database");
for(int i=0;i<nodeList.getLength();i++){
Node node=nodeList.item(i);
NameNodeMap attributes = node.getAttributes();
for(int j=0;j<attributes.getLength();j++){
Node attribute= attributes.item(j);
}
}
}
}
通过DOM4j来解析xml:
public class Dom4jHander{
public void read(String fileName) throws Exception{
SAXReader saxReader=new SAXReader();
Document document=saxReader.read(this.getClass().getResourceAsStream("/"+fileName);
Element root= document.getRootElement();
List<Element> childElements = root.elements("database");
for(Element child:childElements){
//获取属性 不知道属性名称是的遍历方法
List<Attribute> attributes=child.attributes();
for(Attribute attribute:attributes){
//attribute.getName() 和attribute.getValue();
}
//获取已知属性的值
String name=child.attributeValue("name");
}
}
//添加xml内容
public void add() throws Exception{
//创建xml文档
Document docment=DocumentHelper.createDocment();
//添加根节点
Element root = document.addElement("dataSource");
//节点下添加子节点
Element database=root.addElement("database");
//3.将document写出到文件
OutputFormat of= OutputFormat.createPrettyPrint();
of.setEncoding("GBK");
XMLWriter xw=new XMLWriter(new FileOutputStream("db.xml"),of);
xw.write(document);
}
}
- java高级工程师-----JSON和XML的使用
- Java高级工程师的基本要求
- Xml和Json相互转换的java
- python json和java json的使用
- java高级工程师--------nosql和关系型数据库的重点知识
- json,Xml的使用
- java高级工程师-----struts的ActionProxy
- java高级工程师-------------Velocity的应用
- java高级工程师的技术进阶
- java高级工程师
- Java高级工程师
- 使用js对json数据和xml数据的转化
- Http的使用及XML和JSON数据解析
- 使用js对json数据和xml数据的转化
- xml和JSON格式相互转换的Java实现
- Java和JavaScript对XML与JSON的支持
- xml和JSON格式相互转换的Java实现
- xml和JSON相互转换的Java实现
- careercup2.4
- 非模态对话框的创建与销毁
- 数学专项number_theory:UVa 10090
- win7 时间和时区修改不了的问题
- 21、C++类的继承
- java高级工程师-----JSON和XML的使用
- Bitmap处理之Bitmap缓存
- 开篇文字
- 跨站测试与利用中的绕过技术
- static关键字
- Linux命令行下常用svn命令
- 面向对象
- 基于UDP的端与端之间通讯Demo
- 22、C++中的虚继承以及虚基类