java读取XML⽂件的四种⽅法总结(必看篇)
JAVA操作XML⽂档主要有四种⽅式,分别是DOM、SAX、JDOM和DOM4J,DOM和SAX是官⽅提供的,⽽JDOM和DOM4J 则是引⽤第三⽅库的,其中⽤的最多的是DOM4J⽅式。运⾏效率和内存使⽤⽅⾯最优的是SAX,但是由于SAX是基于事件的⽅式,所以SAX⽆法在编写XML的过程中对已编写内容进⾏修改,但对于不⽤进⾏频繁修改的需求,还是应该选择使⽤SAX。
下⾯基于这四种⽅式来读取XML⽂件。
第⼀,以DOM的⽅式实现。
package xmls;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
l.sax.SAXException;
l.parsers.DocumentBuilder;
l.parsers.DocumentBuilderFactory;
l.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;
/**
* Created by lenovo on 2017-6-3.
*/
public class DOMReadDemo {
public static void main(String[] args){
DocumentBuilderFactory dbf = wInstance();
try{
DocumentBuilder db = wDocumentBuilder();
Document document = db.parse("src/l");
NodeList booklist = ElementsByTagName("book");
for(int i = 0; i < Length(); i++){
System.out.println("--------第" + (i+1) + "本书----------");
Element ele = (Element) booklist.item(i);
NodeList childNodes= ChildNodes();
for(int j = 0; j < Length(); j++){
Node n = childNodes.item(j);
NodeName() != "#text"){
System.out.NodeName() + ":" + n.getTextContent());
}
}
System.out.println("---------------------------------");
}
}catch (ParserConfigurationException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}catch (SAXException e){
e.printStackTrace();
}
}
}
第⼆,以SAX的⽅式实现。
package xmls;
l.parsers.SAXParser;
l.parsers.SAXParserFactory;
/**
* Created by lenovo on 2017-6-1.
*/
public class xmlTest2 {
public static void main(String[] args){
SAXParserFactory spf = wInstance();
try{
SAXParser sp = wSAXParser();
SAXParserHandler handler = new SAXParserHandler();
sp.parse("src\ mls\\l", handler);
}catch (Exception e){
e.printStackTrace();
}
}
}
package xmls;
l.sax.Attributes;
l.sax.SAXException;
l.sax.helpers.DefaultHandler;
/**
* Created by lenovo on 2017-6-1.
*/
public class SAXParserHandler extends DefaultHandler {
@Override
public void startDocument() throws SAXException {
super.startDocument();
System.out.println("SAX解析开始");
}
@Override
public void endDocument() throws SAXException {
System.out.println("SAX解析结束");
}
@Override
public void startElement(String s, String s1, String s2, Attributes attributes) throws SAXException {    super.startElement(s, s1, s2, attributes);
System.out.println(s2);
for(int i = 0; i < Length(); i++){
String name = QName(i);
String value = Value(name);
System.out.println("属性值:" + name + "=" + value);
}
}
@Override
public void endElement(String s, String s1, String s2) throws SAXException {
if(s2.equals("book")){
System.out.println("-----------------------");
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
super.characters(ch, start, length);
String value = new String(ch, start, length);
im().equals("")){
return;
}
System.out.println(value);
}
}
第三,以JDOM的⽅式实现。
package xmls;
import org.jdom2.Attribute;
import org.jdom2.Document;
spring怎么读取xml文件
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.JDOMParseException;
import org.jdom2.input.SAXBuilder;
import java.io.*;
import java.util.List;
/
**
* Created by lenovo on 2017-6-2.
*/
public class JDOMTest {
public static void main(String[] args){
SAXBuilder saxBuilder = new SAXBuilder();
InputStream in;
try{
in = new FileInputStream(new File("src\ mls\\l"));
Document document = saxBuilder.build(in);
Element rootElement = RootElement();
List<Element> bookList = Children();
for(Element book: bookList){
System.out.println("第" + (bookList.indexOf(book)+1) + "本书!");
List<Attribute> attrs = Attributes();
for(Attribute attr: attrs){
System.out.Name() + "=" + Value());
}
for(Element item: Children()){
System.out.Name() + ":" + Value());
}
System.out.println("------------------------------------");
}
}catch (FileNotFoundException e){
e.printStackTrace();
}catch (JDOMException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
}
}
第四,以DOM4J的⽅式实现。
package xmls;
import org.dom4j.*;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
/**
* Created by lenovo on 2017-6-2.
*/
public class DOM4JTest {
public void parseXML(){
SAXReader saxReader = new SAXReader();
try{
Document document = ad(new File("src\ mls\\l"));
Element rootElement = RootElement();
Iterator it = rootElement.elementIterator();
while (it.hasNext()){
Element book = (();
List<Attribute> attrs = book.attributes();
for(Attribute attr: attrs){
System.out.println("属性名:" + Name() + "---- 属性值:" + Value() );
}
Iterator cit = book.elementIterator();
while (cit.hasNext()){
Element child = (Element) ();
System.out.println("⼦节点:" + Name());
}
}
}catch (DocumentException e){
e.printStackTrace();
}
}
public static void main(String[] args){
DOM4JTest dom4JTest = new DOM4JTest();
dom4JTest.parseXML();
}
}
以上这篇java读取XML⽂件的四种⽅法总结(必看篇)就是⼩编分享给⼤家的全部内容了,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。