XML配置文件
XML的語法
1.xml是由自定義的標(biāo)簽組成
<開始標(biāo)簽>標(biāo)簽體</結(jié)束標(biāo)簽>
<自閉合標(biāo)簽/>
2.xml文件的語法
1)必須要有一個文檔聲明 <?xml version="1.0" encoding="UTF-8" ?>2)只有一個根標(biāo)簽3)特殊字符 如< > & 必須使用特殊的比較進(jìn)行代替< < 小于> > 大于& & 和號' ' 單引號" " 引號4)CDATA區(qū),數(shù)據(jù)可以被原樣顯示,不被當(dāng)做標(biāo)簽解析<![CADATA[內(nèi)容]]>3.示例
<?xml version="1.0" encoding="utf-8" ?> <!--本文件用于描述多個學(xué)生的信息--> <!--根標(biāo)簽--> <students xmlns="http://www.itheima.com"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.itheima.com student.xsd"><!--第一個學(xué)生信息--><student id ="1"><name>張三</name><age>23</age><!-->; < ;& ;' ;";有特殊的字符表示<info>學(xué)生的信息 < > &'"</info><message> <![CDATA[文本內(nèi)容<< >>]]]> </message>--></student><!--第二個學(xué)生信息--><student id ="2"><name>李四</name><age>24</age></student> </students>XML的解析和約束文檔引入
<1>schema約束文檔[本質(zhì)也是一個xml文檔]
<?xml version="1.0" encoding="UTF-8" ?> <!--Xml的約束文檔[schema]--> <!--被約束目標(biāo)--> <!--約束目標(biāo)--> <!--文件良好--> <schema xmlns="http://www.w3.org/2001/XMLSchema"targetNamespace="http://www.itheima.com"elementFormDefault="qualified"><!--定義students復(fù)雜元素--><element name ="students"><complexType><sequence><!--定義student復(fù)雜元素--><element name ="student" maxOccurs="unbounded"><complexType><sequence><!--定義name和age簡單元素--><element name ="name" type="string"></element><element name ="age" type="int"></element></sequence><!--引入簡單標(biāo)簽屬性required必須的 optional可選的--><attribute name ="id" type="string" use="required"></attribute></complexType></element></sequence></complexType></element> </schema><2>約束文檔引入[xml根標(biāo)簽屬性內(nèi)定義]
<students xmlns="http://www.itheima.com"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.itheima.com student.xsd"><3>XML文檔解析
利用dom4j解析文檔,需要導(dǎo)入相應(yīng)的jar包
domr4點(diǎn)擊下載
總結(jié)