一、使用Dom解析本地XML 
1、本地XML文件为:test.xml

<?xml version="1.0" encoding="UTF-8"?>
<Books>
<Book id="1">
<bookName>think in java</bookName>
<bookAuthor>张三</bookAuthor>
<bookISBN>家</bookISBN>
<bookPrice>75.00</bookPrice>
</Book>
<Book id="2">
<bookName>java核心基础</bookName>
<bookAuthor>王二</bookAuthor>
<bookISBN>家</bookISBN>
<bookPrice>65.00</bookPrice>
</Book>
<Book id="3">
<bookName>Oracle</bookName>
<bookAuthor>李四</bookAuthor>
<bookISBN>家</bookISBN>
<bookPrice>75.00</bookPrice>
</Book>
</Books>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

2、建立book类储存解析出来的内容

package com.yc.domain;

public class Book {
private int id;
private String bookName;
private String bookAuthor;
private String bookISBN;
private String bookPrice;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getBookAuthor() {
return bookAuthor;
}
public void setBookAuthor(String bookAuthor) {
this.bookAuthor = bookAuthor;
}
public String getBookISBN() {
return bookISBN;
}
public void setBookISBN(String bookISBN) {
this.bookISBN = bookISBN;
}
public String getBookPrice() {
return bookPrice;
}
public void setBookPrice(String bookPrice) {
this.bookPrice = bookPrice;
}
@Override
public String toString() {
return "Book [id=" + id + ", bookName=" + bookName + ", bookAuthor="
+ bookAuthor + ", bookISBN=" + bookISBN + ", bookPrice="
+ bookPrice + "]";
} }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

3、使用Dom解析:文件名为TestDom.Java

package com.yc.utils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List; 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.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException; import com.yc.domain.Book; public class TestDom {
public List<Book> getBook(File file){
List<Book> bookList=new ArrayList<Book>();
try {
//创建一个文档构建工厂
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
//通过工厂生产DocumentBuilder对象
DocumentBuilder builder=factory.newDocumentBuilder();
//将指定file的内容解析 返回一个Document的对象
Document doc=builder.parse(file);
Element element=doc.getDocumentElement();//获取根元素
//System.out.println(element);
NodeList nodeList=doc.getElementsByTagName("Book");
//System.out.println(nodeList.getLength());
int len=nodeList.getLength();
for (int i = 0; i < len; i++) {
Book book=new Book();
Node node=nodeList.item(i);
book.setId(Integer.parseInt(node.getAttributes().getNamedItem("id").getNodeValue()));
int len2=nodeList.item(i).getChildNodes().getLength();
for (int j = 0; j < len2; j++) {
Node node1=nodeList.item(i).getChildNodes().item(j);
if(node1.getNodeType()==1){
String content=node1.getFirstChild().getNodeValue();
String nodeName=node1.getNodeName();
switch (nodeName) {
case "bookName":
book.setBookName(content);
break;
case "bookAuthor":
book.setBookAuthor(content);
break;
case "bookISBN":
book.setBookISBN(content);
break;
case "bookPrice":
book.setBookPrice(content);
break; default:
break;
}
}
}
bookList.add(book);
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return bookList;
}
public static void main(String[] args) {
TestDom td=new TestDom();
File file=new File("test.xml");
List<Book> list=td.getBook(file);
for (int i = 0; i <list.size(); i++) {
Book book=list.get(i);
System.out.println(book.toString());
}
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85

4、解析结果:

Book [id=1, bookName=think in java, bookAuthor=张三, bookISBN=家, bookPrice=75.00]
Book [id=2, bookName=java核心基础, bookAuthor=王二, bookISBN=家, bookPrice=65.00]
Book [id=3, bookName=Oracle, bookAuthor=李四, bookISBN=家, bookPrice=75.00]
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

二、使用Dom4j解析本地XML文件 
其他同上只是换用不同的解析方法: 
本次使用Dom4j解析本地文件test.xml

package com.yc.utils;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader; import com.yc.domain.Book; public class TestDom4j {
public static void main(String[] args) {
TestDom4j td=new TestDom4j();
File file=new File("test.xml");
List<Book> list=td.findAll(file);
for (int i = 0; i <list.size(); i++) {
Book book=list.get(i);
System.out.println(book.toString());
}
}
public List<Book> findAll(File file){
List<Book> bookList=new ArrayList<Book>();
SAXReader reader=new SAXReader();
Document doc=null;
try {
doc=reader.read(file);
} catch (DocumentException e) {
e.printStackTrace();
}
Element root=doc.getRootElement();//取出根节点
//System.out.println(root);
//迭代出所有子节点
Iterator its=root.elementIterator();
Book book=null;
while(its.hasNext()){
Element et=(Element) its.next();//取出所有book节点
if("Book".equals(et.getName())){
book=new Book();
//迭代属性
for(Iterator attrIts=et.attributeIterator();attrIts.hasNext();){
Attribute attr=(Attribute) attrIts.next();
if("id".equals(attr.getName())){
book.setId(Integer.parseInt(attr.getValue()));
}
}
//迭代Book地下元素
for(Iterator it=et.elementIterator();it.hasNext();){
Element el=(Element) it.next();
switch (el.getName()) {
case "bookName":
book.setBookName(el.getText());
break;
case "bookAuthor":
book.setBookAuthor(el.getText());
break;
case "bookISBN":
book.setBookISBN(el.getText());
break;
case "bookPrice":
book.setBookPrice(el.getText());
break;
default:
break;
} }
}
bookList.add(book);
}
return bookList;
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77

解析结果同上 
三、使用Sax解析本地文件 
其它同上只是换种解析方法 
1.配置Sax文件显示解析过程

package com.yc.utils;

import java.util.ArrayList;
import java.util.List; import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler; import com.yc.domain.Book; public class SaxXML extends DefaultHandler {
private List<Book> bookList;
private Book book;
private String tagName;//存放每一次存放的标签
//当我解析器解析 触发方法
@Override
public void startDocument() throws SAXException {
bookList=new ArrayList<Book>();
System.out.println("开始读文档了");
}
//当解析到元素节点时 触发这个方法
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
System.out.println("开始读元素了");
if("Book".equals(qName)){
book=new Book();
book.setId(Integer.parseInt(attributes.getValue("id")));
}
tagName=qName;
}
//当每次解析文本节点就调用这个
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
System.out.println("文本解析中");
if(book!=null){
String content=new String(ch,start,length);
if("bookName".equals(tagName)){
book.setBookName(content);
}else if("bookAuthor".equals(tagName)){
book.setBookAuthor(content);
}else if("bookISBN".equals(tagName)){
book.setBookISBN(content);
}else if("bookPrice".equals(tagName)){
book.setBookPrice(content);
}
}
}
@Override
public void endDocument() throws SAXException {
System.out.println("文档结束了");
}
//元素结束
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
System.out.println("元素结束了");
if("Book".equals(qName)){
bookList.add(book);
book=null;
}
tagName=""; }
public List<Book> getBookList() {
return bookList;
} }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72

2、使用Sax解析本地文件test.xml

package com.yc.utils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory; import org.xml.sax.SAXException; import com.yc.domain.Book; public class TestSax {
public List<Book> findAll(File file){
List<Book> bookList=new ArrayList<Book>();
//获取SAX解析工厂
SAXParserFactory spf= SAXParserFactory.newInstance();
try {
SAXParser parser=spf.newSAXParser();
SaxXML sx=new SaxXML();
parser.parse(file, sx);
bookList=sx.getBookList();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return bookList;
}
public static void main(String[] args) {
TestSax ts=new TestSax();
File file=new File("test.xml");
List<Book> list=ts.findAll(file);
for (Book book:list) {
System.out.println(book.toString());
}
} }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

3.解析结果:

开始读文档了
开始读元素了
文本解析中
开始读元素了
文本解析中
开始读元素了
文本解析中
元素结束了
文本解析中
开始读元素了
文本解析中
元素结束了
文本解析中
开始读元素了
文本解析中
元素结束了
文本解析中
开始读元素了
文本解析中
元素结束了
文本解析中
元素结束了
文本解析中
开始读元素了
文本解析中
开始读元素了
文本解析中
元素结束了
文本解析中
开始读元素了
文本解析中
元素结束了
文本解析中
开始读元素了
文本解析中
元素结束了
文本解析中
开始读元素了
文本解析中
元素结束了
文本解析中
元素结束了
文本解析中
开始读元素了
文本解析中
开始读元素了
文本解析中
元素结束了
文本解析中
开始读元素了
文本解析中
元素结束了
文本解析中
开始读元素了
文本解析中
元素结束了
文本解析中
开始读元素了
文本解析中
元素结束了
文本解析中
元素结束了
文本解析中
元素结束了
文档结束了
Book [id=1, bookName=think in java, bookAuthor=张三, bookISBN=家, bookPrice=75.00]
Book [id=2, bookName=java核心基础, bookAuthor=王二, bookISBN=家, bookPrice=65.00]
Book [id=3, bookName=Oracle, bookAuthor=李四, bookISBN=家, bookPrice=75.00]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69

四、解析线上XMl文件我們这里以解析线上新闻为例 
注解:1.线上地址为:http://api.avatardata.cn/GuoNeiNews/Query?key=b5884e12578141e888cf17fe62903bd2&page=1&rows=10&dtype=xml 
2.xml文件为nwes.xml

<?xml version="1.0"?>
<NewsResult>
<error_code>0</error_code>
<reason>Succes</reason>
<result>
<NewsObj>
<ctime>2016-08-28 00:21</ctime>
<title>俄罗斯莫斯科仓库大火致16人死亡4人受伤</title>
<description>网易国内</description>
<picUrl>http://s.cimg.163.com/cnews/2016/8/28/2016082800201872c58_550.jpg.119x83.jpg</picUrl>
<url>http://news.163.com/16/0828/00/BVH193060001121M.html#f=dlist</url>
</NewsObj>
<NewsObj>
<ctime>2016-08-27 19:41</ctime>
<title>江浙两省新任副省长均出身企业 未在政府工作过</title>
<description>网易国内</description>
<picUrl>http://s.cimg.163.com/catchpic/8/8B/8B6D4CBC561C0DC041A842C539584115.jpg.119x83.jpg</picUrl>
<url>http://news.163.com/16/0827/19/BVGH7F1S0001124J.html#f=dlist</url>
</NewsObj>
<NewsObj>
<ctime>2016-08-27 17:30</ctime>
<title>国务院这几年聘任的“智囊”都有谁?</title>
<description>网易国内</description>
<picUrl>http://s.cimg.163.com/photo/0001/2016-08-26/t_BVCOE4IO6VVV0001.jpg.119x83.jpg</picUrl>
<url>http://news.163.com/16/0827/17/BVG9OSRD00014SEH.html#f=dlist</url>
</NewsObj>
<NewsObj>
<ctime>2016-08-27 17:43</ctime>
<title>山东警方深化打击治理网络电信诈骗违法犯罪 电信诈骗</title>
<description>网易国内</description>
<picUrl>http://s.cimg.163.com/catchpic/1/1E/1E766CCB83F19FCD738C223989B52C14.jpg.119x83.jpg</picUrl>
<url>http://news.163.com/16/0827/17/BVGAG3OF00014SEH.html#f=dlist</url>
</NewsObj>
<NewsObj>
<ctime>2016-08-27 18:48</ctime>
<title>广西东兴市发生一起中毒事故 已造成3人死亡</title>
<description>网易国内</description>
<picUrl>http://s.cimg.163.com/catchpic/9/91/916E9B868A41E063AB768112E4016DE1.jpg.119x83.jpg</picUrl>
<url>http://news.163.com/16/0827/18/BVGE6V82000146BE.html#f=dlist</url>
</NewsObj>
<NewsObj>
<ctime>2016-08-27 14:38</ctime>
<title>内地奥运精英代表团开启3日访港之旅,将与当地民众互</title>
<description>网易国内</description>
<picUrl>http://s.cimg.163.com/catchpic/7/76/7673DDB4A375F3CE365A27E05D3551C8.jpg.119x83.jpg</picUrl>
<url>http://news.163.com/16/0827/14/BVFVTQSD00014SEH.html#f=dlist</url>
</NewsObj>
<NewsObj>
<ctime>2016-08-27 14:46</ctime>
<title>丽江女官员被开除公职 多次与人发生不正当性关系</title>
<description>网易国内</description>
<picUrl>http://s.cimg.163.com/cnews/2016/8/27/20160827144553a4f90.jpg.119x83.jpg</picUrl>
<url>http://news.163.com/16/0827/14/BVG0BKN70001124J.html#f=dlist</url>
</NewsObj>
<NewsObj>
<ctime>2016-08-27 15:28</ctime>
<title>甘肃张掖航空大会遇难飞行员为南非籍(图)</title>
<description>网易国内</description>
<picUrl>http://s.cimg.163.com/cnews/2016/8/27/20160827112149ecd50.gif.119x83.jpg</picUrl>
<url>http://news.163.com/16/0827/15/BVG2OPMO00014JB6.html#f=dlist</url>
</NewsObj>
<NewsObj>
<ctime>2016-08-27 15:37</ctime>
<title>哈尔滨刘亚楼旧居等被毁不可移动文物拟原址重建</title>
<description>网易国内</description>
<picUrl>http://s.cimg.163.com/photo/0001/2016-08-26/t_BVCOE4IO6VVV0001.jpg.119x83.jpg</picUrl>
<url>http://news.163.com/16/0827/15/BVG39ELT0001124J.html#f=dlist</url>
</NewsObj>
<NewsObj>
<ctime>2016-08-27 15:56</ctime>
<title>河北唐山市古冶区发生3.1级地震 震源深度10千米</title>
<description>网易国内</description>
<picUrl>http://s.cimg.163.com/catchpic/1/1E/1E766CCB83F19FCD738C223989B52C14.jpg.119x83.jpg</picUrl>
<url>http://news.163.com/16/0827/15/BVG4C7SD0001124J.html#f=dlist</url>
</NewsObj>
</result>
</NewsResult>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77

3.建立News类储存解析内容

package com.yc.domain;

public class News {
private String ctime;
private String title;
private String description;
private String picUrl;
private String url;
public String getCtime() {
return ctime;
}
public void setCtime(String ctime) {
this.ctime = ctime;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getPicUrl() {
return picUrl;
}
public void setPicUrl(String picUrl) {
this.picUrl = picUrl;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
@Override
public String toString() {
return "News [ctime=" + ctime + ", title=" + title + ", description="
+ description + ", picUrl=" + picUrl + ", url=" + url + "]";
} }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

4.解析线上新闻XML文件

package com.yc.utils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List; 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.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException; import com.yc.domain.Book;
import com.yc.domain.News; public class TestNews { public static void main(String[] args) {
List<News> newsList=new ArrayList<News>(); try {
File file=new File("news.xml");
if(!file.exists()){
URL url=new URL("http://api.avatardata.cn/GuoNeiNews/Query?key=b5884e12578141e888cf17fe62903bd2&page=1&rows=10&dtype=xml");
URLConnection con=url.openConnection();
con.connect();
InputStream is=con.getInputStream();
OutputStream os=new FileOutputStream(file,true);
byte[] bt=new byte[1024];
int len=-1;
while((len=is.read(bt))!=-1){
os.write(bt,0,len);
}
os.flush();
os.close();
is.close();
}
/*URL url=new URL("http://api.avatardata.cn/GuoNeiNews/Query?key=b5884e12578141e888cf17fe62903bd2&page=1&rows=10&dtype=xml");
URLConnection con=url.openConnection();
con.connect();
InputStream is=con.getInputStream();*/
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
Document doc=builder.parse(file);
Element element=doc.getDocumentElement();
//System.out.println(element);
NodeList n1=doc.getElementsByTagName("NewsObj");
//System.out.println(n1.getLength());
int len=n1.getLength();
for (int i = 0; i < len; i++) {
News news=new News();
Node node=n1.item(i);
int len2=n1.item(i).getChildNodes().getLength();
for (int j = 0; j < len2; j++) {
Node node1=n1.item(i).getChildNodes().item(j);
if(node1.getNodeType()==1){
String content=node1.getFirstChild().getNodeValue();
String nodeName=node1.getNodeName();
switch (nodeName) {
case "ctime":
news.setCtime(content);
break;
case "title":
news.setTitle(content);
break;
case "description":
news.setDescription(content);
break;
case "picUrl":
news.setPicUrl(content);
break;
case "url":
news.setUrl(content);
break; default:
break;
}
}
}
newsList.add(news);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
for (int i = 0; i <newsList.size(); i++) {
News news=newsList.get(i);
System.out.println(news.toString());
} } }

如何解析本地和线上XML文件获取相应的内容的更多相关文章

  1. scp 从本地往线上传文件

    scp /home/wwwroot/default/tf_ment.sql root@IP:/home/wwwroot/default/

  2. vue本地和线上环境(域名)配置

    vue本身为运行脚手架项目自家搭载了一个nodejs后台环境,本地可通过proxyTable来处理跨域问题,但是上线(或生产环境)之后改域名真是一件麻烦的事情,所以进行一些配置. config/ind ...

  3. js上传文件获取文件流

    上传文件获取文件流 <div> 上传文件 : <input type="file" name = "file" id = "file ...

  4. Filezilla Xshell SecureFX Win10等无法拖放文件(本地或线上)解决办法

    一.win10系统Filezilla Xshell SecureFX等无法拖放文件到线上服务器解决办法: 1.按窗口键+R,打开“运行”对话框:输入regedit回车 2.在注册表编辑器地址栏输入以下 ...

  5. 在java项目中怎样利用Dom4j解析XML文件获取数据

    在曾经的学习.net时常常会遇到利用配置文件来解决项目中一些须要常常变换的数据.比方数据库的连接字符串儿等.这个时候在读取配置文件的时候.我们一般会用到一个雷configuration,通过这个类来进 ...

  6. 浅谈JS中的!=、== 、!==、===的用法和区别 JS中Null与Undefined的区别 读取XML文件 获取路径的方式 C#中Cookie,Session,Application的用法与区别? c#反射 抽象工厂

    浅谈JS中的!=.== .!==.===的用法和区别   var num = 1;     var str = '1';     var test = 1;     test == num  //tr ...

  7. c# 模拟表单提交,post form 上传文件、大数据内容

    表单提交协议规定:要先将 HTTP 要求的 Content-Type 设为 multipart/form-data,而且要设定一个 boundary 参数,这个参数是由应用程序自行产生,它会用来识别每 ...

  8. 通过form表单上传文件获取后台传来的数据

    小伙伴是不是遇到过这样的问题,通过submit提交form表单的时候,不知怎么获取后台传来的返回值.有的小伙伴就会说你不会发送ajax,其实也会.假如提交的form表单中含有文件,怎么办? 步骤1:想 ...

  9. Spring Boot 上传文件 获取项目根路径 物理地址 resttemplate上传文件

    springboot部署之后无法获取项目目录的问题: 之前看到网上有提问在开发一个springboot的项目时,在项目部署的时候遇到一个问题:就是我将项目导出为jar包,然后用java -jar 运行 ...

随机推荐

  1. Zabbix之Python发送邮件

    前言:继前篇zabbix邮件乱码问题解决,转码后,问题是解决了,但是又发现问题,有时候告警邮件没有邮件内容,只有标题,一直没找出原因,所以就换了Python脚本发送邮件,代码如下. 使用前当然是要先安 ...

  2. 三者互ping,PC,虚拟机,uboot,nfs网络文件系统搭建

    要想实现三者互ping,韦老师虽然专门出了视频说明,但是在自己配置过程还是出现了问题,这里记录一下解决办法,虽然我也不知道原因,但是解决了出现的问题也实现了三者互ping. 首先,我的硬件设备是PC通 ...

  3. linux下的c/c++调试器gdb

    Reference:  http://www.cnblogs.com/xd502djj/archive/2012/08/30/2663960.html linux下的c/c++调试器gdb gdbLi ...

  4. [转]Mustache 使用心得总结

    原文地址:https://blog.csdn.net/xuemoyao/article/details/17896203 前言: 之前的一个项目里面就有用到这个前台的渲染模版,当时挺忙的也没时间抽空总 ...

  5. [转]ORA-01555错误总结(一)

    原文地址:http://blog.csdn.net/sh231708/article/details/52935695 这篇文章算是undo相关问题总结的补充,因为ORA-01555错误与undo有着 ...

  6. android 中使用jwt token(json web token)--java

    http://blog.csdn.net/mingzhnglei/article/details/51119836 下面贴上自己项目中的一个小小的example import com.nimbusds ...

  7. Redis (1) —— 安装

    Redis (1) -- 安装 摘要 介绍Mac OS X安装Redis基本方法 版本 Redis版本: 2.8.24 内容 下载Redis包 地址:http://download.redis.io/ ...

  8. MacBook如何用Parallels Desktop安装windows7/8

    虽然MacBook真的很好用,不过在天朝的国情下,有很多软件还是仅支持IE和windows系统下才有.所以有必要为自己的MacBook装一个windows版本的系统,之前试过用Boot Camp来建立 ...

  9. 局域网内的机器不能ping通虚拟机,但是虚拟机可以ping通局域网的机器

    将虚拟机的网络网络适配器模式改成桥接模式即可: 一.选择虚拟机里面的设置: 二.在虚拟机设置界面将网络适配器模式改成桥接模式

  10. jQuery(五):文本操作

    text()可以获取或设置元素的文本内容.例如: 示例: <!DOCTYPE html> <html lang="en"> <head> < ...