首先在tomcat服务器下建立student.xml文件,注意文件存放位置
student.xml内容为:
<?xml version="1.0" encoding="UTF-8"?>
<students>
<student>
<id>0001</id>
<name>小明</name>
<class>一班</class>
</student>
<student>
<id>0002</id>
<name>小红</name>
<class>二班</class>
</student>
<student>
<id>0003</id>
<name>小强</name>
<class>三班</class>
</student>
</students>
`
编写ParseXml.java类去解析它,并保存
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.HttpURLConnection;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
/**
*
* @author 小宋
*/
public class PraseXml {
public static void main(String[] args) {
//解析網絡中的xml文件 我是訪問本地的服務器
String afterPraseFromXml = praseXml("http://localhost:8080/firsrt/student.xml");
//把解析后的結果保存在文件中
boolean flag = saveXml(afterPraseFromXml, "d://a.txt");
System.out.println("保存成功" + flag);
}
/**
* 解析xml
*
* @param path
* @return
*/
public static String praseXml(String path) {
StringBuffer sb = null;
InputStream ism = null;
HttpURLConnection connection = null;
URL url = null;
try {
url = new URL(path);
connection = (HttpURLConnection) url.openConnection();//獲取連接
ism = connection.getInputStream();
// BufferedReader reader=new BufferedReader(new InputStreamReader(ism));
// StringBuffer sb=new StringBuffer();
// String line=null;
// while((line=reader.readLine())!=null){
// sb.append(line);
// }
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document d = db.parse(ism);
Element element = d.getDocumentElement();
//獲取students下的子節點,即 student
NodeList list = element.getChildNodes();
sb = new StringBuffer();
for (int i = 0; i < list.getLength(); i++) {
NodeList nodes = list.item(i).getChildNodes();//獲取student節點下的子節點
for (int j = 0; j < nodes.getLength(); j++) {
if (nodes.item(j).getTextContent() != "") {
sb.append(nodes.item(j).getTextContent());//把解析后的內容保存在sb對象中
}
}
}
// Element element= d.getDocumentElement();
// Node node= element.getAttributeNode("name");
// System.out.println(node.getNodeName());
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
if (ism != null) {
ism.close();
ism = null;
}
if (connection != null) {
connection.connect();
connection = null;
}
} catch (IOException ex) {
Logger.getLogger(PraseXml.class.getName()).log(Level.SEVERE, null, ex);
}
}
return sb.toString().trim();
}
/**
* 保存到文件中
*
* @param contentString
* @param filePath
* @return
*/
public static boolean saveXml(String contentString, String filePath) {
File file = new File(filePath);
BufferedWriter out = null;
try {
if (file.exists()) {
file.delete();
}
file.createNewFile();
out = new BufferedWriter(new FileWriter(file));
out.write(contentString, 0, contentString.length() - 1);
return true;
} catch (Exception ex) {
Logger.getLogger(PraseXml.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (out != null) {
try {
out.close();
out = null;
} catch (IOException ex) {
Logger.getLogger(PraseXml.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
return false;
}
}
打开tomcat服务器,就可以编译运行了。
总结;快期末了,老师叫我每个人出些题,我是出两个题。而我想到的是一题是访问网络文件,然后解析,保存。运用到的知识点 HttpUrlConnection这个类,返回的数据我去解析它,然后写到文件中。也是一些基本的io操作。。。 对http协议在我的理解就是我们向服务器请求,然后服务器响应数据给你,比如我们在浏览器打开http://www.baidu.com 其实我们是请求了百度的服务器,然后百度服务器返回的是HTML,我们浏览器解析它,才显示界面。
时间: 2024-09-30 22:55:41