Read the XML file on website

Hi Expert,
I have to download the information form the web site XML file and populate the ComboBox with these information. I would like if u could provide me with examples that can help me to do so -- regards
The information in the web site, as an example, like this :

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
- <FDStations>
- <FDStation>
- <FDStation>

I have to populate Combo Box with the Name above.

Who is Participating?
1. Get the root element of the Document (see link on the following page too)

2. Get the 'Name' elements

3. Construct a DefaultComboBoxModel with the resulting list to create your combo

this code is a genric parser used to parse an XML and return all the elements with its values.
Once you have got the XML from the inter net. you can pass it to this parser and get the required information.


import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class GenricParser {
      public static void main(String[] args) throws
            ParserConfigurationException, SAXException, IOException{
            DocumentBuilderFactory factory =
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(new File("result.xml"));
            Element root = document.getDocumentElement();
            NodeList nList = root.getChildNodes();

            for(int i=0;i<nList.getLength();i++){
                  Node node = nList.item(i);
                  if(node.getNodeType() == Node.ELEMENT_NODE){
                        NodeList nList1 = node.getChildNodes();
                        int count=0;
                        for(int j=0;j<nList1.getLength();j++){
                        Node node1 = nList1.item(j);
                                    }//End of IF
                              }//End of inner For(j)
                        }//End of outer IF
                  }//End of Outer For(i)


Instead of the file. you can give the String also to the parser

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.