2010-06-17 7 views
14

Ho un file XML si trova in una posizione tale daXML da HTTP

http://example.com/test.xml 

Sto cercando di analizzare il file XML da utilizzare nel mio programma con xPath come questo, ma non funziona .

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

Come posso ottenere il file XML?

+1

Perché mettere una taglia di +100 per quello? Vedi la risposta di Nils devi solo prima prendere il tuo file xml come stream e poi analizzarlo. –

risposta

1

sbarazzarsi del new File():

Document doc = builder.parse(url); 
1

Un po 'più in dettaglio, in base alla risposta laz:

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

builder.parse non può gestire un URL. – Travis

+0

Uhmm, ok, ho fatto un errore. Ma è così che dovresti farlo. Per prima cosa apri una connessione con un URL, leggi il contenuto e poi analizzalo. Mi dispiace per quel fratello. – santiagobasulto

+1

Questo codice non verrà nemmeno compilato: http://docs.oracle.com/javase/8/docs/api/javax/xml/parsers/DocumentBuilder.html –

-1
File fileXml = new File(url); 

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

dovrebbe andare

21

Prova utilizzando URLConnection.getInputStream() per ottenere il manico di file XML.

vedere il codice qui sotto, nel senso che io sto cercando di aprire un file xml e la stampa di tutti i description campi:

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<descNodes.getLength();i++) 
     { 
      System.out.println(descNodes.item(i).getTextContent()); 
     } 
    } 

    private Document parseXML(InputStream stream) 
    throws Exception 
    { 
     DocumentBuilderFactory objDocumentBuilderFactory = null; 
     DocumentBuilder objDocumentBuilder = null; 
     Document doc = null; 
     try 
     { 
      objDocumentBuilderFactory = DocumentBuilderFactory.newInstance(); 
      objDocumentBuilder = objDocumentBuilderFactory.newDocumentBuilder(); 

      doc = objDocumentBuilder.parse(stream); 
     } 
     catch(Exception ex) 
     { 
      throw ex; 
     }  

     return doc; 
    } 
} 
+0

Esempio eccezionale. Grazie! – fivetwentysix

2

Ecco la semplice esempio per ottenere sotto forma di dati questa stringa "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); 

     } 
    }