-5

I want to read a xml code from an url and print it, using java. The URL is http://ste.hwg.cz/values.xml

I tried many codes after reading stackoverflow answers but it doesnt work.

By the way , Im new to Java, I just programmed Pascal and Delphi

Awkzz
  • 1
  • 1
  • 1
  • First of all, search for "How to request/response on HTTP android" then when you get response, search for "parse XML in android". – Chintan Rathod Jun 06 '13 at 09:15
  • 1
    What have you tried? Have you tried this: [http://stackoverflow.com/questions/2310139/how-to-read-xml-response-from-a-url-in-java][1] [1]: http://stackoverflow.com/questions/2310139/how-to-read-xml-response-from-a-url-in-java – zargarf Jun 06 '13 at 09:16
  • read this: http://www.ibm.com/developerworks/library/x-android/ – thepoosh Jun 06 '13 at 09:19

2 Answers2

2

Parse or Print?

This is based on my tiny research on SO.

Parse:

For xml parsing of an inputstream you can do:

// the SAX way:
XMLReader myReader = XMLReaderFactory.createXMLReader();
myReader.setContentHandler(handler);
myReader.parse(new InputSource(new URL(url).openStream()));

// or if you prefer DOM:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new URL(url).openStream());

Print:

If you want to print XML directly onto the screen you can use TransformerFactory

URL url = new URL(urlString);
URLConnection conn = url.openConnection();

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(conn.getInputStream());

TransformerFactory factory = TransformerFactory.newInstance();
Transformer xform = factory.newTransformer();

// that’s the default xform; use a stylesheet to get a real one
xform.transform(new DOMSource(doc), new StreamResult(System.out));
Dheeraj Bhaskar
  • 18,633
  • 9
  • 63
  • 66
  • Print on the screen, but it isnt working, can you send me the full code with the import stuff and already the URL : http://ste.hwg.cz/values.xml on it? – Awkzz Jun 06 '13 at 09:33
  • @Awkzz that is spoon feeding and I do not approve of it. Is this an homework assignment? – Dheeraj Bhaskar Jun 06 '13 at 09:56
1

Here's the code I use for this kind of tasks:

public void readFromUrl(String inurl){
try {
    URL url = new URL(inurl);

    BufferedReader in = new BufferedReader(
        new InputStreamReader(url.openStream()));

        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            // HERE YOU CAN PRINT/LOG/SAVE THE LINE.
        }
        in.close();
} catch (Exception e) {
    e.printStackTrace();
}
}

P.S.

You can use StringBuilder to build a String containing your XML.

aveschini
  • 1,632
  • 3
  • 22
  • 39
  • It doesnt work :\, I tried with this : import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URL; public class Main { public void readFromUrl(String inurl){ try { URL url = new URL("http://ste.hwg.cz/values.xml"); – Awkzz Jun 06 '13 at 09:23
  • It doesnt work :\, I tried with this : ` import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URL; public class Main { public void readFromUrl(String inurl){ try { URL url = new URL("http://ste.hwg.cz/values.xml"); ` – Awkzz Jun 06 '13 at 09:28
  • How can I write the code here? – Awkzz Jun 06 '13 at 09:29
  • You can't write code here... Can you edit your question with the exception log? – aveschini Jun 06 '13 at 09:46