Parsing XML from webpage

davekeats

New Member
If I copy and paste the xml from this site into a xml file I can parse it with javahttp://api.indeed.com/ads/apisearch...mage&filter&latlong=1&chnl&userip=1.2.3.4&v=2However, I want to parse it directly from a webpage if possible!Here's my current code:\[code\]import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.ParserConfigurationException;import org.w3c.dom.Document;import org.w3c.dom.NodeList;import org.w3c.dom.Node;import org.w3c.dom.Element;import org.xml.sax.SAXException;import java.io.File;import java.io.IOException; public class XMLParser {public void readXML(String parse) { File xml = new File(parse); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder; try { dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(xml); // System.out.println("Root element :" // + doc.getDocumentElement().getNodeName()); NodeList nList = doc.getElementsByTagName("result"); System.out.println("----------------------------"); for (int temp = 0; temp < nList.getLength(); temp++) { Node nNode = nList.item(temp); // System.out.println("\nCurrent Element :" + nNode.getNodeName()); if (nNode.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) nNode; System.out.println("job title : " + eElement.getElementsByTagName("jobtitle").item(0) .getTextContent());; System.out.println("Company: " + eElement.getElementsByTagName("company") .item(0).getTextContent()); System.out.println("City : " + eElement.getElementsByTagName("city").item(0) .getTextContent()); System.out.println("State : " + eElement.getElementsByTagName("state").item(0) .getTextContent()); System.out.println("Country : " + eElement.getElementsByTagName("country").item(0) .getTextContent()); System.out.println("Date posted : " + eElement.getElementsByTagName("date").item(0) .getTextContent()); System.out.println("Job summary : " + eElement.getElementsByTagName("snippet").item(0) .getTextContent()); System.out.println("Latitude : " + eElement.getElementsByTagName("latitude").item(0).getTextContent()); System.out.println("longitude : " + eElement.getElementsByTagName("longitude").item(0).getTextContent()); } } } catch (ParserConfigurationException | SAXException | IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }}public static void main(String[] args) { new XMLParser().readXML("test.xml");} }\[/code\]any help would be appreciated.
 
Back
Top