java 解析web.xml_javaweb高级第一章 解析xml

1.本章目标

了解xml

掌握xml格式的编写

掌握jdom,dom4解析xml

2.xml

xml是一种可扩展的标记语言

特点:

它的标签没有被预定义,需要自定义标签

它主要用于传输数据,而不是显示数据

具有自我描述性

还可以使用dtt,schema定义xml的格式(了解)

html:超文本标记语言

特点:

预定义标签,如:input,br......

主要用于显示页面

共同点:

都是一个树形结构,由一个根节点,N个子节点组成

.3.xml格式编写

格式:

前导区:xml版本,编码格式的定义

数据区:只有一个根节点,所有的节点必须完结(/)

4.解析xml(重点)

对xml文件的读取,写入,添加,修改,删除节点

1.dom解析

2.sax解析

3.jdom解析

4.dom4解析

特点:

dom:

将xml文件一次性全部加载到内存中

优点:

1.形成了树形结构,有助于更好的理解,掌握,且代码容易编写

2.解析过程中,树形结构保存在内存中,方便修改

缺点:

1.由于文件是一次性读取,所以对内存的消耗比较大

2.如果文件过大,容易影响解析性能,有可能会造成内存的溢出

sax:

顺序模式的访问

优点:

1.采用事件驱动模式,对内存消耗比较小

2.适用于只处理xml文件中的数值

缺点:

1.编码比较麻烦

2.很难同时访问xml文件中的多处不同数据

5.jdom解析xml

简化与xml的交互,并且比dom实现更快

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

import java.io.File;

import java.io.IOException;

import java.util.List;

import org.jdom2.Document;

import org.jdom2.Element;

import org.jdom2.JDOMException;

import org.jdom2.input.SAXBuilder;public classDemo1 {public static voidmain(String[] args) throws JDOMException, IOException {

SAXBuilder builder= newSAXBuilder();//通过指定的xml文件,构建出document对象

Document document = builder.build(new File("C:\\Users\\Administrator\\HBuilderProjects\\web高级\\stus.xml"));//获取xml文档中的根节点对象

Element root =document.getRootElement();

List childrens =root.getChildren();for(Element e:childrens){

System.out.println("获取id属性:"+e.getAttributeValue("id"));

System.out.println("获取name子节点的值"+e.getChildText("name"));

System.out.println("获取sex子节点的值"+e.getChildText("sex"));

System.out.println("获取age子节点的值"+e.getChildText("age"));

}

}

}

jdom解析xml

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

importjava.io.File;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importorg.jdom2.Document;importorg.jdom2.Element;importorg.jdom2.output.Format;importorg.jdom2.output.XMLOutputter;public classDemo2 {public static void main(String[] args) throwsFileNotFoundException, IOException {//jdom创建xml文件

Element stus= new Element("stus");

Element stu= new Element("stu");

Element no= new Element("no");

Element name= new Element("name");

Element sex= new Element("sex");

Element age= new Element("age");//创建document对象

Document doc=newDocument(stus);//将stu节点添加到stus节点

stus.addContent(stu);//将子节点添加到stu节点

stu.addContent(no);

stu.addContent(name);

stu.addContent(sex);

stu.addContent(age);//为各个节点赋值

stu.setAttribute("id","10");

no.setText("S100");

name.setText("zhangsan");

sex.setText("女");

age.setText("30");//格式化xml文件

Format format =Format.getCompactFormat();

format.setEncoding("utf-8");//设置编码

format.setIndent(" ");//设置缩进//创建xmloutput对象

XMLOutputter out = newXMLOutputter(format);

out.output(doc,new FileOutputStream("C:\\Users\\Administrator\\HBuilderProjects\\web高级\\stus1.xml"));

System.out.println("创建成功");

}

}

jdom创建xml文档

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

importjava.io.File;importjava.io.FileOutputStream;importjava.io.IOException;importorg.jdom2.Document;importorg.jdom2.Element;importorg.jdom2.JDOMException;importorg.jdom2.input.SAXBuilder;importorg.jdom2.output.XMLOutputter;public classDemo3 {public static void main(String[] args) throwsJDOMException, IOException {//jdom创建节点//创建一个bulider对象

SAXBuilder builder = newSAXBuilder();//创建document对象

Document document = builder.build(new File("C:\\Users\\Administrator\\HBuilderProjects\\web高级\\stus.xml"));//创建节点对象

Element stu = new Element("stu");

Element no= new Element("no");

Element name= new Element("name");

Element sex= new Element("sex");

Element age= new Element("age");//设置关系

stu.addContent(no);

stu.addContent(name);

stu.addContent(sex);

stu.addContent(age);//设置文本值

stu.setAttribute("id","11");

no.setText("S101");

name.setText("lisi");

sex.setText("女");

age.setText("31");//获取根节点对象

Element root =document.getRootElement();

root.addContent(stu);

document.setContent(root);//重写写入文件

XMLOutputter out = newXMLOutputter();

out.output(document,new FileOutputStream("C:\\Users\\Administrator\\HBuilderProjects\\web高级\\stus.xml"));

System.out.println("创建成功");

}

}

jdom创建子节点文档

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

importjava.io.File;importjava.io.FileOutputStream;importjava.io.IOException;importjava.util.List;importorg.jdom2.Document;importorg.jdom2.Element;importorg.jdom2.JDOMException;importorg.jdom2.input.SAXBuilder;importorg.jdom2.output.XMLOutputter;public classDemo4 {public static void main(String[] args) throwsJDOMException, IOException {//jdom修改节点//builder对象

SAXBuilder builder = newSAXBuilder();

Document document= builder.build(new File("C:\\Users\\Administrator\\HBuilderProjects\\web高级\\stus.xml"));//获取根节点

Element root=document.getRootElement();

List childrens =root.getChildren();for(Element e:childrens){

String id= e.getAttributeValue("id");if("10".equals(id)){

e.getChild("sex").setText("男");

document.setContent(root);

XMLOutputter out= newXMLOutputter();

out.output(document,new FileOutputStream("C:\\Users\\Administrator\\HBuilderProjects\\web高级\\stus.xml"));

System.out.println("修改节点完成");break;

}

}

}

}

jdom修改节点

需要一个jdom2.jar

6.dom4解析xml(重点)

dom4j的性能最优

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.xml2;importjava.io.File;importjava.util.List;importorg.dom4j.Document;importorg.dom4j.DocumentException;importorg.dom4j.Element;importorg.dom4j.io.SAXReader;public classDemo1 {public static void main(String[] args) throwsDocumentException {//dom4j读取xml//reader对象

SAXReader reader = newSAXReader();

Document document=reader.read(new File("C:\\Users\\Administrator\\HBuilderProjects\\web高级\\stus.xml"));//根节点对象

Element root =document.getRootElement();

List elements =root.elements();for(Element e : elements){

System.out.println("获取id属性: "+e.attributeValue("id"));

System.out.println("获取name子节点的值 "+e.elementText("name"));

System.out.println("获取sex子节点的值 "+e.elementText("sex"));

System.out.println("获取age子节点的值 "+e.elementText("age"));

}

System.out.println("成功");

}

}

dom4j读取xml

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.xml2;importjava.io.File;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.UnsupportedEncodingException;importorg.dom4j.Document;importorg.dom4j.DocumentHelper;importorg.dom4j.Element;importorg.dom4j.io.XMLWriter;public classDemo2 {public static void main(String[] args) throwsIOException {//dom4j创建xml//文档对象

Document document =DocumentHelper.createDocument();//创建根节点

Element root= DocumentHelper.createElement("stus");

document.setRootElement(root);//创建子节点(stu)

Element stu=root.addElement("stu");//创建孙子节点(no,name,sex,age)

Element no=stu.addElement("no");

Element name=stu.addElement("name");

Element sex=stu.addElement("sex");

Element age=stu.addElement("age");//为节点赋值

stu.addAttribute("id", "100");

no.setText("S300");

name.setText("xiaoming");

sex.setText("女");

age.setText("12");//写入文件

XMLWriter writer = new XMLWriter(new FileOutputStream("C:\\Users\\Administrator\\HBuilderProjects\\web高级\\stus_dom4j.xml"));

writer.write(document);

writer.close();

System.out.println("创建成功");

}

}

dom4j创建xml

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.xml2;importjava.io.File;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.UnsupportedEncodingException;importorg.dom4j.Document;importorg.dom4j.DocumentException;importorg.dom4j.Element;importorg.dom4j.io.SAXReader;importorg.dom4j.io.XMLWriter;public classDemo3 {public static void main(String[] args) throwsDocumentException, IOException {//dom4j新增节点

SAXReader reader = newSAXReader();

Document document=reader.read(new File("C:\\Users\\Administrator\\HBuilderProjects\\web高级\\stus_dom4j.xml"));//获取xml文档中的根节点对象

Element root =document.getRootElement();//给根节点添加子节点//创建节点对象

Element stu = root.addElement("stu");

Element no=stu.addElement("no");

Element name= stu.addElement("name");

Element sex= stu.addElement("sex");

Element age= stu.addElement("age");//为节点赋值

stu.addAttribute("id", "200");

no.setText("S200");

name.setText("xiaohing");

sex.setText("女");

age.setText("18");

document.setRootElement(root);//写入

XMLWriter writer = new XMLWriter(new FileOutputStream("C:\\Users\\Administrator\\HBuilderProjects\\web高级\\stus_dom4j.xml"));

writer.write(document);

writer.close();

System.out.println("新增成功");

}

}

dom4j新增节点

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.xml2;importjava.io.File;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.UnsupportedEncodingException;importjava.util.List;importorg.dom4j.Document;importorg.dom4j.Element;importorg.dom4j.io.SAXReader;importorg.dom4j.io.XMLWriter;public classDemo4 {public static void main(String[] args) throwsException {//dom4j修改xml节点

SAXReader reader = newSAXReader();

Document document= reader.read(new File("C:\\Users\\Administrator\\HBuilderProjects\\web高级\\stus_dom4j.xml"));

Element rootElement=document.getRootElement();

List elements =rootElement.elements();for(Element e :elements){

String id= e.attributeValue("id");if("200".equals(id)){

Element age=e.element("age");

System.out.println(age);

age.setText("20");//把根节点设置到文档中

document.setRootElement(rootElement);

XMLWriter writer= new XMLWriter(new FileOutputStream("C:\\Users\\Administrator\\HBuilderProjects\\web高级\\stus_dom4j.xml"));

writer.write(document);

writer.close();

System.out.println("修改xml节点完成");break;

}

}

}

}

dom4j修改xml节点

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.xml2;importjava.io.File;importjava.io.FileOutputStream;importjava.util.List;importorg.dom4j.Document;importorg.dom4j.Element;importorg.dom4j.io.SAXReader;importorg.dom4j.io.XMLWriter;public classDemo55 {public static void main(String[] args) throwsException {//dom4j删除xml节点

SAXReader reader = newSAXReader();

Document document= reader.read(new File("C:\\Users\\Administrator\\HBuilderProjects\\web高级\\stus_dom4j.xml"));

Element rootElement=document.getRootElement();

List elements =rootElement.elements();for(Element e :elements){

String id= e.attributeValue("id");if("200".equals(id)){

rootElement.remove(e);//把根节点设置到文档中

document.setRootElement(rootElement);

XMLWriter writer= new XMLWriter(new FileOutputStream("C:\\Users\\Administrator\\HBuilderProjects\\web高级\\stus_dom4j.xml"));

writer.write(document);

writer.close();

System.out.println("删除xml节点完成");break;

}

}

}

}

dom4j删除xml节点

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值