Search code examples
javaandroidxmlparsexml

XML Parse in website


I'm trying parse a webpage in XML and print data in my logcat, but my output is empty. This is my XML structure:

    <?xml version="1.0"?>
<FLIGHTS>
    <FLIGHT airport="b: 3  "
        logo="IG"
        code="IG"
        numero="1234" 
        carrier="AirBerlin"
        city="NEW YORK"
        terminal="Terminal A"
        sched="08:40"
        expect="09:09" 
        tipo_volo="L" stato="J"
    </FLIGHT>
    <FLIGHT airport="c: 3  "
     ....
     .....more...
    </FLIGHT>
</FLIGHTS>

and this is my Android code inside AsyncTask:

     @Override
    protected String doInBackground(String... params) {

        try {


            URL url = new URL("http://myurl");
            URLConnection conn = url.openConnection();

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

            NodeList nodes = doc.getElementsByTagName("FLIGHT");
            for (int i = 0; i < nodes.getLength(); i++) {
                Element element = (Element) nodes.item(i);
              // NodeList title = element.getElementsByTagName("airport");
               // Element line = (Element) title.item(0);
           //     a.add(line.getTextContent());

                Log.d("LOG...", "" + element.getTextContent());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

inside the AndroidManifest, i have the internet permission!

Thank you!


Solution

  • Your code sample works. Java is case sensitive. Are you sure your XML tag name is correct?

    Here is a working example using a XML example of w3schools:

      try {
    
             URL url = new URL("http://www.w3schools.com/xml/note.xml");
             URLConnection conn = url.openConnection();
    
             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
             DocumentBuilder builder = factory.newDocumentBuilder();
             Document doc = builder.parse(conn.getInputStream());
    
             NodeList nodes = doc.getElementsByTagName("note");
             for (int i = 0; i < nodes.getLength(); i++) {
                Element element = (Element) nodes.item(i);
                // NodeList title = element.getElementsByTagName("airport");
                // Element line = (Element) title.item(0);
                // a.add(line.getTextContent());
    
                System.out.println(element.getTextContent());
             }
          } catch (Exception e) {
             e.printStackTrace();
          }
    

    And now using your XML example and Joop Eggen's answer:

    try {
    
         URL url = new URL("http://myurl");
         URLConnection conn = url.openConnection();
    
         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
         DocumentBuilder builder = factory.newDocumentBuilder();
         Document doc = builder.parse(conn.getInputStream());
    
         NodeList nodes = doc.getElementsByTagName("FLIGHT");
         for (int i = 0; i < nodes.getLength(); i++) {
            Element element = (Element) nodes.item(i);
            String airport = element.getAttribute("airport");
    
            System.out.println("element " + i + ": airport=" + airport);
         }
      } catch (Exception e) {
         e.printStackTrace();
      }
    

    Example using a simple file:

    test.xml:

    <?xml version="1.0"?>
    <FLIGHTS>
        <FLIGHT airport="b: 3  " logo="IG" code="IG" numero="1234"
            carrier="AirBerlin" city="NEW YORK" terminal="Terminal A" sched="08:40"
            expect="09:09" tipo_volo="L" stato="J">
        </FLIGHT>
    </FLIGHTS>
    

    Example.java:

    import java.io.File;
    import java.io.FileInputStream;
    
    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;
    
    public class Example {
    
        public static void main(String[] args) {
        File f = new File("test.xml");
        try {
    
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(new FileInputStream(f));
    
            NodeList nodes = doc.getElementsByTagName("FLIGHT");
            for (int i = 0; i < nodes.getLength(); i++) {
            Element element = (Element) nodes.item(i);
            String airport = element.getAttribute("airport");
    
            System.out.println("element " + i + ": airport='" + airport + "'");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        }
    }