码迷,mamicode.com
首页 > 其他好文 > 详细

xml基础小结

时间:2016-02-26 18:47:40      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:

XML基础

                   1)XML的作用

                                     1.1 作为软件配置文件

                                     1.2 作为小型的“数据库”

                   2)XML语法(由w3c组织规定的)

                                     标签: 

                                                        标签名不能以数字开头,中间不能有空格,区分大小写。有且仅有一个根标签。

                                     属性:             

                                                        可有多个属性,但属性值必须用引号(单引号或双引号)包含,但不能省略,也不能单                                                  双混用。

                                     文档声明:

                                                        <?xml version="1.0" encoding="utf-8"?>       

                                                        encoding="utf-8": 打开或解析xml文档时的编码

                                                        注意:

                                               保存xml文档时的编码 和 解析xml文档时的编码要保持一致,才能避免中文乱码问题!

                   3)XML解析

                                     程序读取或操作xml文档

 

                                     两种解析方式:   DOM解析   vs   SAX解析

 

                                     DOM解析原理:一次性把xml文档加载成Document树,通过Document对象得到节点对象,通过节点对象访问xml文档内容(标签,属性,文本,注释)。

 

                                     Dom4j工具(基于DOM解析原理):

                                                        读取xml文档:

                                                                     Document doc = new SAXReader().read("xml文件");

 

                                                        节点:

                                                                           nodeIterator();  所有节点

                                                        标签:

                                                                           element("名称")   指定名称的第一个子标签对象

                                                                           elementIterator("名称");  指定名称的所有子标签对象

                                                                           elements();  所有子标签对象

                                                        属性:

                                                                           attributeValue(“名称”) 指定名称的属性值

                                                                           attribute("名称")   指定名称的属性对象

                                                                                             getName()  属性名称

                                                                                             getValue()  属性值

                                                                           atributeIterator()   所有属性对象(Iterator)

                                                                           attributes()       所有属性对象(List)

                                                        文本:

                                                                           getText()  得到当前标签的文本

                                                                           elementText("子标签名称")  得到子标签的文本

xml基础小结

标签:

原文地址:http://www.cnblogs.com/jesonjason/p/5221174.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!