来自HTTP的XML解析文件

我有一个XML文件位于如下的位置

http://example.com/test.xml 

我正在尝试解析XML文件以在我的程序中使用它与xPath这样但它无法正常工作。

 Document doc = builder.parse(new File(url)); 

我怎样才能获得XML文件?

尝试使用URLConnection.getInputStream()来获取XML文件的句柄。

请参阅下面的代码,我试图打开一个xml文件并打印所有description字段:

 import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.NodeList; public class HTTPXMLTest { public static void main(String[] args) { try { new HTTPXMLTest().start(); } catch (Exception e) { e.printStackTrace(); } } private void start() throws Exception { URL url = new URL("http://localhost:8080/AutoLogin/resource/web.xml"); URLConnection connection = url.openConnection(); Document doc = parseXML(connection.getInputStream()); NodeList descNodes = doc.getElementsByTagName("description"); for(int i=0; i 

以下是从此字符串“ http://www.gettingagile.com/feed/rss2/ ”获取数据的简单示例

 public class MainClassXml { public static void main(String args[]) throws URISyntaxException, ClientProtocolException, IOException, MalformedURLException { String url = "http://www.gettingagile.com/feed/rss2/"; System.out.println("Url is careated****"); URL url2 = new URL(url); HttpGet httpGet = new HttpGet(url); HttpClient httpClient = new DefaultHttpClient(); HttpResponse httpResponse = httpClient.execute(httpGet); HttpEntity entity = httpResponse.getEntity(); System.out.println("Entity is*****" + entity); try { String xmlParseString = EntityUtils.toString(entity); System.out.println("This Stirng to be Pasrse***" + xmlParseString); HttpURLConnection connection = (HttpURLConnection) url2 .openConnection(); InputStream inputStream = connection.getInputStream(); DocumentBuilderFactory builderFactory = DocumentBuilderFactory .newInstance(); DocumentBuilder documentBuilder = builderFactory .newDocumentBuilder(); Document document = documentBuilder.parse(inputStream); document.getDocumentElement().normalize(); System.out.println("Attributes are***" + document.getAttributes()); NodeList nodeList = document.getElementsByTagName("rss"); System.out.println("This is firstnode" + nodeList); for (int getChild = 0; getChild < nodeList.getLength(); getChild++) { Node Listnode = nodeList.item(getChild); System.out.println("Into the for loop" + Listnode.getAttributes().getLength()); Element firstnoderss = (Element) Listnode; System.out.println("ListNodes" + Listnode.getAttributes()); System.out.println("This is node list length" + nodeList.getLength()); Node Subnode = nodeList.item(getChild); System.out.println("This is list node" + Subnode); System.out.println("rss attributes***************"); } } catch (Exception exception) { System.out.println("Exception is" + exception); } } 

摆脱new File()

 Document doc = builder.parse(url); 

更多细节,基于laz答案:

 String urlString = "http://example.com/test.xml"; URL url = new URL(urlString); Document doc = builder.parse(url); 

使用XMLPullParser会更容易……你不必处理这些事件,并且可以快速获取一些关键字……我也在使用它……只有几行代码:)

http://developer.android.com/reference/org/xmlpull/v1/XmlPullParser.html

关于HTTP和文件看看这里下载一个文件与DefaultHTTPClient和抢先认证

 File fileXml = new File(url); DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document document = parser.parse(fileXml); 

它应该去