代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
dom4j操作xml
作者:
xulong1516519
/ 发布于
2012/10/22
/
691
在传输过程中经常用到xml
<div>static String xmlPath= "xmlFile.xml";</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>public static void createXML() {//完成基本的xml要素的生成,以及最后xml文件的存储</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span></div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>Document doc= DocumentHelper.createDocument();</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>Element root= doc.addElement("root");</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>setBeanValue(root);</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>try {</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>FileOutputStream output= new FileOutputStream(xmlPath);//也可以通过writer来添加但是注意乱麻问题要</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>XMLWriter xWriter= new XMLWriter(output, OutputFormat.createPrettyPrint());//对xml文件进行格式化</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>xWriter.write(doc);</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>output.flush();</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>xWriter.flush();</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>output.close();</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>xWriter.close();</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>} catch (IOException e) {</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>e.printStackTrace();</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>} <div><span class="Apple-tab-span" style="white-space:pre"> </span>} <div></div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>public static void setBeanValue(Node parentNode) {//对具体的节点的生成</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span></div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>Person[] persons= new Person[3];<span class="Apple-tab-span" style="white-space:pre"> </span></div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>for(int i= 0; i< persons.length; i++) {</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>persons[i]= new Person();</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>persons[i].setAge(i+"");</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>persons[i].setSex("男");</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>} <div><span class="Apple-tab-span" style="white-space:pre"> </span>Element personsNode= ((Element)parentNode).addElement("persons");</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>for(int i= 0;i< 3; i++) {</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span></div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>Element person = personsNode.addElement("person");</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>Element age= person.addElement("age");</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>Element sex= person.addElement("sex");</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>setXMLValue(age, persons[i]);</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>setXMLValue(sex, persons[i]);</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>} <div><span class="Apple-tab-span" style="white-space:pre"> </span></div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>} <div><span class="Apple-tab-span" style="white-space:pre"> </span></div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>public static void setXMLValue(Node nodeName, Object obj) {//通过反射获取具体的属性值</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span></div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>Class<Person> classType= (Class<Person>) obj.getClass();</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>try {</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>Field filed= classType.getDeclaredField(nodeName.getName());</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>filed.setAccessible(true);</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>nodeName.setText(filed.get(obj)+"");</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span></div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>} catch (IllegalAccessException e) {</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>e.printStackTrace();</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>} catch (SecurityException e) {</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>e.printStackTrace();</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>} catch (NoSuchFieldException e) {</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>e.printStackTrace();</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>} <div><span class="Apple-tab-span" style="white-space:pre"> </span>} <div><span class="Apple-tab-span" style="white-space:pre"> </span></div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>public static void parserXML() {</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span></div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>SAXReader reader= new SAXReader();</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>try {</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>InputStream input= new FileInputStream(xmlPath);</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>Document doc= reader.read(input);</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>Element root= doc.getRootElement();</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>Iterator<Element> it= root.elementIterator();</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>while(it.hasNext()) {</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span></div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>Element element= it.next();</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>readNode(element);</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>} <div><span class="Apple-tab-span" style="white-space:pre"> </span></div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>} catch (FileNotFoundException e) {</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>e.printStackTrace();</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>} catch (IOException e) {</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>e.printStackTrace();</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>} catch (DocumentException e) {</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>e.printStackTrace();</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>} <div><span class="Apple-tab-span" style="white-space:pre"> </span>} <div><span class="Apple-tab-span" style="white-space:pre"> </span></div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>public static void readNode(Node node) {</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span></div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>Element element= (Element)node;//persons</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>Iterator<Element> childElement= element.elementIterator();</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>while(childElement.hasNext()) {//person</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span></div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>Element ele= childElement.next();//age sex</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>if(ele.elementIterator()!= null) {</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>if(!"".equals(ele.getText().trim())) {</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>System.out.println(ele.getName()+"-->"+ele.getText().trim());</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>} <div><span class="Apple-tab-span" style="white-space:pre"> </span>readNode(ele);</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>} <div><span class="Apple-tab-span" style="white-space:pre"> </span>} <div><span class="Apple-tab-span" style="white-space:pre"> </span>} <div><span class="Apple-tab-span" style="white-space:pre"> </span></div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>public static void main(String[] A) {</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span></div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>createXML();//生成xml</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>parserXML();//解析xml</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>} <div>} <div></div>
试试其它关键字
dom4j操作xml
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
xulong1516519
贡献的其它代码
(
4
)
.
复制域账号到剪贴板
.
文件分割+AES加密+Base64编码
.
socket接收输入流问题
.
dom4j操作xml
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3