java读取xml文件(二)

2014-11-24 01:19:41 · 作者: · 浏览: 1
d ****");

92 }

93 /**

94 * 在解析名字空间开始时调用

95 * @param prefix

96 * @param uri

97 * @throws SAXException

98 */

99 publicvoid startPrefixMapping(String prefix

100 , String uri) throws SAXException{

101 }

102 /**

103 * 在解析名字空间结束时调用

104 * @param prefix

105 * @throws SAXException

106 */

107 publicvoid endPrefixMapping(String prefix) throws SAXException{

108 }

109 /**

110 * 在解析元素开始时调用

111 * @param namespaceURI

112 * @param localName

113 * @param qName

114 * @param atts

115 * @throws SAXException

116 */

117 publicvoid startElement(String namespaceURI, String localName

118 , String qName, Attributes atts) throws SAXException{

119 }

120 /** 在解析元素结束时调用

121 * @param namespaceURI

122 * @param localName 本地名,如student

123 * @param qName 原始名,如LIT:student

124 * @throws SAXException */

125 publicvoid endElement(String namespaceURI, String localName,String qName) throws SAXException{

126 if (localName.equals(“student”)){

127 System.out.println(localName+":"+currentData);

128 }

129 }

取得元素数据的方法――characters

取得元素数据中的空白的方法――ignorableWhitespace
在解析到处理指令时调用的方法――processingInstruction
当未验证解析器忽略实体时调用的方法――skippedEntity
运行时,只需要使用下列代码:

Java代码

130 MySAXParser mySAXParser = new MySAXParser();

131 mySAXParser.parserXMLFile("SutInfo.xml");

3.JDOM

JDOM的处理方式有些类似于DOM,但它主要是用SAX实现的 。JDOM用Java的数据类型来定义操作数据树的各个节点 。JDOM的性能也很优越。

Java代码

132 import org.jdom.*;

133 import org.jdom.input.*;

134 import org.jdom.output.*;

135 SAXBuilder builder = new SAXBuilder(false);

136 //得到Document

137 Document doc = builder.build(fileURI);

138 //名字空间

139 Namespace ns = Namespace.getNamespace("LIT" , "http://www.lit.edu.cn/student/ ");

140 //取得所有LIT:student节点的集合

141 List lstStudents = elmtStuInfo.getChildren("student",ns);

142 for ( … ){

143 Element elmtStudent = (Element)lstStudents.get(i);

144 elmtStudent.getChildTextTrim("name", ns);

145 }

146 //修改

147 elmtLesson.getChild("lessonScore" , ns).setText("100");

148 //删除

149 elmtStuInfo.removeChild("master", ns);

150 //添加

151 elmtStuInfo.addContent(new Element("master" , ns).addContent(new Entity("masterName")));

152 //输出文档

153 //第一个参数是缩进字符串,这里是4个空格。

154 //第二个参数是true,表示需要换行。

155 XMLOutputter printDoc = new XMLOutputter(" ", true);

156 printDoc.output(doc, new FileOutputStream("StuInfo.xml"));

4.JAXB (Java And XML Binding)

JAXB 是以SUN为主的一些公司公布的。JAXB将schema(或者DTD)映射为java对象(.java文件),然后使用这些java对象来解析xml文件。需要使用之前生成java文件,因而要有固定的schema,无法处理动态的xml文件。

首先使用xjc命令,生成java文件
xjc [-options ...]

(生成的文件较多)

Java代码

157 JAXBContext jc = JAXBContext.newInstance(“packageName");

158 Unmarshaller unmarshaller = jc.createUnmarshaller();

159 Collection collection= (Collection)unmarshaller.unmarshal(new File( "books.xml"));

160 CollectionType.BooksType booksType =collection.getBooks();

161 List bookList = booksType.getBook();

162 for( … ){

163 test.jaxb.BookType book =(test.jaxb.BookType) bookList.get(i);

164 System.out.println("Book Name: " + book.getName().trim());

165 System.out.println("Book ISBN: " + book.getISBN());

166 }

5.DOM4J

据悉dom4j在xml解析方面是性能最好的,hibernate等框架都使用它作为解析的工具。

要使用dom4j读写XML文档,需要先下载dom4j包,dom4j官方网站在 http://www.dom4j.org/

目前最新dom4j包下载地址:http://nchc.dl.sourceforge.net/sourceforge/dom4j/dom4j-1.6.1.zip

解开后有两个包,仅操作XML文档的话把dom4j-1.6.1.jar加入工程就可以了,如果需要使用XPath的话还需要加入包jaxen-1.1-beta-7.jar