在Java中有一种很方便的配置文件-->xml配置文件,使用非常快捷,易懂,很容易掌握,下面让我们一起来学习他吧!!!
怎样写xml文档呢???
一个XML文档如果符合一些基本的规范,那它就是结构规范的。XML格式有一套比HTML简单的解析规则,允许XML解析器不需要外部描述或了解数据含义就可以解析XML数据。
起始标签和结束标签必须匹配
XML元素可以包含正文和其他元素,在它的schema中用严格的规范给出了文档的类型。但是,元素必须严格嵌套:每个起始标签必须有对应的结束标签。
元素不能交迭
1.xml文档的基本写法:
<title>Evolution of Culture
<sub>in Animal</sub>
<author>by John T. Bonner</author>
</title><br>
XML标签对大小写是敏感的
下面是不同的元素。
<City> <CITY> <city>
表示空元素
XML对空元素有速记办法:一个标签以/>符号结尾就表示空元素。例如,下面两行是等效的:
<title/><br> <title></title>
保留字符
一些字符是XML句法结构的一部分。如果你想要在XML数据中引用它们,必须用特殊的字符来替代它们。下面列出这些字符。
< <
& &
> >
" "
' '
例如,"Melons cost < $1 at the A&P"要写成"Melons cost < $1 at the A&P."。
每个XML文档必须有唯一的根元素
例如,在天气报告中,元素<weather-report>表示这个XML文档唯一的根元素。
2.xml文档在Java中的四种解析方式:
1).DOM
2).XAL
3).JDOM
4).DOM4J
3.下面以DOM为例学习下xml在Java中的使用
首先学习下DOM在内存中的存储结构:
接着我们在学习下怎么把xml文档写入Java程序(以程序代码为例)
1.得到DOM解析器的工厂实例
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
2.从DOM工厂获得DOM解析器
DocumentBuilder db = dbf.newDocumentBuilder();
3.解析XML文档,得到一个Document,即DOM树
Document doc = db.parse("e:/book.xml");
4. 得到所有book节点列表信息
NodeList petList = doc.getElementsByTagName("book");
5.轮循书本信息
通过以上步骤即可完成把xml文档写到Java中,下面是完整的程序:
xml文档:
<?xml version="1.0" encoding="gbk"?> <bookstore> <book id="1"> <title tid="1">Harry Potter</title> <author>J K Rowling</author> <year>2005</year> <price>29.99</price> </book> <book id="2"> <title tid="2">Harry Potter</title> <author>J K Rowling</author> <year>2006</year> <price>39.99</price> </book> <book id="3"> <title tid="1">明朝那些事儿</title> <author>当年明月</author> <year>2009</year> <price>19.99</price> </book> </bookstore>
将xml读到Java的程序:
package xml; import java.io.FileNotFoundException; import java.io.IOException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; /** * 使用DOM解析xml文档。 */ public class TestDOMBook { public static void main(String[] args) { // 1、得到DOM解析器的工厂实例 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); try { // 2、从DOM工厂获得DOM解析器 DocumentBuilder db = dbf.newDocumentBuilder(); // 3、解析XML文档,得到一个Document,即DOM树 Document doc = db.parse("e:/book.xml"); // 4、得到所有book节点列表信息 NodeList petList = doc.getElementsByTagName("book"); // 5、轮循书本信息 System.out.println("XML文件中book的初始化信息:"); for (int i = 0; i < petList.getLength(); i++) { Element book = (Element) petList.item(i); String strId = book.getAttributeNode("id").getNodeValue(); System.out.println("ID:"+strId); String strTitle = book.getElementsByTagName("title").item(0).getFirstChild().getNodeValue(); Element title = (Element) book.getElementsByTagName("title").item(0); String strTid = title.getAttributeNode("tid").getNodeValue(); String strAuthor = book.getElementsByTagName("author").item(0).getFirstChild().getNodeValue(); String strYear = book.getElementsByTagName("year").item(0).getFirstChild().getNodeValue(); String strPrice = book.getElementsByTagName("price").item(0).getFirstChild().getNodeValue(); System.out.println("标题:"+strTitle); System.out.println("标题ID:"+strTid); System.out.println("作者:"+strAuthor); System.out.println("出版日期:"+strYear); System.out.println("价格:"+strPrice); } } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
程序运行的结果:
注意:在程序中需要多次用到异常处理,一定要处理好,不能抛给编译器。