1-XML:可扩展的标记语言,类似HTML。用来传输数据。与此类似的还有JSON格式的。自定义标签。
XML的解析常用dom4j包。
2-XML缺点:文件大,格式复杂,解析方式多种可能不一致,传输带宽要求高等。具体和Json的对比,参考 见-http://www.cnblogs.com/SanMaoSpace/p/3139186.html
3-详细的基础知识 见-http://www.ibm.com/developerworks/cn/xml/x-newxml/
XML的解析 见下面代码:
package com;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.junit.Test;
public class TestXml {
/**
*<famliy city="Beijing">
<fumu name="luxury" age="24"></student>
<fumu name="chenchen" age="21"></student>
<haizi name="lucy" age="11"></teacher>
<haizi name="tom" age="8"></teacher>
</famliy>
*/
@Test
public void test1(){
/**
* 解析 XML文件
*/
File xml = new File("R:\\famliy.xml");
System.out.println("xml路径 : "+xml.getPath());
if(xml.exists()){
SAXReader reader = new SAXReader();
try {
//读取xml文件 到 Document(文档)
Document document = reader.read(xml);
//把 Document 转化为 字符串--打印出 xml文件 内容
System.out.println("#内容#\r"+document.asXML());
System.out.println("======================================");
//得到 root节点
Element root = document.getRootElement();
//获得 root节点的 city属性
System.out.println(root.getName()+" 节点[city-属性]: "+root.attributeValue("city"));
System.out.println("======================================");
//iterator 遍历 解析root根节点下的 fumu子节点
System.out.println("解析 fumu节点");
for(Iterator iterator=root.elementIterator("fumu");iterator.hasNext();){
Element e = (Element) iterator.next();
System.out.println(e.getName()+" name-"+e.attributeValue("name")+" ; age-"+e.attributeValue("age"));
}
System.out.println("======================================");
//iterator 遍历 解析root根节点下的 haizi子节点
System.out.println("解析 haizi节点");
for(Iterator iterator=root.elementIterator("haizi");iterator.hasNext();){
Element e = (Element) iterator.next();
System.out.println(e.getName()+" name-"+e.attributeValue("name")+" ; age-"+e.attributeValue("age"));
}
} catch (DocumentException e) {
e.printStackTrace();
}
}else{
System.out.println("文件 不存在...");
}
}
@Test
public void test2(){
/**
* XML 和 字符串
*/
String str = "<so><head><body><gg name='mm' age='10'></gg></body></head></so>";
try {
//将 字符串 str 转为 XML
Document document = DocumentHelper.parseText(str);
System.out.println(document);
System.out.println(document.asXML());
System.out.println(document.getRootElement().getName());
} catch (DocumentException e) {
e.printStackTrace();
}
}
/**
* 创建 Document
* 添加 根节点+子节点
* 给 子节点 添加值
* 将Docuemnt 写入 文件.xml
* @throws IOException
*/
@Test
public void test3() throws IOException{
//创建 Document对象
Document document = DocumentHelper.createDocument();
//添加 root 根节点
Element root = document.addElement("root");
//在 root 下 添加 子节点 father
Element father = root.addElement("childen");
//给 father 添加值
father.setText("好有爱哦");
System.out.println(document.asXML());
FileOutputStream fos = new FileOutputStream("R:\\ceshixml.xml",true);
fos.write(document.asXML().getBytes());
}
}