xml基础小结

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("子标签名称")  得到子标签的文本

posted @ 2016-02-26 17:49  AnglusWang  阅读(136)  评论(0编辑  收藏  举报