• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 213
  • Last Modified:

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>
  <ID>BIH</ID>
  <Name>BISHOP AIRPORT</Name>
  <City>BISHOP</City>
  <State>CA</State>
  </FDStation>
- <FDStation>
  <ID>BLH</ID>
  <Name>BLYTHE AIRPORT</Name>
  <City>BLYTHE</City>
  <State>CA</State>
  </FDStation>
......

I have to populate Combo Box with the Name above.

0
westerntour
Asked:
westerntour
2 Solutions
 
CEHJCommented:
1. Get the root element of the Document (see link on the following page too)
http://javaalmanac.com/egs/org.w3c.dom/GetRoot.html

2. Get the 'Name' elements
http://java.sun.com/j2se/1.5.0/docs/api/org/w3c/dom/Element.html#getElementsByTagName(java.lang.String)

3. Construct a DefaultComboBoxModel with the resulting list to create your combo
http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/DefaultComboBoxModel.html

0
 
mukundha_expertCommented:
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 java.io.File;
import java.io.IOException;

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 =
                  DocumentBuilderFactory.newInstance();
            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){
                        System.out.println(node.getNodeName());
                        NodeList nList1 = node.getChildNodes();
                        int count=0;
                        for(int j=0;j<nList1.getLength();j++){
                        Node node1 = nList1.item(j);
                        if(!node1.getNodeName().startsWith("#"))
                        {
                        System.out.print(node1.getNodeName()+"-->");
                   System.out.println(node1.getChildNodes().item(0).getNodeValue());
                                          count++;
                                    }//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

Cheers,
Mukundh
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now