加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

XML系列:(7)XML解析-Dom4j的DOM解析方式更新XML

发布时间:2020-12-16 08:37:56 所属栏目:百科 来源:网络整理
导读:Dom4j工具,是非官方的,不在jdk中。 使用步骤: 1)导入dom4j的核心包。 dom4j-1.6.1.jar 2)编写Dom4j读取xml文件的代码 1、更新XML 1.1、写出内容到xml文档 packagecom.rk.xml.g_dom4j_write;importjava.io.File;importjava.io.FileOutputStream;importorg

Dom4j工具,是非官方的,不在jdk中。

使用步骤:

1)导入dom4j的核心包。 dom4j-1.6.1.jar

2)编写Dom4j读取xml文件的代码


1、更新XML


1.1、写出内容到xml文档

packagecom.rk.xml.g_dom4j_write;

importjava.io.File;
importjava.io.FileOutputStream;

importorg.dom4j.Document;
importorg.dom4j.io.SAXReader;
importorg.dom4j.io.XMLWriter;

/**
*第一个写出内容到xml文档
*@authorRK
*
*/
publicclassDemo01
{
	publicstaticvoidmain(String[]args)throwsException
	{
		//一、读取或创建一个Document对象
		Documentdoc=newSAXReader().read(newFile("./src/animals.xml"));
		
		//二、修改Document对象内容
		
		//三、把修改后的Document对象写出到xml文档中
		//指定文件输出的位置
		FileOutputStreamoutStream=newFileOutputStream("D:/rk/result.xml");
		//1.创建写出对象
		XMLWriterwriter=newXMLWriter(outStream);
		//2.写出Document对象
		writer.write(doc);
		//3.关闭流
		writer.close();
		System.out.println("执行结束!");
	}
}


1.2、写出内容到xml文档的细节

packagecom.rk.xml.g_dom4j_write;

importjava.io.File;
importjava.io.FileOutputStream;
importorg.dom4j.Document;
importorg.dom4j.io.OutputFormat;
importorg.dom4j.io.SAXReader;
importorg.dom4j.io.XMLWriter;

/**
*讨论写出内容到xml文档的细节
*@authorRK
*
*/
publicclassDemo02
{
	publicstaticvoidmain(String[]args)throwsException
	{
		Documentdoc=newSAXReader().read(newFile("./src/animals2.xml"));
		
		/**
		*1.指定写出的格式
		*/
//		OutputFormatformat=OutputFormat.createCompactFormat();//紧凑的格式.去除空格换行.项目上线的时候
		OutputFormatformat=OutputFormat.createPrettyPrint();//漂亮的格式.有空格和换行.开发调试的时候
		/**
		*2.指定生成的xml文档的编码
		*同时影响了xml文档保存时的编码和xml文档声明的encoding的编码(xml解析时的编码)
		*结论:使用该方法生成的xml文档避免中文乱码问题。
		*/
		format.setEncoding("UTF-8");
		
		//指定文件输出的位置
		FileOutputStreamoutStream=newFileOutputStream("D:/rk/result.xml");
		//1.创建写出对象
		XMLWriterwriter=newXMLWriter(outStream,format);
		//2.写出Document对象
		writer.write(doc);
		//3.关闭流
		writer.close();
		System.out.println("执行结束!");
	}
}



1.3、修改xml内容:添加、修改、删除

packagecom.rk.xml.g_dom4j_write;

importjava.io.File;
importjava.io.FileOutputStream;

importorg.dom4j.Document;
importorg.dom4j.DocumentHelper;
importorg.dom4j.Node;
importorg.dom4j.Element;
importorg.dom4j.Attribute;
importorg.dom4j.Text;
importorg.dom4j.io.SAXReader;
importorg.dom4j.io.XMLWriter;
importorg.dom4j.io.OutputFormat;
importorg.junit.Test;

/**
*修改xml内容
*增加:文档,标签,属性
*修改:属性值,文本
*删除:标签,属性
*@authorRK
*
*/
publicclassDemo03
{
	/**
	*增加:文档,标签,属性
	*/
	@Test
	publicvoidtestAdd()throwsException
	{
		//1.创建文档
		Documentdoc=DocumentHelper.createDocument();
		
		//2.增加标签
		ElementrootElement=doc.addElement("ContactList");
		ElementcontactElement=rootElement.addElement("Contact");
		ElementnameElement=contactElement.addElement("Name");
		nameElement.setText("小明");
		
		//3.增加属性
		contactElement.addAttribute("id","c001");
		contactElement.addAttribute("region","北京");
		
		//把修改后的Document对象写出到xml文档中
		FileOutputStreamout=newFileOutputStream("D:/rk/contact.xml");
		OutputFormatformat=OutputFormat.createPrettyPrint();
		format.setEncoding("utf-8");
		XMLWriterwriter=newXMLWriter(out,format);
		writer.write(doc);
		writer.close();
		System.out.println("执行结束!");
	}

	/**
	*修改:属性值,文本
	*/
	@Test
	publicvoidtestModify()throwsException
	{
		Documentdoc=newSAXReader().read(newFile("./src/animals.xml"));
		/**
		*方案一:修改属性值1.得到标签对象2.得到属性对象3.修改属性值
		*/
		//1.1得到标签对象
		ElementcatElement=doc.getRootElement().element("Cat");
		//1.2得到属性对象
		AttributecatAttr=catElement.attribute("id");
		//1.3修改属性值
		catAttr.setValue("c100");
		
		/**
		*方案二:修改属性值
		*/
		//2.1得到标签对象
		ElementdogElement=doc.getRootElement().element("Dog");
		//2.2通过增加同名属性的方法,修改属性值
		dogElement.addAttribute("id","d100");
		
		/**
		*修改文本1.得到标签对象2.修改文本
		*/
		ElementnameElement=doc.getRootElement().element("Cat").element("Home");
		nameElement.setText("第六宇宙");
		
		FileOutputStreamout=newFileOutputStream("D:/rk/Animals.xml");
		OutputFormatformat=OutputFormat.createPrettyPrint();
		format.setEncoding("utf-8");
		XMLWriterwriter=newXMLWriter(out,format);
		writer.write(doc);
		writer.close();
		System.out.println("执行结束!");
	}

	/**
	*删除:标签,属性
	*/
	@Test
	publicvoidtestDelete()throwsException
	{
		Documentdoc=newSAXReader().read(newFile("./src/animals.xml"));
		/**
		*1.删除标签1.1得到标签对象1.2删除标签对象
		*/
		//1.1得到标签对象
		ElementnameElement=doc.getRootElement().element("Cat").element("Home");
		//1.2删除标签对象
		nameElement.detach();
//		nameElement.getParent().remove(nameElement);
		
		/**
		*2.删除属性2.1得到属性对象2.2删除属性
		*/
		ElementcatElement=doc.getRootElement().element("Cat");
		//2.1得到属性对象
		AttributeidAttr=catElement.attribute("id");
		//2.2删除属性
		idAttr.detach();
//		idAttr.getParent().remove(idAttr);
		
		FileOutputStreamout=newFileOutputStream("D:/rk/animals.xml");
		OutputFormatformat=OutputFormat.createPrettyPrint();
		format.setEncoding("utf-8");
		XMLWriterwriter=newXMLWriter(out,format);
		writer.write(doc);
		writer.close();
		System.out.println("执行结束!");
	}
}



2、思维导图

wKioL1c2J2mQaTfAAAHmv1_GZM0598.png

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读