通过http远程访问解析xml

通过HttpURLConnection类实现http获取xml参考https://mp.csdn.net/postedit/85246429

 引入jdom来进行xml解析。

xml内容:



	
		elaa3420
		2
		
			1.0
		
        
			1.2
		

	

实例代码:


package org.eclipse.leshan.standalone.utils;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONObject;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

public class parsexml {

    public static JSONObject savexmlUrl(String xmlUrl) {
        JSONObject xmlConfig = new JSONObject();
        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);
            /*jdom 解析xml数据*/
            SAXBuilder sax = new SAXBuilder();
            Document doc = (Document) sax.build(new StringReader(document.toString()));
            Element stu = doc.getRootElement();
            List list = stu.getChildren("Model");
            for(int i=0;i

 

你可能感兴趣的:(xml,java类)