2

If I copy and paste the xml from this site into a xml file I can parse it with java

http://api.indeed.com/ads/apisearch?publisher=8397709210207872&q=java&l=austin%2C+tx&sort&radius&st&jt&start&limit&fromage&filter&latlong=1&chnl&userip=1.2.3.4&v=2

However, I want to parse it directly from a webpage if possible!

Here's my current 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");
}
 }

any help would be appreciated.

3 Answers 3

3

Give it the URI instead of the XML. It will download it for you.

Document doc = dBuilder.parse(uriString)

Sign up to request clarification or add additional context in comments.

Comments

1

Please find the code snippet like this

String url = "http://api.indeed.com/ads/apisearch?publisher=8397709210207872&q=java&l=austin%2C+tx&sort&radius&st&jt&start&limit&fromage&filter&latlong=1&chnl&userip=1.2.3.4&v=2";

try
{
  DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
  DocumentBuilder b = f.newDocumentBuilder();
  Document doc = b.parse(url);
}

Comments

0

That's how I parsed data directly from url http://www.nbp.pl/kursy/xml/+something

static class Kurs {
    public float kurs_sprzedazy;
    public float kurs_kupna;
}

private static DocumentBuilder dBuilder;

private static Kurs getData(String filename, String currency) throws Exception {
    Document doc = dBuilder.parse("http://www.nbp.pl/kursy/xml/"+filename+".xml");

    doc.getDocumentElement().normalize();
    NodeList nList = doc.getElementsByTagName("pozycja");

    for(int i = 0; i < nList.getLength(); i++) {
        Element nNode = (Element)nList.item(i);
        if(nNode.getElementsByTagName("kod_waluty").item(0).getTextContent().equals(currency)) {
            Kurs kurs = new Kurs();
            String data = nNode.getElementsByTagName("kurs_sprzedazy").item(0).getTextContent();
            data = data.replace(',', '.'); 
            kurs.kurs_sprzedazy = Float.parseFloat(data);
            data = nNode.getElementsByTagName("kurs_kupna").item(0).getTextContent();
            data = data.replace(',', '.');
            kurs.kurs_kupna = Float.parseFloat(data);
            return kurs;
        }
    }
    return null;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.