Java远程解析xml

(转:http://blog.163.com/liucy_18/blog/static/531929062010627105145250/)

package com.erry.util;

import java.net.*;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

public class TestUrl {
// 获取远程地址图片并保存到本地
public boolean saveUrlAs(String photoUrl, String fileName) {
try {
URL url = new URL(photoUrl);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
DataInputStream in = new DataInputStream(connection
.getInputStream());
DataOutputStream out = new DataOutputStream(new FileOutputStream(
fileName));
byte[] buffer = new byte[4096];
int count = 0;
while ((count = in.read(buffer)) > 0) {
out.write(buffer, 0, count);
}
out.close();
in.close();
// connection.disconnect();
return true;

} catch (Exception e) {
System.out.println(e);
return false;
}
}

// 读取远程xml文件,并解析获取所需的值数据。
public void savexmlUrl(String xmlUrl, String fileName) {
try {
URL url = new URL(xmlUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.connect();
InputStream stream = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(
stream, "UTF-8"));
StringBuffer document = new StringBuffer();
String line = null;
while ((line = reader.readLine()) != null) {
document.append(line);
}
System.out.println(document);// 输出XML信息(假如是如下信息)
// <?xml version="1.0" encoding="utf-8"?><configs><page size="104" isFree="false" freePage="15" catalogPage="5"/></configs>

/*dom4j 感觉这个jar的解析方式很笨。也没怎么研究,以下方式算是能读到xml里的属性值。*/
String text = document.toString();
System.out.println(text);
Document doc = DocumentHelper.parseText(text);
Element root = (Element) doc.getRootElement();
System.out.println(root.getName());
// 枚举名称为foo的节点
System.out.println(root.elementText("page"));
for (Iterator i = root.elementIterator("page"); i.hasNext();) {
Element page = (Element) i.next();
// 枚举属性
for (Iterator j = page.attributeIterator(); j.hasNext();) {
Attribute att = (Attribute) j.next();
System.out.println(att.getText());
}
}

/*jdom 这个解析xml数据个人感觉很简便,直接就可以得到一个String类型的值,比较容易满足需求。*/
SAXBuilder sax = new SAXBuilder();
Document doc = (Document) sax.build(new StringReader(document.toString()));
Element root = doc.getRootElement();
String size = root.getChild("page").getAttributeValue("size");
String catalogPage = root.getChild("page").getAttributeValue("catalogPage");
System.out.println("num: "+ size +" : "+catalogPage);

conn.disconnect();
} catch (Exception e) {
System.out.println(e);
}
}

/**
* 兼容HTTP和FTP协议
*
* @param urlString
* @return
*/
public String getDocumentAt(String urlString) {
StringBuffer document = new StringBuffer();

try {

URL url = new URL(urlString);
URLConnection conn = url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
document.append(line + "\n");
}
reader.close();
} catch (MalformedURLException e) {
System.out.println("Unable to connect to URL: " + urlString);
} catch (IOException e) {
System.out.println("IOException when connecting to URL: "
+ urlString);
}
return document.toString();
}

/**
*
* @param args
*/
public static void main(String[] args) {
TestUrl test = new TestUrl();
String photoUrl = "http://www.xxx.com/images/name.jpg";
String fileName = photoUrl.substring(photoUrl.lastIndexOf("/"));
String filePath = "d:\\";
boolean flag = test.saveUrlAs(photoUrl, filePath + fileName);//获取远程地址图片并保存到本地
System.out.println("Run ok!\n Get URL file " + flag);
String xmlUrl = "http://www.xxx.com/name.xml";
test.savexmlUrl(xmlUrl, filePath + fileName); //读取远程xml文件
System.out.println("~~~~~~~~end");
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值