xml:可扩展标记语言
作用:用来存储和传输数据,在web页面中作为配置文件
Dom解析:比较复杂,用到大量的抽象类和接口,内存消耗比较大
好处:可以实现随机访问标签
public class DomTest
{
public static void main(String[] args) throws Exception
{
ArrayList<Student> al=new ArrayList<Student>();
al.add(new Student(1002,"张思",25));
al.add(new Student(1002,"张思",25));
al.add(new Student(1002,"张思",25));
al.add(new Student(1002,"张思",25));
File file=new File("src/com/aowin/dom/dom.xml");
// write(file,al);
read(file);
}
//写xml文档
//思路:先在内存中创建一棵文档树
private static void write(File file,ArrayList<Student> al) throws Exception
{
//1.创建解析工厂 DocumentBuilderFactory
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
//2.创建解析器 DocumentBuilder 通过解析工厂
DocumentBuilder builder=factory.newDocumentBuilder();
//3.创建文档树 Document 通过解析器
Document document=builder.newDocument();
//4.创建标签 通过Document
/*Element root=document.createElement("classroom");//classroom标签
Element student=document.createElement("student");//student标签
student.setAttribute("time", "2016-5-4");//设置标签属性
Element no=document.createElement("no");//学号标签
no.setTextContent("10001");//设置标签内容
Element name=document.createElement("name");//姓名标签
name.setTextContent("张三");
Element age=document.createElement("age");
age.setTextContent("20");
//5.确定标签之间的关系
root.appendChild(student);//将student作为root子标签
student.appendChild(no);//将no作为student子标签
student.appendChild(name);
student.appendChild(age);
document.appendChild(root);//将root作为整棵树的根标签
*/
Element root=document.createElement("classroom");//根标签
for(int i=0;i<al.size();i++){
Element student=document.createElement("student");
student.setAttribute("time", "2016-5-4");//设置标签属性
Element no=document.createElement("no");//学号标签
no.setTextContent(String.valueOf(al.get(i).no));//设置标签内容
Element name=document.createElement("name");//姓名标签
name.setTextContent(al.get(i).name);
Element age=document.createElement("age");
age.setTextContent(String.valueOf(al.get(i).age));
root.appendChild(student);//将student作为root子标签
student.appendChild(no);//将no作为student子标签
student.appendChild(name);
student.appendChild(age);
}
document.appendChild(root);//将root作为整棵树的根标签
//6.将文档树传递到文件中 用到传输工具
//首先创建传输工厂
TransformerFactory transformer=TransformerFactory.newInstance();
//通过传输工厂创建传输工具 transformer
Transformer tf=transformer.newTransformer();
DOMSource source=new DOMSource(document);//文档树源
StreamResult result=new StreamResult(file);//传输的目的地
//调用方法开始传输
tf.transform(source, result);
}
//从xml文件中读取内容
public static void read(File file) throws Exception
{
//1.创建解析器
DocumentBuilder builder=DocumentBuilderFactory.newInstance().newDocumentBuilder();
//2.解析指定文件成一棵文档树
Document document=builder.parse(file);
//3.遍历树
//第一种方式:从根标签依次往下遍历
/*Node root=document.getFirstChild();//拿到根标签
NodeList studentList=root.getChildNodes();//拿到根标签下所有子标签student
for(int i=0;i<studentList.getLength();i++)
{
Node student=studentList.item(i);//拿到单个的student标签
//向下转型
Element e=(Element)student;
String value=e.getAttribute("time");//通过属性名称获得属性值
System.out.println(value);
//获得student标签下的所有子标签
NodeList childList=student.getChildNodes();
for(int j=0;j<childList.getLength();j++)
{
//获得单个的student下的子标签
Node child=childList.item(j);
//获得标签的内容
String content=child.getTextContent();
System.out.println(content);
}
}*/
//第二种方式:随机访问标签
//随机访问student标签
NodeList studentList=document.getElementsByTagName("student");//获得所有的student标签
for(int i=0;i<studentList.getLength();i++)
{
Node student=studentList.item(i);//拿到单个的student标签
//向下转型
Element e=(Element)student;
String value=e.getAttribute("time");//通过属性名称获得属性值
System.out.println(value);
//获得student标签下的所有子标签
NodeList childList=student.getChildNodes();
for(int j=0;j<childList.getLength();j++)
{
//获得单个的student下的子标签
Node child=childList.item(j);
//获得标签的内容
String content=child.getTextContent();
System.out.println(content);
}
}
}
}
Dom4j:基于Dom解析的,比Dom解析要方便,用到类都是实体类
思路与Dom解析一致:
public class Dom4jTest
{
public static void main(String[] args) throws Exception
{
//实现一次写多个对象?
File file=new File("src/com/aowin/dom4j/dom4j.xml");
ArrayList<Student> al=new ArrayList<Student>();
al.add(new Student(1001,"李四",30));
al.add(new Student(1002,"李大",31));
al.add(new Student(1003,"李二",32));
al.add(new Student(1004,"李三",33));
// write(file,al);
read(file);
}
//写文档
private static void write(File file,ArrayList<Student> al) throws IOException
{
//1.创建文档树Document
Document document=DocumentHelper.createDocument();
//2.添加标签
Element root=document.addElement("classroom");//根标签
//给根标签设置子标签
/*Element student=root.addElement("student");
//给标签设置属性
student.addAttribute("time", "2016-5-4");
Element no=student.addElement("no");//给student标签设置子标签
no.setText("1001");
student.addElement("name").setText("小红");//添加name标签并设置内容
student.addElement("age").setText("30");*/
for(int i=0;i<al.size();i++){
Student stu=al.get(i);
Element student=root.addElement("student");
student.addAttribute("time", "2016-5-4");
student.addElement("no").setText(String.valueOf(stu.no));
student.addElement("name").setText(stu.name);
student.addElement("age").setText(String.valueOf(stu.age));
}
//3.将文档传入到文件中
//第一种方式:
/* FileWriter fw=new FileWriter(file);
OutputFormat format=new OutputFormat();//创建格式编码对象
format.setEncoding("gbk");
XMLWriter writer=new XMLWriter(fw, format);
writer.write(document);//将文档树写进流中
fw.flush();
fw.close();*/
//第二种方式:
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file),"utf-8"));
document.write(bw);
bw.flush();
bw.close();
}
//从xml文件中读取数据
public static void read(File file) throws Exception
{
//从指定文件中读取文档树
//首先创建一个解析器
SAXReader reader=new SAXReader();
//解析指定文件
Document document=reader.read(file);
//遍历文档树
Element root=document.getRootElement();//先拿到根标签
//第一种方式:利用标签下的迭代器
/*Iterator<Element> it=root.elementIterator();//获得根标签下的迭代器
while(it.hasNext())
{
Element student=it.next();//拿到单个的student标签
String value=student.attributeValue("time");//获得标签下的属性值
System.out.println(value);
//获得student标签所有的子标签
Iterator<Element> it1=student.elementIterator();
while(it1.hasNext())
{
Element child=it1.next();//获得student标签单个的子标签
String text=child.getText();
System.out.println(text);
}
}*/
//第二种方式:通过标签下的List列表
List<Element> studentList=root.elements();//获得所有的student标签
for(int i=0;i<studentList.size();i++)
{
Element student=studentList.get(i);//获得单个的student标签
System.out.println(student.attributeValue("time"));//获得属性值
//获得student标签下的所有子标签
List<Element> childList=student.elements();
for(int j=0;j<childList.size();j++)
{
Element child=childList.get(j);//单个的子标签
//获得标签的内容
System.out.println(child.getText());
}
}
}
}