以下文章是从老罗教学中的总结,感谢老罗
需要解析的XML格式为:
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book id="12">
<name>疯狂Java讲义</name>
<price>99元</price>
</book>
<book id="15">
<name>Android网络开发技术实战经典</name>
<price>69元</price>
</book>
</books>
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
首先创建实体类
package com.dom.xml.domain;
public class Book {
private int id;
private String name;
private float price;
public Book() {
}
//省略getter,setter方法
@Override
public String toString() {
return "Book [id=" + id + ", name=" + name + ", price=" + price + "]";
}
}
创建返回输入流的工具类
package com.dom.xml.http;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpUtils {
public HttpUtils() {
// TODO Auto-generated constructor stub
}
public static InputStream getXML(String path) {
InputStream inputStream = null;
try {
URL url = new URL(path);
if (url != null) {
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setConnectTimeout(3000);
connection.setDoInput(true);
connection.setRequestMethod("GET");
int code = connection.getResponseCode();
if (code == 200) {
inputStream = connection.getInputStream();
}
}
} catch (Exception e) {
// TODO: handle exception
}
return inputStream;
}
}
创建service,并包含测试方法
package com.dom.xml.service;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.soap.Node;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import com.dom.xml.domain.Book;
import com.dom.xml.http.HttpUtils;
public class DomService {
public DomService() {
// TODO Auto-generated constructor stub
}
public List<Book> getBooks(InputStream inputStream) throws Exception {
List<Book> list = new ArrayList<Book>();
// 创建一个document解析的工厂
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(inputStream);
Element element = document.getDocumentElement();// 获得稳定的元素节点
NodeList bookNodes = element.getElementsByTagName("book");
for (int i = 0; i < bookNodes.getLength(); i++) {
Element bookeElement = (Element) bookNodes.item(i);
Book book = new Book();
book.setId(Integer.parseInt(bookeElement.getAttribute("id")));
NodeList childNodes = bookeElement.getChildNodes();
for (int j = 0; j < childNodes.getLength(); j++) {
if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE) {
if ("name".equals(childNodes.item(j).getNodeName())) {
book.setName(childNodes.item(j).getFirstChild()
.getNodeValue());
} else if ("price".equals(childNodes.item(j).getNodeName())) {
book.setPrice(Float.parseFloat(childNodes.item(j)
.getFirstChild().getNodeValue()));
}
}
}
list.add(book);
}
return list;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String path = "http://192.168.0.102:8080/myhttp/book.xml";
InputStream inputStream = HttpUtils.getXML(path);
DomService service = new DomService();
try {
List<Book> list = service.getBooks(inputStream);
for (Book book : list) {
System.out.println(book.toString());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}